Skip to content

Fix OOM crash, when closing a transaction while Queries are still ongoing #1193

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

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/core/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ class Transaction {
// error will be "acknowledged" by sending a RESET message
// database will then forget about this transaction and cleanup all corresponding resources
// it is thus safe to move this transaction to a FAILED state and disallow any further interactions with it

if (this._state === _states.FAILED) {
// already failed, nothing to do
// if we call onError for each result again, we might run into an infinite loop, that causes an OOM eventually
return Promise.resolve(null)
}
this._state = _states.FAILED
this._onClose()
this._results.forEach(result => {
Expand Down
6 changes: 6 additions & 0 deletions packages/neo4j-driver-deno/lib/core/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ class Transaction {
// error will be "acknowledged" by sending a RESET message
// database will then forget about this transaction and cleanup all corresponding resources
// it is thus safe to move this transaction to a FAILED state and disallow any further interactions with it

if (this._state === _states.FAILED) {
// already failed, nothing to do
// if we call onError for each result again, we might run into an infinite loop, that causes an OOM eventually
return Promise.resolve(null)
}
this._state = _states.FAILED
this._onClose()
this._results.forEach(result => {
Expand Down
25 changes: 25 additions & 0 deletions packages/neo4j-driver/test/transaction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,29 @@ describe('#integration transaction', () => {
.catch(done.fail.bind(done))
)
}

/**
* This test is to verify that the driver does not run out of memory when one of the queries fails,
* while others are concurrently running and the transaction is being committed/closed.
*
* Because this causes an infinite loop in the failure case, the test has a timeout of 50 seconds, in order to actualy
* trip the OOM error, and not run into a timeout error prematurely.
*/
it('transactions should not run OOM when one of the queries fails', async () => {
const transaction = await session.beginTransaction()
const queries = Array.from(
{ length: 10 },
(_, i) => () => transaction.run(`RETURN ${i}`)
)
const destroy = () => transaction.close()

const calls = [...queries, destroy, ...queries]
const promises = calls.map(call => call())
try {
await Promise.all(promises)
} catch (e) {
// This is the success case, as we god an exception, not run into an OOM
expect(e.message).toBe('Cannot run query in this transaction, because it has already been rolled back.')
}
}, 50000)
})