Skip to content

Expose AbortSignal to handlers for cooperative cancellation#8

Merged
fgiova merged 2 commits into
mainfrom
feat/abort-signal
May 7, 2026
Merged

Expose AbortSignal to handlers for cooperative cancellation#8
fgiova merged 2 commits into
mainfrom
feat/abort-signal

Conversation

@fgiova
Copy link
Copy Markdown
Owner

@fgiova fgiova commented May 7, 2026

This pull request introduces handler cancellation support via AbortSignal for the SQS consumer, allowing handlers to be aborted when they exceed their execution timeout. It also updates the documentation and adds comprehensive tests to ensure correct behavior and prevent unhandled promise rejections after timeouts. Additionally, it updates a dependency to the latest major version.

Handler cancellation and context propagation

  • Introduced AsyncLocalStorage-based context (handlerContext) to inject an AbortSignal into the handler execution context, accessible via the new getAbortSignal() helper. This enables handler cancellation on timeout, allowing integration with cancellable APIs like fetch or undici. (src/context.ts [1] src/index.ts [2] [3]
  • When handlerOptions.executionTimeout is set, the handler is run with an associated AbortController signal. If the timeout triggers, the signal is aborted and the handler is promptly cancelled. (src/index.ts src/index.tsR144-R169)

Documentation updates

  • Updated README.md to document the new handler cancellation feature, including usage instructions for getAbortSignal(), and added it to the API reference. (README.md [1] [2]

Testing improvements

  • Added a new test suite for handler context and cancellation, verifying that getAbortSignal() behaves as expected inside and outside handler contexts and that the signal aborts on timeout. (test/test/context.ts [1] test/scripts/runners/localstack.js [2]
  • Extended error handling tests to ensure that late handler rejections after a timeout do not result in unhandled promise rejections. (test/test/error.ts test/test/error.tsR263-R319)

Dependency updates

  • Bumped @fgiova/mini-sqs-client dependency from v4 to v5 for compatibility and improvements. (package.json package.jsonL59-R59)

Test setup improvements

Wrap handler execution in an AsyncLocalStorage context carrying an
AbortController.signal so handlers can opt into cooperative cancellation
without threading the signal through their call sites. The signal is
aborted (with a TimeoutError as reason) when executionTimeout fires.

- src/context.ts: AsyncLocalStorage-backed handler context and
  getAbortSignal() accessor.
- src/index.ts: run handler inside handlerContext.run(); abort the
  controller on timeout; re-export getAbortSignal.
- README: document the new API and usage pattern.
- test/test/context.ts: cover undefined-outside-context, signal
  available inside handler, and abort-on-timeout (reason is
  TimeoutError). Adds test-queue-context to the localstack bootstrap.
@fgiova fgiova force-pushed the feat/abort-signal branch from 21df1bf to 231d95d Compare May 7, 2026 21:23
@fgiova fgiova force-pushed the feat/abort-signal branch from 231d95d to f3e2d05 Compare May 7, 2026 21:25
@fgiova fgiova merged commit f69be41 into main May 7, 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.

1 participant