Skip to content

Commit 4fe73ff

Browse files
committed
remove Sendable on OpenAPILambda + fix compilattion error on 6.2
1 parent 41fbf82 commit 4fe73ff

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

Sources/OpenAPILambda.swift

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import OpenAPIRuntime
1919
import HTTPTypes
2020

2121
/// A Lambda function implemented with a OpenAPI server (implementing `APIProtocol` from Swift OpenAPIRuntime)
22-
public protocol OpenAPILambda: Sendable {
22+
public protocol OpenAPILambda {
2323

2424
associatedtype Event: Decodable
2525
associatedtype Output: Encodable
@@ -53,7 +53,28 @@ extension OpenAPILambda {
5353
public static func run(logger: Logger? = nil) async throws {
5454

5555
let _logger = logger ?? Logger(label: "OpenAPILambda")
56-
let lambdaRuntime = try LambdaRuntime(logger: _logger, body: Self.handler())
56+
#if swift(>=6.2)
57+
let box = UnsafeTransferBox(value: try Self.handler())
58+
let lambdaRuntime = LambdaRuntime(logger: _logger, body: box.value)
59+
#else
60+
let lambdaHandler = try Self.handler()
61+
let lambdaRuntime = LambdaRuntime(logger: _logger, body: lambdaHandler)
62+
#endif
5763
try await lambdaRuntime.run()
5864
}
5965
}
66+
67+
// on Swift 6.2, with approachable concurrency, the compiler considers
68+
// the `lambdaHandler` can not be sent to the `LambdaRuntime(body:)` directly
69+
// despite the fact `lambdaHandler` is not used after that
70+
// There are two workarounds:
71+
// - make `OpenAPILambda` conform to `Sendable`. But this would require users to ensure their implementations are also `Sendable`
72+
// - wrap the handler in a `UnsafeTransferBox`
73+
#if swift(>=6.2)
74+
fileprivate struct UnsafeTransferBox<Value>: @unchecked Sendable {
75+
let value: Value
76+
init(value: Value) {
77+
self.value = value
78+
}
79+
}
80+
#endif

0 commit comments

Comments
 (0)