Skip to content

fix(better-sqlite3): await query event subscribers and report failed queries - #12752

Merged
alumni merged 1 commit into
typeorm:masterfrom
kyungseopk1m:fix/better-sqlite3-query-event-broadcast
Aug 26, 2026
Merged

alumni merged 1 commit into
typeorm:masterfrom
kyungseopk1m:fix/better-sqlite3-query-event-broadcast

Conversation

@kyungseopk1m

@kyungseopk1m kyungseopk1m commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

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 calls broadcastBeforeQueryEvent() directly instead of awaiting the BeforeQuery broadcast, and broadcasterResult.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 sqlite one, but better-sqlite3 is not in that PR's file list. #11836 later dropped sqlite3 and made better-sqlite3 the default SQLite driver, so the runner that missed the fix became the one most people end up using.

Reproduced against master with a stub connection and an async subscriber:

better-sqlite3                sqljs
1. QUERY EXECUTED             1. beforeQuery settled
2. query() resolved           2. QUERY EXECUTED
3. beforeQuery settled        3. afterQuery settled
4. afterQuery settled         4. query() resolved

So an async beforeQuery hook runs after the query it is meant to precede, and query() resolves before afterQuery has 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 catch block never broadcasts afterQuery with success set to false, so subscribers are told nothing when a query fails, and again this is the only broadcasting runner missing that call. And getStmt() sits outside the try, so a statement that fails to prepare throws the raw better-sqlite3 error instead of QueryFailedError:

                better-sqlite3        sqljs
error type      SqliteError           QueryFailedError
afterQuery      not broadcast         success: false

The fix awaits the BeforeQuery broadcast, broadcasts the failure event, waits for the subscribers in a finally, and moves the prepare into the try. All four match what the sibling runners already do.

One thing I deliberately left alone: if an afterQuery subscriber rejects, the finally lets that rejection replace the original QueryFailedError. That is not introduced here. All twelve broadcasting runners behave that way today, and unmodified sqljs on master does the same, so I kept the parity rather than making better-sqlite3 differ 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 queryStartTime line 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 the QueryFailedError wrapping.

test/github-issues/11085/issue-11085.test.ts gains better-sqlite3 in enabledDrivers. That test was postgres only, which is why the gap went unnoticed. With the driver added it fails on master with expected +0 to equal 1 and 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

  • Code is up-to-date with the master branch
  • This pull request links a relevant issue using a closing keyword:
    Fixes #12753
  • There are new or updated tests validating the change (tests/**.test.ts)
  • Documentation has been updated to reflect this change (docs/docs/**.md): N/A

…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.
@github-actions github-actions Bot added the needs-triage PR needs issue link triage label Aug 4, 2026
@pkg-pr-new

pkg-pr-new Bot commented Aug 4, 2026

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm@12752
    

commit: f38796c

@github-actions github-actions Bot added linked-issue PR references an issue and removed needs-triage PR needs issue link triage labels Aug 4, 2026
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

  • Author self-review: I have reviewed the code review findings, and addressed the relevant ones.

To customize comments, go to the Qodo configuration screen, or learn more in the docs.

Qodo Logo

@gioboa gioboa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kyungseopk1m for your help 🎉

@alumni
alumni merged commit 8cf2258 into typeorm:master Aug 26, 2026
50 checks passed
@github-actions github-actions Bot added this to the 2.0.next milestone Aug 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

linked-issue PR references an issue

Development

Successfully merging this pull request may close these issues.

better-sqlite3 does not await query event subscribers and never reports failed queries

3 participants