Skip to content

fix: close micro race window between connect() resolve and abort listener removal#700

Open
hanhan761 wants to merge 2 commits into
microsoft:mainfrom
hanhan761:fix-644-race-abort-connect
Open

fix: close micro race window between connect() resolve and abort listener removal#700
hanhan761 wants to merge 2 commits into
microsoft:mainfrom
hanhan761:fix-644-race-abort-connect

Conversation

@hanhan761

Copy link
Copy Markdown

@

Summary

  • Added a connected flag that is set immediately after mongoClient.connect() resolves
  • The onAbort handler now checks this flag before calling close(), preventing a late abort from closing an already-connected client
  • Eliminates the micro race window between connect() resolving and removeEventListener firing

Issue

Fixes #644

Changes

File Change
src/documentdb/ClustersClient.ts Add connected flag; guard onAbort close with !connected check

Verification

  • The connected flag transitions from false to true atomically in the same synchronous tick as connect() resolves
  • If abort fires during connection: connected === false → close the client (existing behavior preserved)
  • If abort fires after connect but before removeEventListener: connected === true → skip close (race window closed)
  • The existing finally block still removes the listener for cleanup
  • No behavior change in normal (non-race) paths

@

fix: close micro race window between connect() resolve and abort listener removal

Add a `connected` flag that is set immediately after `mongoClient.connect()`
resolves. The `onAbort` handler now checks this flag before calling `close()`,
preventing a late abort from closing an already-connected client during the
synchronous window between connect() resolving and removeEventListener firing.

Fixes microsoft#644

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@
Copilot AI review requested due to automatic review settings May 29, 2026 13:37
@hanhan761 hanhan761 requested a review from a team as a code owner May 29, 2026 13:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a guard in the abort handler to prevent closing an already-connected MongoClient during the brief window between connect() resolving and the abort listener being removed.

Changes:

  • Introduce a connected flag tracked across the connect call.
  • Early-return from onAbort when connected is true.

@tnaum-ms tnaum-ms added the in-triage-queue We've seen your input and will triage it label Jun 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in-triage-queue We've seen your input and will triage it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Revisit: micro race window between connect() success and abort listener removal

3 participants