Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a ContextVar to manage the sql connection instance as an instance… #169

Merged
merged 1 commit into from
Nov 1, 2023

Conversation

chaen
Copy link
Contributor

@chaen chaen commented Oct 31, 2023

The DB instances is unique throughout the application, so setting self._conn for each requests requires protection.

Without this PR, running

 for i in $(seq -w 10); do (dirac jobs search --all &) ; done

results in

Traceback (most recent call last):                                                                                                                                                                                                                                                                                                                                                                                                                                                          
  File "/home/chaen/mambaforge/envs/diracx-dev/lib/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 428, in run_asgi                                                                                                                                                                                                                                                                                                                                                      
    result = await app(  # type: ignore[func-returns-value]                                                                                                                                                                                                                                                                                                                                                                                                                                 
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                                                                                                                                                                                                                                                                 
  File "/home/chaen/mambaforge/envs/diracx-dev/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__                                                                                                                                                                                                                                                                                                                                                      
    return await self.app(scope, receive, send)                                                                                                                                                                                                                                                                                                                                                                                                                                             
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                                                                                                                                                                                                                                                                             
  File "/home/chaen/mambaforge/envs/diracx-dev/lib/python3.11/site-packages/fastapi/applications.py", line 284, in __call__                                                                                                                                                                                                                                                                                                                                                                 
    await super().__call__(scope, receive, send)                                                                                                                                                                                                                                                                                                                                                                                                                                            
  File "/home/chaen/mambaforge/envs/diracx-dev/lib/python3.11/site-packages/starlette/applications.py", line 122, in __call__                                                                                                                                                                                                                                                                                                                                                               
    await self.middleware_stack(scope, receive, send)                                                                                                                                                                                                                                                                                                                                                                                                                                       
  File "/home/chaen/mambaforge/envs/diracx-dev/lib/python3.11/site-packages/starlette/middleware/errors.py", line 184, in __call__                                                                                                                                                                                                                                                                                                                                                          
    raise exc                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
  File "/home/chaen/mambaforge/envs/diracx-dev/lib/python3.11/site-packages/starlette/middleware/errors.py", line 162, in __call__                                                                                                                                                                                                                                                                                                                                                          
    await self.app(scope, receive, _send)                                                                                                                                                                                                                                                                                                                                                                                                                                                   
  File "/home/chaen/mambaforge/envs/diracx-dev/lib/python3.11/site-packages/starlette/middleware/cors.py", line 83, in __call__                                                                                                                                                                                                                                                                                                                                                             
    await self.app(scope, receive, send)                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  File "/home/chaen/mambaforge/envs/diracx-dev/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 79, in __call__                                                                                                                                                                                                                                                                                                                                                       
    raise exc                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
  File "/home/chaen/mambaforge/envs/diracx-dev/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 68, in __call__                                                                                                                                                                                                                                                                                                                                                       
    await self.app(scope, receive, sender)                                                                                                                                                                                                                                                                                                                                                                                                                                                  
  File "/home/chaen/mambaforge/envs/diracx-dev/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 14, in __call__                                                                                                                                                                                                                                                                                                                                                     
    async with AsyncExitStack() as stack:                                                                                                                                                                                                                                                                                                                                                                                                                                                   
  File "/home/chaen/mambaforge/envs/diracx-dev/lib/python3.11/contextlib.py", line 733, in __aexit__                                                                                                                                                                                                                                                                                                                                                                                        
    raise exc_details[1]                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  File "/home/chaen/mambaforge/envs/diracx-dev/lib/python3.11/contextlib.py", line 716, in __aexit__                                                                                                                                                                                                                                                                                                                                                                                        
    cb_suppress = await cb(*exc_details)                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                  ^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  File "/home/chaen/mambaforge/envs/diracx-dev/lib/python3.11/contextlib.py", line 222, in __aexit__                                                                                                                                                                                                                                                                                                                                                                                        
    await self.gen.athrow(typ, value, traceback)                                                                                                                                                                                                                                                                                                                                                                                                                                            
  File "/home/chaen/dirac/diracx-project/diracx/src/diracx/routers/__init__.py", line 229, in db_transaction                                                                                                                                                                                                                                                                                                                                                                                
    async with db:                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
  File "/home/chaen/dirac/diracx-project/diracx/src/diracx/db/sql/utils.py", line 158, in __aexit__                                                                                                                                                                                                                                                                                                                                                                                         
    await self._conn.__aexit__(exc_type, exc, tb)                                                                                                                                                                                                                                                                                                                                                                                                                                           
          ^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                                                                                                                                                                                                                                                                                              
AttributeError: 'NoneType' object has no attribute '__aexit__' 

With it, it works fine.

@chaen chaen mentioned this pull request Oct 31, 2023
@chrisburr chrisburr merged commit 904f45a into DIRACGrid:main Nov 1, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants