Skip to content

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

Merged
tnaum-ms merged 2 commits into
microsoft:mainfrom
hanhan761:fix-644-race-abort-connect
Jun 19, 2026
Merged

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

Conversation

@hanhan761

@hanhan761 hanhan761 commented May 29, 2026

Copy link
Copy Markdown
Contributor

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

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
@tnaum-ms

Copy link
Copy Markdown
Collaborator

Thank you again for this contribution. We are genuinely amazed by both the quality and the volume of improvements you have submitted. We also apologize for the delayed review while the 0.9.0 release was in flight. This race condition fix is thoughtful, precise, and very valuable for connection stability.

@tnaum-ms tnaum-ms removed the in-triage-queue We've seen your input and will triage it label Jun 19, 2026
@tnaum-ms tnaum-ms added this to the 0.9.1 milestone Jun 19, 2026
hanhan761 and others added 2 commits June 19, 2026 09:56
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>
@
@tnaum-ms tnaum-ms force-pushed the fix-644-race-abort-connect branch from 469f262 to 0b66bab Compare June 19, 2026 09:59

@tnaum-ms tnaum-ms 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 again for the excellent contribution. Approved after rebase and full validation pass.

@tnaum-ms tnaum-ms enabled auto-merge June 19, 2026 10:30
@tnaum-ms tnaum-ms merged commit c0a1be5 into microsoft:main Jun 19, 2026
5 checks passed
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.

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

3 participants