Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Add cancellation handling for nextInvocation() #459

Merged
merged 7 commits into from
Feb 27, 2025

Conversation

adam-fowler
Copy link
Member

@adam-fowler adam-fowler commented Jan 12, 2025

Allow LambdaChannelHandler.nextInvocation to be cancelled.

Motivation:

If we want to use ServiceLifecycle with the lambda runtime the lambda runtime needs to be cancellable either via a ServiceLifecycle graceful shutdown or via Task cancellation. To avoid bringing in the ServiceLifecycle dependency this PR adds cancellation via Task cancellation handler.

Modifications:

Add withTaskCancellationHandler to nextInvocation which calls close on cancel.
In LambdaChannelHandler.channelInactive resume continuation if state is waitingForNextInvocation
Added LambdaRuntimeClientTests.testCancellation

Result:

You can now cancel the runtime while it is waiting for the next invocation.

@sebsto sebsto added the semver/none No version bump required. label Jan 12, 2025
@sebsto sebsto changed the title Add cancellation handling for nextInvocation() [core] Add cancellation handling for nextInvocation() Jan 14, 2025
@sebsto sebsto added this to the 2.0 milestone Jan 16, 2025
@adam-fowler
Copy link
Member Author

This PR does not compile in swift 6 mode and I don't see an easy way around the issues.

Copy link
Member

@fabianfett fabianfett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My goodness. two comments have lingered here forever in pending. Just flushing those right now

@adam-fowler adam-fowler force-pushed the cancel-next-invocation branch from 546acc9 to 467d012 Compare February 21, 2025 08:19
@adam-fowler
Copy link
Member Author

Re-wrote this. It is considerably simpler. Also fixed test to ensure it does check for cancellation

}
// wait a small amount to ensure we are waiting for continuation
try await Task.sleep(for: .milliseconds(100))
group.cancelAll()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should check that the spawned task returns with a cancellation error indeed!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't return cancellation. Currently returns isInClosedChannel. Not sure how to get it to return the cancellation error as I'm just closing the connection and waiting for it to clean up. I could catch the error and then check Task.isCancelled and throw a Cancellation error if it is cancelled but that seems a bit of a hack.

Copy link
Member

@fabianfett fabianfett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really good! Mind adding the test check.

@sebsto
Copy link
Contributor

sebsto commented Feb 26, 2025

This looks really good! Mind adding the test check.

@adam-fowler ?

@adam-fowler
Copy link
Member Author

Currently the test check will fail. There isn't an easy way to indicate the connection was closed due to cancellation without some re-structuring.

@sebsto
Copy link
Contributor

sebsto commented Feb 27, 2025

Looks like we're good to merge then. Thank you for having confirmed

@fabianfett fabianfett merged commit 61cd5d5 into main Feb 27, 2025
31 checks passed
@fabianfett fabianfett deleted the cancel-next-invocation branch February 27, 2025 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/none No version bump required.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants