Skip to content

Cancel partial-message timers when disposing stream readers - #1950

Open
Pradeep Ramola (pradeep-ramola) wants to merge 3 commits into
microsoft:mainfrom
pradeep-ramola:fix/reader-partial-timer-disposal
Open

Pradeep Ramola (pradeep-ramola) wants to merge 3 commits into
microsoft:mainfrom
pradeep-ramola:fix/reader-partial-timer-disposal

Conversation

@pradeep-ramola

@pradeep-ramola Pradeep Ramola (pradeep-ramola) commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

ReadableStreamMessageReader inherits dispose() from AbstractMessageReader, which disposes its event emitters but leaves the partial-message timeout active. A peer that sends a header and then stalls can leave a repeating timeout retaining the disposed reader.

Cancel the pending timeout on disposal and prevent further partial-message timeout scheduling. The guard also covers disposal from an onPartialMessage handler, where the timeout callback would otherwise schedule another timer after the handler returns. Disposal state uses a native private field so it cannot collide with an existing subclass property or method.

Added deterministic timer tests for both disposal paths, plus a subclass compatibility regression. The timer tests fail without the cleanup; the subclass test reproduces TS2415 with the original TypeScript-private flag and passes with the native private field. Stream listener registration and transport ownership are unchanged.

Validation on the current branch:

  • npm run compile:jsonrpc
  • npm --prefix jsonrpc run lint
  • npm --prefix jsonrpc run test:node (108 passing)
  • npm --prefix jsonrpc run test:browser
  • git diff --check

The subclass and stream timer tests run in Node; the existing browser suite also passes. Editor integration suites were not run.

Copilot AI balanced review requested due to automatic review settings November 22, 2026 05:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are recorded for the change addressing partial-message timers retaining disposed JSON-RPC readers.

No open issues 🎉

What changed in this PR

This PR fixes partial-message timers retaining disposed JSON-RPC stream readers, without changing stream listener registration or transport ownership.

Changes:

  • Cancel pending partial-message timers when a reader is disposed.
  • Prevent timer rescheduling after disposal, including disposal inside a partial-message callback.
  • Add deterministic regression tests for both disposal paths.
File Description
jsonrpc/​src/​node/​test/​messages.test.ts Tests timer cancellation during direct and callback-triggered disposal.
jsonrpc/​src/​common/​messageReader.ts Tracks disposal state, clears pending timers, and blocks further scheduling.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 30, 2026 15:52

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block approval of the partial-message timer disposal fix.

No open issues 🎉

Review effort: Balanced

@dbaeumer Dirk Bäumer (dbaeumer) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The timer cleanup looks correct, including when a partial-message handler disposes the reader. Please make the disposal flag private at runtime so this fix stays compatible with existing reader subclasses.

Comment thread jsonrpc/src/common/messageReader.ts Outdated
private partialMessageTimer: Disposable | undefined;
private _partialMessageTimeout: number;
private readSemaphore: Semaphore<void>;
private isDisposed: boolean;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please use #isDisposed = false here, remove the constructor assignment, and use this.#isDisposed in dispose() and the timer guard. TypeScript's private still creates an ordinary JavaScript property. I verified that a StreamMessageReader subclass declaring isDisposed() compiles before this change but fails with TS2415 afterward; for existing JavaScript callers the boolean also shadows the method. A # field avoids both collisions without changing the timer behavior.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated in ef74784. The flag is now initialized as #isDisposed = false, the constructor assignment is removed, and both disposal and the timer guard use this.#isDisposed. Added a subclass regression test that failed compilation with TS2415 before the change and now verifies that its isDisposed() method remains callable before and after disposal. The existing timer-cancellation tests are unchanged. Validation: JSON-RPC compile, lint, all 108 Node tests, and the browser suite pass.

@dbaeumer Dirk Bäumer (dbaeumer) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See comment above

Copilot AI balanced review requested due to automatic review settings November 30, 2026 18:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the partial-message timer cleanup change.

No open issues 🎉

Review effort: Balanced

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.

3 participants