-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: evict connections with open transactions on pool release #3597
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
Open
panga
wants to merge
5
commits into
brianc:master
Choose a base branch
from
panga:poison-pool
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.
+237
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
af7d4ad
feat: close connections with open transactions on release
eceec55
fix: handle empty message
panga f72010f
feat: add log and more tests
panga 287728b
fix: flakly integration test
panga 74f4336
fix: ignore txStatus tests in native mode
panga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| 'use strict' | ||
|
|
||
| const expect = require('expect.js') | ||
| const describe = require('mocha').describe | ||
| const it = require('mocha').it | ||
| const Pool = require('..') | ||
|
|
||
| describe('leaked connection pool guard', function () { | ||
| it('removes a client with an open transaction on release', async function () { | ||
| const logMessages = [] | ||
| const pool = new Pool({ | ||
| max: 1, | ||
| log: (msg) => logMessages.push(msg), | ||
| }) | ||
| const client = await pool.connect() | ||
| await client.query('BEGIN') | ||
| expect(client._txStatus).to.be('T') | ||
|
|
||
| client.release() | ||
| expect(pool.totalCount).to.be(0) | ||
| expect(pool.idleCount).to.be(0) | ||
| expect(logMessages).to.contain('remove client with leaked transaction') | ||
|
|
||
| // pool recovers by creating a fresh connection | ||
| const { rows } = await pool.query('SELECT 1 as num') | ||
| expect(rows[0].num).to.be(1) | ||
| expect(pool.totalCount).to.be(1) | ||
| expect(pool.idleCount).to.be(1) | ||
|
|
||
| await pool.end() | ||
| }) | ||
|
|
||
| it('removes a client in a failed transaction state on release', async function () { | ||
| const pool = new Pool({ max: 1 }) | ||
| const client = await pool.connect() | ||
| await client.query('BEGIN') | ||
| try { | ||
| await client.query('SELECT invalid_column FROM nonexistent_table') | ||
| } catch (e) { | ||
| // swallow the error to avoid pool close the connection | ||
| } | ||
| // The ReadyForQuery message with status 'E' may arrive on a separate I/O event. | ||
| // Issue a follow-up query to ensure it has been processed — this will also fail | ||
| // (since the transaction is aborted) but guarantees _txStatus is updated. | ||
| try { | ||
| await client.query('SELECT 1') | ||
| } catch (e) { | ||
| // expected — "current transaction is aborted" | ||
| } | ||
| expect(client._txStatus).to.be('E') | ||
|
|
||
| client.release() | ||
| expect(pool.totalCount).to.be(0) | ||
| expect(pool.idleCount).to.be(0) | ||
|
|
||
| // pool recovers by creating a fresh connection | ||
| const { rows } = await pool.query('SELECT 1 as num') | ||
| expect(rows[0].num).to.be(1) | ||
| expect(pool.totalCount).to.be(1) | ||
| expect(pool.idleCount).to.be(1) | ||
|
|
||
| await pool.end() | ||
| }) | ||
|
|
||
| it('only removes connections with open transactions, keeps idle ones', async function () { | ||
| const pool = new Pool({ max: 3 }) | ||
| const clientA = await pool.connect() | ||
| const clientB = await pool.connect() | ||
| const clientC = await pool.connect() | ||
|
|
||
| // Client A: open transaction (leaked) | ||
| await clientA.query('BEGIN') | ||
| expect(clientA._txStatus).to.be('T') | ||
|
|
||
| // Client B: normal query (idle) | ||
| await clientB.query('SELECT 1') | ||
| expect(clientB._txStatus).to.be('I') | ||
|
|
||
| // Client C: committed transaction (idle) | ||
| await clientC.query('BEGIN') | ||
| await clientC.query('COMMIT') | ||
| expect(clientC._txStatus).to.be('I') | ||
|
|
||
| clientA.release() | ||
| clientB.release() | ||
| clientC.release() | ||
|
|
||
| // A was removed, B and C kept | ||
| expect(pool.totalCount).to.be(2) | ||
| expect(pool.idleCount).to.be(2) | ||
| await pool.end() | ||
| }) | ||
|
|
||
| describe('pool.query', function () { | ||
| it('removes a client after pool.query leaks transaction via BEGIN', async function () { | ||
| const logMessages = [] | ||
| const pool = new Pool({ max: 1, log: (msg) => logMessages.push(msg) }) | ||
|
|
||
| await pool.query('BEGIN') | ||
|
|
||
| // Client auto-released with txStatus='T', should be removed | ||
| expect(pool.totalCount).to.be(0) | ||
| expect(pool.idleCount).to.be(0) | ||
| expect(logMessages).to.contain('remove client with leaked transaction') | ||
|
|
||
| // Verify pool recovers | ||
| const { rows } = await pool.query('SELECT 1 as num') | ||
| expect(rows[0].num).to.be(1) | ||
| expect(pool.totalCount).to.be(1) | ||
| expect(pool.idleCount).to.be(1) | ||
|
|
||
| await pool.end() | ||
| }) | ||
|
|
||
| it('removes a client after pool.query in failed transaction state', async function () { | ||
| const pool = new Pool({ max: 1 }) | ||
|
|
||
| await pool.query('BEGIN') | ||
|
|
||
| try { | ||
| await pool.query('SELECT invalid_column FROM nonexistent_table') | ||
| } catch (e) { | ||
| // Expected error | ||
| } | ||
|
|
||
| // Client with txStatus='E' should be removed | ||
| expect(pool.totalCount).to.be(0) | ||
| expect(pool.idleCount).to.be(0) | ||
|
|
||
| // Pool recovers | ||
| const { rows } = await pool.query('SELECT 1 as num') | ||
| expect(rows[0].num).to.be(1) | ||
| expect(pool.totalCount).to.be(1) | ||
| expect(pool.idleCount).to.be(1) | ||
|
|
||
| await pool.end() | ||
| }) | ||
| }) | ||
| }) |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| 'use strict' | ||
| const helper = require('./test-helper') | ||
| const suite = new helper.Suite() | ||
| const pg = helper.pg | ||
| const assert = require('assert') | ||
|
|
||
| // txStatus tracking is not implemented in native client | ||
| if (!helper.args.native) { | ||
| suite.test('txStatus tracking', function (done) { | ||
| const client = new pg.Client() | ||
| client.connect( | ||
| assert.success(function () { | ||
| // Run a simple query to initialize txStatus | ||
| client.query( | ||
| 'SELECT 1', | ||
| assert.success(function () { | ||
| // Test 1: Initial state after query (should be idle) | ||
| assert.equal(client._txStatus, 'I', 'should start in idle state') | ||
|
|
||
| // Test 2: BEGIN transaction | ||
| client.query( | ||
| 'BEGIN', | ||
| assert.success(function () { | ||
| assert.equal(client._txStatus, 'T', 'should be in transaction state') | ||
|
|
||
| // Test 3: COMMIT | ||
| client.query( | ||
| 'COMMIT', | ||
| assert.success(function () { | ||
| assert.equal(client._txStatus, 'I', 'should return to idle after commit') | ||
|
|
||
| client.end(done) | ||
| }) | ||
| ) | ||
| }) | ||
| ) | ||
| }) | ||
| ) | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| suite.test('txStatus error state', function (done) { | ||
| const client = new pg.Client() | ||
| client.connect( | ||
| assert.success(function () { | ||
| // Run a simple query to initialize txStatus | ||
| client.query( | ||
| 'SELECT 1', | ||
| assert.success(function () { | ||
| client.query( | ||
| 'BEGIN', | ||
| assert.success(function () { | ||
| // Execute invalid SQL to trigger error state | ||
| client.query('INVALID SQL SYNTAX', function (err) { | ||
| assert(err, 'should receive error from invalid query') | ||
|
|
||
| // Issue a sync query to ensure ReadyForQuery has been processed | ||
| // This guarantees _txStatus has been updated | ||
| client.query('SELECT 1', function () { | ||
| // This callback fires after ReadyForQuery is processed | ||
| assert.equal(client._txStatus, 'E', 'should be in error state') | ||
|
|
||
| // Rollback to recover | ||
| client.query( | ||
| 'ROLLBACK', | ||
| assert.success(function () { | ||
| assert.equal(client._txStatus, 'I', 'should return to idle after rollback from error') | ||
| client.end(done) | ||
| }) | ||
| ) | ||
| }) | ||
| }) | ||
| }) | ||
| ) | ||
| }) | ||
| ) | ||
| }) | ||
| ) | ||
| }) | ||
| } |
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.
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.
Is this check just for tests and pg-native? Both should probably be updated to consistently report a status in their
readyForQueryevents.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.
Added integration tests and updated PR description. However
pg-nativeis not supported because this is a protocol level feature.