Skip to content

Commit 586fa81

Browse files
committed
WIP to remove Swift6 concurrency errors
1 parent 5b76105 commit 586fa81

File tree

5 files changed

+30
-23
lines changed

5 files changed

+30
-23
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ let package = Package(
3636
.product(name: "NIOCore", package: "swift-nio"),
3737
.product(name: "NIOConcurrencyHelpers", package: "swift-nio"),
3838
.product(name: "NIOPosix", package: "swift-nio"),
39-
],
40-
swiftSettings: [.swiftLanguageMode(.v5)]
39+
]
40+
// swiftSettings: [.swiftLanguageMode(.v5)]
4141
),
4242
.plugin(
4343
name: "AWSLambdaPackager",

Sources/AWSLambdaRuntime/Lambda+Codable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ extension LambdaRuntime {
9191
public convenience init<Event: Decodable, Output>(
9292
decoder: JSONDecoder = JSONDecoder(),
9393
encoder: JSONEncoder = JSONEncoder(),
94-
body: sending @escaping (Event, LambdaContext) async throws -> Output
94+
body: @Sendable @escaping (Event, LambdaContext) async throws -> Output
9595
)
9696
where
9797
Handler == LambdaCodableAdapter<
@@ -116,7 +116,7 @@ extension LambdaRuntime {
116116
/// - Parameter decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default.
117117
public convenience init<Event: Decodable>(
118118
decoder: JSONDecoder = JSONDecoder(),
119-
body: sending @escaping (Event, LambdaContext) async throws -> Void
119+
body: @Sendable @escaping (Event, LambdaContext) async throws -> Void
120120
)
121121
where
122122
Handler == LambdaCodableAdapter<

Sources/AWSLambdaRuntimeCore/Lambda+Codable.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import NIOCore
1616

1717
/// The protocol a decoder must conform to so that it can be used with ``LambdaCodableAdapter`` to decode incoming
1818
/// `ByteBuffer` events.
19-
public protocol LambdaEventDecoder {
19+
public protocol LambdaEventDecoder: Sendable {
2020
/// Decode the `ByteBuffer` representing the received event into the generic `Event` type
2121
/// the handler will receive.
2222
/// - Parameters:
@@ -28,7 +28,7 @@ public protocol LambdaEventDecoder {
2828

2929
/// The protocol an encoder must conform to so that it can be used with ``LambdaCodableAdapter`` to encode the generic
3030
/// ``LambdaOutputEncoder/Output`` object into a `ByteBuffer`.
31-
public protocol LambdaOutputEncoder {
31+
public protocol LambdaOutputEncoder: Sendable {
3232
associatedtype Output
3333

3434
/// Encode the generic type `Output` the handler has returned into a `ByteBuffer`.
@@ -52,7 +52,7 @@ public struct LambdaHandlerAdapter<
5252
Event: Decodable,
5353
Output,
5454
Handler: LambdaHandler
55-
>: LambdaWithBackgroundProcessingHandler where Handler.Event == Event, Handler.Output == Output {
55+
>: Sendable, LambdaWithBackgroundProcessingHandler where Handler.Event == Event, Handler.Output == Output {
5656
@usableFromInline let handler: Handler
5757

5858
/// Initializes an instance given a concrete handler.
@@ -86,7 +86,7 @@ public struct LambdaCodableAdapter<
8686
Output,
8787
Decoder: LambdaEventDecoder,
8888
Encoder: LambdaOutputEncoder
89-
>: StreamingLambdaHandler where Handler.Event == Event, Handler.Output == Output, Encoder.Output == Output {
89+
>: Sendable, StreamingLambdaHandler where Handler.Event == Event, Handler.Output == Output, Encoder.Output == Output, Encoder: Sendable, Decoder: Sendable {
9090
@usableFromInline let handler: Handler
9191
@usableFromInline let encoder: Encoder
9292
@usableFromInline let decoder: Decoder
@@ -139,7 +139,7 @@ public struct LambdaCodableAdapter<
139139
/// A ``LambdaResponseStreamWriter`` wrapper that conforms to ``LambdaResponseWriter``.
140140
public struct LambdaCodableResponseWriter<Output, Encoder: LambdaOutputEncoder, Base: LambdaResponseStreamWriter>:
141141
LambdaResponseWriter
142-
where Output == Encoder.Output {
142+
where Output == Encoder.Output, Encoder: Sendable {
143143
@usableFromInline let underlyingStreamWriter: Base
144144
@usableFromInline let encoder: Encoder
145145

Sources/AWSLambdaRuntimeCore/LambdaHandlers.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import NIOCore
2020
/// Background work can also be executed after returning the response. After closing the response stream by calling
2121
/// ``LambdaResponseStreamWriter/finish()`` or ``LambdaResponseStreamWriter/writeAndFinish(_:)``,
2222
/// the ``handle(_:responseWriter:context:)`` function is free to execute any background work.
23-
public protocol StreamingLambdaHandler {
23+
public protocol StreamingLambdaHandler: Sendable {
2424
/// The handler function -- implement the business logic of the Lambda function here.
2525
/// - Parameters:
2626
/// - event: The invocation's input data.
@@ -45,7 +45,7 @@ public protocol StreamingLambdaHandler {
4545

4646
/// A writer object to write the Lambda response stream into. The HTTP response is started lazily.
4747
/// before the first call to ``write(_:)`` or ``writeAndFinish(_:)``.
48-
public protocol LambdaResponseStreamWriter {
48+
public protocol LambdaResponseStreamWriter: Sendable {
4949
/// Write a response part into the stream. Bytes written are streamed continually.
5050
/// - Parameter buffer: The buffer to write.
5151
func write(_ buffer: ByteBuffer) async throws
@@ -64,7 +64,7 @@ public protocol LambdaResponseStreamWriter {
6464
///
6565
/// - note: This handler protocol does not support response streaming because the output has to be encoded prior to it being sent, e.g. it is not possible to encode a partial/incomplete JSON string.
6666
/// This protocol also does not support the execution of background work after the response has been returned -- the ``LambdaWithBackgroundProcessingHandler`` protocol caters for such use-cases.
67-
public protocol LambdaHandler {
67+
public protocol LambdaHandler: Sendable {
6868
/// Generic input type.
6969
/// The body of the request sent to Lambda will be decoded into this type for the handler to consume.
7070
associatedtype Event: Decodable
@@ -86,7 +86,7 @@ public protocol LambdaHandler {
8686
/// ``LambdaResponseWriter``that is passed in as an argument, meaning that the
8787
/// ``LambdaWithBackgroundProcessingHandler/handle(_:outputWriter:context:)`` function is then
8888
/// free to implement any background work after the result has been sent to the AWS Lambda control plane.
89-
public protocol LambdaWithBackgroundProcessingHandler {
89+
public protocol LambdaWithBackgroundProcessingHandler: Sendable {
9090
/// Generic input type.
9191
/// The body of the request sent to Lambda will be decoded into this type for the handler to consume.
9292
associatedtype Event: Decodable
@@ -110,7 +110,7 @@ public protocol LambdaWithBackgroundProcessingHandler {
110110
/// Used with ``LambdaWithBackgroundProcessingHandler``.
111111
/// A mechanism to "return" an output from ``LambdaWithBackgroundProcessingHandler/handle(_:outputWriter:context:)`` without the function needing to
112112
/// have a return type and exit at that point. This allows for background work to be executed _after_ a response has been sent to the AWS Lambda response endpoint.
113-
public protocol LambdaResponseWriter<Output> {
113+
public protocol LambdaResponseWriter<Output>: Sendable {
114114
associatedtype Output
115115
/// Sends the generic ``LambdaResponseWriter/Output`` object (representing the computed result of the handler)
116116
/// to the AWS Lambda response endpoint.
@@ -150,17 +150,17 @@ public struct StreamingClosureHandler: StreamingLambdaHandler {
150150
/// A ``LambdaHandler`` conforming handler object that can be constructed with a closure.
151151
/// Allows for a handler to be defined in a clean manner, leveraging Swift's trailing closure syntax.
152152
public struct ClosureHandler<Event: Decodable, Output>: LambdaHandler {
153-
let body: (Event, LambdaContext) async throws -> Output
153+
let body: @Sendable (Event, LambdaContext) async throws -> Output
154154

155155
/// Initialize with a closure handler over generic `Input` and `Output` types.
156156
/// - Parameter body: The handler function written as a closure.
157-
public init(body: @escaping (Event, LambdaContext) async throws -> Output) where Output: Encodable {
157+
public init(body: @Sendable @escaping (Event, LambdaContext) async throws -> Output) where Output: Encodable {
158158
self.body = body
159159
}
160160

161161
/// Initialize with a closure handler over a generic `Input` type, and a `Void` `Output`.
162162
/// - Parameter body: The handler function written as a closure.
163-
public init(body: @escaping (Event, LambdaContext) async throws -> Void) where Output == Void {
163+
public init(body: @Sendable @escaping (Event, LambdaContext) async throws -> Void) where Output == Void {
164164
self.body = body
165165
}
166166

@@ -194,7 +194,7 @@ extension LambdaRuntime {
194194
>(
195195
encoder: Encoder,
196196
decoder: Decoder,
197-
body: sending @escaping (Event, LambdaContext) async throws -> Output
197+
body: @Sendable @escaping (Event, LambdaContext) async throws -> Output
198198
)
199199
where
200200
Handler == LambdaCodableAdapter<
@@ -220,7 +220,7 @@ extension LambdaRuntime {
220220
/// - body: The handler in the form of a closure.
221221
public convenience init<Event: Decodable, Decoder: LambdaEventDecoder>(
222222
decoder: Decoder,
223-
body: sending @escaping (Event, LambdaContext) async throws -> Void
223+
body: @Sendable @escaping (Event, LambdaContext) async throws -> Void
224224
)
225225
where
226226
Handler == LambdaCodableAdapter<

Sources/AWSLambdaRuntimeCore/LambdaRuntimeClient.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
16+
// TODO: rewrite for Swift 6 concurrency
17+
1518
import Logging
1619
import NIOCore
1720
import NIOHTTP1
@@ -140,6 +143,7 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
140143
}
141144
}
142145

146+
// FIXME: add support for graceful shutdown
143147
func nextInvocation() async throws -> (Invocation, Writer) {
144148
switch self.lambdaState {
145149
case .idle:
@@ -336,12 +340,15 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
336340
case .disconnected, .connected:
337341
fatalError("Unexpected state: \(self.connectionState)")
338342

339-
case .connecting(let array):
343+
// case .connecting(let array):
344+
case .connecting:
340345
self.connectionState = .connected(channel, handler)
341346
defer {
342-
for continuation in array {
343-
continuation.resume(returning: handler)
344-
}
347+
// for continuation in array {
348+
// // This causes an error in Swift 6
349+
// // 'self'-isolated 'handler' is passed as a 'sending' parameter; Uses in callee may race with later 'self'-isolated uses
350+
// continuation.resume(returning: handler)
351+
// }
345352
}
346353
return handler
347354
}

0 commit comments

Comments
 (0)