Skip to content

feat(worker): add notify-termination and cancellation-token utilities - #449

Merged
WinterYukky merged 1 commit into
aws-samples:mainfrom
WinterYukky:feature/notify-termination-1785859334
Aug 24, 2026
Merged

feat(worker): add notify-termination and cancellation-token utilities#449
WinterYukky merged 1 commit into
aws-samples:mainfrom
WinterYukky:feature/notify-termination-1785859334

Conversation

@WinterYukky

Copy link
Copy Markdown
Contributor

Summary

Add two complementary session lifecycle primitives for cooperative shutdown and cancellation signaling.

Motivation

Long-running agent sessions need reliable mechanisms to:

  1. Signal cancellation to in-flight operations (subprocess teardown, streaming abort) without waiting for the next polling interval
  2. Perform graceful shutdown when the orchestrator requests termination — draining in-flight work before process exit with a configurable timeout and force-kill fallback

These primitives enable cleaner session lifecycle management and reduce orphaned subprocess accumulation.

Changes

  • CancellationToken: cooperative cancellation signal with synchronous listener support. Listeners fire immediately on cancel(), enabling instant subprocess teardown without polling.
  • notifyTermination: graceful shutdown handler that registers SIGTERM/SIGINT handlers, drains registered cleanup functions with a configurable timeout, and force-exits if cleanup exceeds the deadline.

Files changed (4 files, +449):

  • packages/worker/src/common/cancellation-token.ts
  • packages/worker/src/common/cancellation-token.test.ts
  • packages/worker/src/common/notify-termination.ts
  • packages/worker/src/common/notify-termination.test.ts

Testing

  • 21 unit tests (8 cancellation-token + 13 notify-termination) — all passing
  • TypeScript compilation passes
  • Prettier format check passes

Notes

Self-contained. No dependencies on other PRs.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Add two complementary session lifecycle primitives:
- CancellationToken: a cooperative cancellation signal with synchronous listener
  support for immediate subprocess teardown
- notifyTermination: graceful shutdown handler that drains in-flight operations
  before process exit, with configurable timeout and force-kill fallback

Both utilities include comprehensive unit tests.
@WinterYukky
WinterYukky merged commit e54bd6a into aws-samples:main Aug 24, 2026
8 checks passed
@WinterYukky
WinterYukky deleted the feature/notify-termination-1785859334 branch August 24, 2026 05:43
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.

1 participant