Skip to content

fix: clean up orchestration history streams#314

Open
YunchuWang wants to merge 1 commit into
microsoft:mainfrom
YunchuWang:copilot-finds/bug/fix-client-stream-resource-leak
Open

fix: clean up orchestration history streams#314
YunchuWang wants to merge 1 commit into
microsoft:mainfrom
YunchuWang:copilot-finds/bug/fix-client-stream-resource-leak

Conversation

@YunchuWang

Copy link
Copy Markdown
Member

Summary

  • Remove listeners and destroy the gRPC history stream on both completion and error.
  • Attach a terminal error handler before destruction to prevent unhandled errors.
  • Add focused stream cleanup coverage.

Fixes #215

Testing

  • npm test -w @microsoft/durabletask-js -- --runTestsByPath test/client-stream-cleanup.spec.ts
  • npm run build:core

…ce leak

getOrchestrationHistory() calls stream.removeAllListeners() but does not
call stream.destroy() or register a no-op error handler afterward. This
means stream resources are never explicitly released, and a late gRPC
error event after removeAllListeners() could crash the Node.js process
with an unhandled error.

Add stream.destroy() and a no-op error handler in both the 'end' and
'error' stream handlers, matching the established cleanup pattern already
used in task-hub-grpc-worker.ts.

Add 8 unit tests verifying stream destroy on success/error, late error
event safety, input validation, and error code handling.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 17, 2026 16:40

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

This PR addresses a resource-leak / unhandled-error risk in the core SDK client by ensuring TaskHubGrpcClient.getOrchestrationHistory() properly tears down its gRPC history stream, aligning cleanup behavior with the existing worker stream cleanup pattern.

Changes:

  • Destroy the history stream and attach a terminal no-op "error" handler after removeAllListeners() on both "end" and "error" paths.
  • Add a focused Jest test suite covering cleanup on success, common gRPC error cases, and “late error” safety after cleanup.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/durabletask-js/src/client/client.ts Ensures history streams are fully cleaned up (no-op error handler + destroy) on end/error.
packages/durabletask-js/test/client-stream-cleanup.spec.ts Adds targeted tests validating stream teardown and late-error safety behaviors.

Comment on lines +51 to +54
const event = new pb.HistoryEvent();
event.setEventid(1);
event.setTimestamp(Timestamp.fromDate(new Date()));
const orchestratorStarted = new pb.OrchestratorStartedEvent();
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.

[copilot-finds] Bug: getOrchestrationHistory() does not destroy gRPC stream, risking resource leak and unhandled error crash

3 participants