Repository navigation
Cancel partial-message timers when disposing stream readers - #1950
Pradeep Ramola (pradeep-ramola) wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
🟢 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
Dirk Bäumer (dbaeumer)
left a comment
There was a problem hiding this comment.
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.
| private partialMessageTimer: Disposable | undefined; | ||
| private _partialMessageTimeout: number; | ||
| private readSemaphore: Semaphore<void>; | ||
| private isDisposed: boolean; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Dirk Bäumer (dbaeumer)
left a comment
There was a problem hiding this comment.
See comment above
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:
The subclass and stream timer tests run in Node; the existing browser suite also passes. Editor integration suites were not run.