@@ -25,16 +25,12 @@ import Foundation
2525
2626// This is our guardian to ensure only one LambdaRuntime is running at the time
2727// We use an Atomic here to ensure thread safety
28- private let _isRunning = Atomic < Bool > ( false )
28+ fileprivate let _isRunning = Atomic < Bool > ( false )
2929
30- // We need `@unchecked` Sendable here, as `NIOLockedValueBox` does not understand `sending` today.
31- // We don't want to use `NIOLockedValueBox` here anyway. We would love to use Mutex here, but this
32- // sadly crashes the compiler today (on Linux).
33- // See https://github.com/swiftlang/swift/issues/80036
30+ // we use unchecked here because Handler are not Sendable
3431public final class LambdaRuntime < Handler> : @unchecked Sendable where Handler: StreamingLambdaHandler {
35- // TODO: We want to change this to Mutex as soon as this doesn't crash the Swift compiler on Linux anymore
3632 @usableFromInline
37- let handlerMutex : NIOLockedValueBox < Handler ? >
33+ let handler : Handler
3834 @usableFromInline
3935 let logger : Logger
4036 @usableFromInline
@@ -45,7 +41,7 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
4541 eventLoop: EventLoop = Lambda . defaultEventLoop,
4642 logger: Logger = Logger ( label: " LambdaRuntime " )
4743 ) {
48- self . handlerMutex = NIOLockedValueBox ( handler)
44+ self . handler = handler
4945 self . eventLoop = eventLoop
5046
5147 // by setting the log level here, we understand it can not be changed dynamically at runtime
@@ -77,23 +73,13 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
7773
7874 // if the original value was already true, run() is already running
7975 if original {
80- throw LambdaRuntimeError ( code: . moreThanOneLambdaRuntimeInstance )
76+ throw LambdaRuntimeError ( code: . runtimeCanOnlyBeStartedOnce )
8177 }
8278
8379 defer {
8480 _isRunning. store ( false , ordering: . releasing)
8581 }
8682
87- let handler = self . handlerMutex. withLockedValue { handler in
88- let result = handler
89- handler = nil
90- return result
91- }
92-
93- guard let handler else {
94- throw LambdaRuntimeError ( code: . runtimeCanOnlyBeStartedOnce)
95- }
96-
9783 // are we running inside an AWS Lambda runtime environment ?
9884 // AWS_LAMBDA_RUNTIME_API is set when running on Lambda
9985 // https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html
@@ -132,7 +118,7 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
132118 ) { runtimeClient in
133119 try await Lambda . runLoop (
134120 runtimeClient: runtimeClient,
135- handler: handler,
121+ handler: self . handler,
136122 logger: self . logger
137123 )
138124 }
0 commit comments