Skip to content

Clear leftover begin() onclose handler after the transaction settles - #1201

Open
gepotumu wants to merge 1 commit into
porsager:masterfrom
gepotumu:fix/clear-begin-onclose-after-commit
Open

Clear leftover begin() onclose handler after the transaction settles#1201
gepotumu wants to merge 1 commit into
porsager:masterfrom
gepotumu:fix/clear-begin-onclose-after-commit

Conversation

@gepotumu

Copy link
Copy Markdown

Summary

  • sql.begin() races the transaction against connection.onclose = reject so a disconnect during the transaction can fail the begin() promise.
  • After a successful commit that handler is never cleared, so a later pool disconnect (idle timeout, sql.end(), server-side recycle) still calls reject. The raced promise is already settled, so this becomes unhandledRejection — often undefined.
  • Clear connection.onclose in finally. Add a regression test that commits a transaction, then closes the pool and asserts no leftover rejection.

Test plan

  • node tests/index.js (needs the usual postgres_js_test setup)
  • Confirm the new test fails on current master and passes with this change
  • After await sql.begin(...), close the connection via sql.end({ timeout: 0 }) or idle timeout and confirm no unhandledRejection

Made with Cursor

sql.begin() races the transaction against connection.onclose = reject,
but never removes that handler. After a successful commit the pooled
connection can later close (idle timeout, sql.end, server-side recycle)
and reject(undefined), which surfaces as an unhandledRejection.

Co-authored-by: Cursor <cursoragent@cursor.com>
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.

1 participant