Expose AbortSignal to handlers for cooperative cancellation#8
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces handler cancellation support via
AbortSignalfor 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
AsyncLocalStorage-based context (handlerContext) to inject anAbortSignalinto the handler execution context, accessible via the newgetAbortSignal()helper. This enables handler cancellation on timeout, allowing integration with cancellable APIs likefetchorundici. (src/context.ts[1]src/index.ts[2] [3]handlerOptions.executionTimeoutis set, the handler is run with an associatedAbortControllersignal. If the timeout triggers, the signal is aborted and the handler is promptly cancelled. (src/index.tssrc/index.tsR144-R169)Documentation updates
README.mdto document the new handler cancellation feature, including usage instructions forgetAbortSignal(), and added it to the API reference. (README.md[1] [2]Testing improvements
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]test/test/error.tstest/test/error.tsR263-R319)Dependency updates
@fgiova/mini-sqs-clientdependency from v4 to v5 for compatibility and improvements. (package.jsonpackage.jsonL59-R59)Test setup improvements
dotenvpackage directly for loading environment variables. (test/helpers/localtest.tstest/helpers/localtest.tsR4-R10)