Skip to content

fix: release SQL Server query locks after subscriber errors - #12939

Merged
naorpeled merged 3 commits into
typeorm:masterfrom
IbrahimHafez1:codex/sqlserver-subscriber-lock-release
Oct 8, 2026
Merged

naorpeled merged 3 commits into
typeorm:masterfrom
IbrahimHafez1:codex/sqlserver-subscriber-lock-release

Conversation

@IbrahimHafez1

Copy link
Copy Markdown
Contributor

Description of change

Fixes #12936.

A thrown/rejected beforeQuery hook exits after SQL Server's query lock is acquired but before its protected execution block. A rejected afterQuery promise similarly bypasses release() inside finally. Both leave subsequent queries on the runner waiting indefinitely.

Release and rethrow if pre-query logging/broadcasting fails; guarantee release with a nested finally around the post-query subscriber wait. Successful hook ordering, database error handling, and the awaited beforeQuery behavior introduced by #11086 are preserved. The fix adds no queries, timers, lock acquisitions, or successful-path asynchronous boundaries.

Validation (Node.js 22.17.1):

  • Three new cases fail on unchanged master: synchronous/asynchronous beforeQuery failures and asynchronous afterQuery failure block the next query.
  • All 7 lock tests pass after the fix, including synchronous afterQuery, pending-hook ordering, and a database-request error control.
  • The tests use the real SQL Server query runner, broadcaster and lock; only the mssql pool/request transport is stubbed. No live SQL Server instance was tested locally.
  • Full unit suite: 146 passing.
  • Complete TypeScript compilation, focused ESLint (no errors), formatting and diff checks pass.

Developed with AI assistance; source, prior fixes, regression behavior, and final diff reviewed.

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • This pull request links a relevant issue using a closing keyword
  • There are new or updated tests validating the change
  • Documentation has been updated to reflect this change — N/A: restores failure cleanup without changing subscriber APIs

@github-actions github-actions Bot added the linked-issue PR references an issue label Oct 5, 2026
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Later queries overlap unfinished hooks 🐞 Bug ≡ Correctness
Description
The new beforeQuery catch releases the query lock as soon as broadcast() rejects, although its
promise wait can reject while another subscriber is still running. With one rejecting hook and
another pending hook, the next query acquires the lock before that pending hook settles; the new
afterQuery release path has the same behavior.
Code

packages/typeorm/src/driver/sqlserver/SqlServerQueryRunner.ts[R231-233]

+        } catch (error) {
+            release()
+            throw error
Evidence
The broadcaster collects returned hook promises, but its wait uses fail-fast Promise.all. The
newly added release calls therefore unblock the next lock waiter on the first rejection rather than
after all started hooks settle; the lock test establishes that a pending hook should keep the next
query blocked.

packages/typeorm/src/subscriber/Broadcaster.ts[105-118]
packages/typeorm/src/subscriber/Broadcaster.ts[464-485]
packages/typeorm/src/subscriber/Broadcaster.ts[493-526]
packages/typeorm/src/subscriber/BroadcasterResult.ts[14-22]
packages/typeorm/src/query-runner/QueryLock.ts[8-24]
packages/typeorm/src/driver/sqlserver/SqlServerQueryRunner.ts[226-234]
packages/typeorm/src/driver/sqlserver/SqlServerQueryRunner.ts[354-359]
packages/typeorm/test/unit/driver/sqlserver-query-lock.test.ts[55-78]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A rejected query hook can release the SQL Server query lock while other hooks from the same event remain pending, allowing the next query to overlap them.
## Fix Focus Areas
- packages/typeorm/src/driver/sqlserver/SqlServerQueryRunner.ts[228-233]
- packages/typeorm/src/driver/sqlserver/SqlServerQueryRunner.ts[355-359]
- packages/typeorm/src/subscriber/BroadcasterResult.ts[18-22]
## Recommended Fix
Ensure all started hook promises settle before either failure path releases the lock, then propagate the hook failure. Cover both before- and after-query events with a test containing one rejecting hook and one independently pending hook.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


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

Grey Divider

Tip of the day
💡 Did you know, you can keep summaries lean with Findings visible per group, which tucks the rest behind a View link

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@pkg-pr-new

pkg-pr-new Bot commented Oct 5, 2026 •

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

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

commit: ba08ce7

@Cprakhar Cprakhar 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 @IbrahimHafez1 for the fix!

@Cprakhar
Cprakhar requested review from alumni and gioboa October 5, 2026 17:21

@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.

@Cprakhar Cprakhar mentioned this pull request Oct 8, 2026
1 of 4 tasks
@naorpeled
naorpeled enabled auto-merge (squash) October 8, 2026 04:38
@naorpeled
naorpeled merged commit 9f5a345 into typeorm:master Oct 8, 2026
46 checks passed
@github-actions github-actions Bot added this to the 2.0.next milestone Oct 8, 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.

SQL Server query subscriber failures retain the query-runner lock

4 participants