-
-
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
9
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.
Open
Changes from 2 commits
Commits
Show all changes
9 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 1d66437
feat: add new pool option evictOnOpenTransaction
panga 60fabaa
feat: add new client.getTransactionStatus() method
panga b10746f
add native support
panga 2180079
bump libpq
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,82 @@ | ||
| 'use strict' | ||
|
|
||
| const expect = require('expect.js') | ||
| const describe = require('mocha').describe | ||
| const it = require('mocha').it | ||
| const Pool = require('..') | ||
|
|
||
| describe('poison connection pool defense (_txStatus check)', function () { | ||
| it('removes a client with an open transaction on release', async function () { | ||
| const pool = new Pool({ max: 1 }) | ||
| 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) | ||
|
|
||
| // pool should still work by creating a fresh connection | ||
| const { rows } = await pool.query('SELECT 1 as num') | ||
| expect(rows[0].num).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 should still work | ||
| const { rows } = await pool.query('SELECT 1 as num') | ||
| expect(rows[0].num).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 (poisoned) | ||
| 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() | ||
| }) | ||
| }) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.