This repository was archived by the owner on Aug 19, 2025. It is now read-only.
  
  
  - 
                Notifications
    You must be signed in to change notification settings 
- Fork 263
Fix connection leak when transaction commit/rollback raise an exception #498
          
     Open
      
      
            masipcat
  wants to merge
  6
  commits into
  encode:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
vinissimus:0.4.3-vinissimus-hotfix
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          Conversation
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
    | Hi, thanks for the PR. | 
| Yeah no problem I'll add a test | 
              
                    masipcat
  
              
              commented
              
                  
                    Jul 29, 2022 
                  
              
              
            
            
      Comment on lines
    
      +554
     to 
      +570
    
  
  | async with Database(database_url) as database: | ||
| with pytest.raises(Exception) as excinfo: | ||
| async with database.connection() as connection: | ||
| await connection.execute( | ||
| """ | ||
| CREATE TABLE test (id integer PRIMARY KEY INITIALLY DEFERRED); | ||
| """ | ||
| ) | ||
| async with connection.transaction(): | ||
| await connection.execute("insert into test (id) values (1)") | ||
| await connection.execute("insert into test (id) values (1)") | ||
|  | ||
| # During transaction.commit() postgres will raise this exception: | ||
| # asyncpg.exceptions.UniqueViolationError: duplicate key value violates unique constraint "test_pkey" | ||
| # DETAIL: Key (id)=(1) already exists. | ||
| assert "unique constraint" in str(excinfo.value) | ||
| assert connection._connection_counter == 0 | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aminalaee I think this test it's only valid for postgresql. Is there a way to skip for other databases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah sure,
databases/tests/test_databases.py
Line 482 in 6a52a4a
| async def test_transaction_commit_serializable(database_url): | 
              
                    aminalaee
  
              
              reviewed
              
                  
                    Jul 29, 2022 
                  
              
              
            
            
  
      Sign up for free
      to subscribe to this conversation on GitHub.
      Already have an account?
      Sign in.
  
      
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
This fixes the following case: