Repository navigation
fix(better-sqlite3): await query event subscribers and report failed queries - #12752
Merged
alumni merged 1 commit intoAug 26, 2026
Merged
Conversation
…queries Of the runners that implement query events, better-sqlite3 is the only one that never waits for its subscribers. It calls broadcastBeforeQueryEvent directly instead of awaiting the BeforeQuery broadcast, and it never calls broadcasterResult.wait(). An async beforeQuery hook therefore runs after the query it is supposed to precede, query() settles before afterQuery has finished, and a rejecting subscriber escapes as an unhandled rejection. The same method has two further gaps. The catch block never broadcasts afterQuery with success set to false, so subscribers are told nothing when a query fails. And getStmt() sits outside the try, so a statement that fails to prepare throws the raw driver error rather than QueryFailedError. Await the BeforeQuery broadcast like the other runners do, broadcast the failure event, wait for the subscribers in a finally, and move the prepare into the try.
commit: |
Code Review by Qodo🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)
Great, no issues found!Qodo reviewed your code and found no material issues that require review
To customize comments, go to the Qodo configuration screen, or learn more in the docs. |
alumni
approved these changes
Aug 26, 2026
gioboa
approved these changes
Aug 26, 2026
gioboa
left a comment
Collaborator
There was a problem hiding this comment.
Thanks @kyungseopk1m for your help 🎉
This was referenced Aug 27, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description of change
Fixes #12753
BetterSqlite3QueryRunner.query()broadcasts the query events but never waits for the subscribers, and among the runners that implement those events it is the only one that doesn't. It callsbroadcastBeforeQueryEvent()directly instead of awaiting theBeforeQuerybroadcast, andbroadcasterResult.wait()appears nowhere in the file. The other eleven (cockroachdb, cordova, expo, mysql, oracle, postgres, react-native, sap, spanner, sqljs, sqlserver) all do both.This looks like an oversight rather than an intentional difference. #11086 fixed exactly this across 13 runners, including the old
sqliteone, butbetter-sqlite3is not in that PR's file list. #11836 later droppedsqlite3and madebetter-sqlite3the default SQLite driver, so the runner that missed the fix became the one most people end up using.Reproduced against
masterwith a stub connection and an async subscriber:So an async
beforeQueryhook runs after the query it is meant to precede, andquery()resolves beforeafterQueryhas finished. A rejecting subscriber has nothing awaiting it and surfaces as an unhandled rejection, which ends the process under Node's default behaviour.Two more gaps in the same method. The
catchblock never broadcastsafterQuerywithsuccessset to false, so subscribers are told nothing when a query fails, and again this is the only broadcasting runner missing that call. AndgetStmt()sits outside thetry, so a statement that fails to prepare throws the raw better-sqlite3 error instead ofQueryFailedError:The fix awaits the
BeforeQuerybroadcast, broadcasts the failure event, waits for the subscribers in afinally, and moves the prepare into thetry. All four match what the sibling runners already do.One thing I deliberately left alone: if an
afterQuerysubscriber rejects, thefinallylets that rejection replace the originalQueryFailedError. That is not introduced here. All twelve broadcasting runners behave that way today, and unmodified sqljs onmasterdoes the same, so I kept the parity rather than makingbetter-sqlite3differ in a new direction. #12724 makes the case for the stricter behaviour and is a better place to settle it for the whole family.Also worth flagging: #12750 edits the adjacent
queryStartTimeline in this file. The two changes don't overlap semantically and a three way merge applies cleanly, but whichever lands second will want a quick rebase.Tests
test/functional/entity-subscriber/query-events/is new. It runs on every driver except mongodb, which doesn't go through this path, and spanner, which the existing subscriber tests already exclude. It covers the settle ordering on the success and failure paths, the failure event, and theQueryFailedErrorwrapping.test/github-issues/11085/issue-11085.test.tsgainsbetter-sqlite3inenabledDrivers. That test was postgres only, which is why the gap went unnoticed. With the driver added it fails onmasterwithexpected +0 to equal 1and passes with this change.Reverting each of the four parts on its own breaks at least one test, so none of them is decoration.
Full suite against better-sqlite3, postgres, mysql, mariadb and mssql: 3070 passing, 172 pending, 0 failing. Oracle, SAP HANA and Spanner were not run locally.
Pull-Request Checklist
masterbranchFixes #12753tests/**.test.ts)docs/docs/**.md): N/A