swift-aws-lambda-runtime icon indicating copy to clipboard operation
swift-aws-lambda-runtime copied to clipboard

Add support swift-service-lifecycle

Open fabianfett opened this issue 1 year ago • 8 comments

The code on the main branch currently does not support Swift Service Lifecycle.

fabianfett avatar Sep 27 '24 12:09 fabianfett

We started the implementation with these two PR

  • #459 : make LambdaRuntimeClient.nextInvocation() cancellable
  • #456 : Add LambdaRuntimService and an example function

These two PR have unresolved issues.

  • #459 is not Swift 6 compliant
/swift/lambda/swift-aws-lambda-runtime/Sources/AWSLambdaRuntimeCore/LambdaRuntimeClient.swift:343:38: warning: sending 'handler' risks causing data races; this is an error in the Swift 6 language mode
341 |                 defer {
342 |                     for continuation in array {
343 |                         continuation.resume(returning: handler)
    |                                      |- warning: sending 'handler' risks causing data races; this is an error in the Swift 6 language mode
    |                                      `- note: 'self'-isolated 'handler' is passed as a 'sending' parameter; Uses in callee may race with later 'self'-isolated uses
344 |                     }
345 |                 }
  • #456 adds partial support for graceful shutdown. But the graceful shutdown flag is not propagated down to the LambdaRuntimeClient. Currently, this call cannot be interrupted when a graceful shutdown is requested.

sebsto avatar Jan 12 '25 16:01 sebsto

Do we want to use package traits here?

https://github.com/swiftlang/swift-evolution/blob/main/proposals/0450-swiftpm-package-traits.md

cc @FranzBusch

fabianfett avatar Feb 20 '25 13:02 fabianfett

I think we landed this with #490. @sebsto close this one?

fabianfett avatar Mar 06 '25 15:03 fabianfett

A big part has been landed yes. Not all of it. I’ll keep this for the example and the actual structure that implements the Service protocol

sebsto avatar Mar 06 '25 15:03 sebsto

The missing part is graceful shutdown support?

fabianfett avatar Mar 06 '25 15:03 fabianfett

We need to add

  • an example
  • create an extension of LambdaRuntime that implements Service protocol
  • and check what happens in case of graceful shutdown.

Points 1. and 2. are in the PR I closed and I will submit a separate PR for these.

IMHO, when the LambdaRuntimeClient waits on /next, the only way to stop it is to cancel the Tasks. Therefore, graceful shutdown is very similar to task cancellation.

Ideally, we must be sure than any in flight user handler has the time to return a response. But the Lambda service will take care to re-create a runtime environment if one fails to return a response or error. So in this specific context, it is not that important to cleanly shutdown.

sebsto avatar Mar 06 '25 16:03 sebsto

create an extension of LambdaRuntime that implements Service protocol

https://github.com/swift-server/swift-aws-lambda-runtime/blob/bf02bcec9a22c060234ee9e417d36519eda6a572/Sources/AWSLambdaRuntime/LambdaRuntime%2BServiceLifecycle.swift#L18

fabianfett avatar Mar 06 '25 16:03 fabianfett

Excellent I missed it. Thank you

sebsto avatar Mar 06 '25 16:03 sebsto

Complete

sebsto avatar Jul 30 '25 02:07 sebsto