Skip to content

Commit efb6e3f

Browse files
committed
Fix a few simple sendability issues
Motivation: We're about to go on a sendability journey. Let's pick some low hanging fruit to get started. Modifications: - Add a few assume-isolated calls - Stop using static var - Use a dispatch group instead of a work item to wait for work to be done. Result: Fewer warnings
1 parent efb08f9 commit efb6e3f

File tree

7 files changed

+17
-13
lines changed

7 files changed

+17
-13
lines changed

Sources/AsyncHTTPClient/ConnectionPool/ChannelHandler/HTTP1ProxyConnectHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ final class HTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableChannelHand
137137
return
138138
}
139139

140-
let timeout = context.eventLoop.scheduleTask(deadline: self.deadline) {
140+
let timeout = context.eventLoop.assumeIsolated().scheduleTask(deadline: self.deadline) {
141141
switch self.state {
142142
case .initialized:
143143
preconditionFailure("How can we have a scheduled timeout, if the connection is not even up?")

Sources/AsyncHTTPClient/ConnectionPool/ChannelHandler/SOCKSEventsHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ final class SOCKSEventsHandler: ChannelInboundHandler, RemovableChannelHandler {
9999
return
100100
}
101101

102-
let scheduled = context.eventLoop.scheduleTask(deadline: self.deadline) {
102+
let scheduled = context.eventLoop.assumeIsolated().scheduleTask(deadline: self.deadline) {
103103
switch self.state {
104104
case .initialized, .channelActive:
105105
// close the connection, if the handshake timed out

Sources/AsyncHTTPClient/ConnectionPool/ChannelHandler/TLSEventsHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ final class TLSEventsHandler: ChannelInboundHandler, RemovableChannelHandler {
104104

105105
var scheduled: Scheduled<Void>?
106106
if let deadline = deadline {
107-
scheduled = context.eventLoop.scheduleTask(deadline: deadline) {
107+
scheduled = context.eventLoop.assumeIsolated().scheduleTask(deadline: deadline) {
108108
switch self.state {
109109
case .initialized, .channelActive:
110110
// close the connection, if the handshake timed out

Sources/AsyncHTTPClient/ConnectionPool/State Machine/HTTPConnectionPool+StateMachine.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ extension HTTPConnectionPool {
2626
self.connection = connection
2727
}
2828

29-
static let none = Action(request: .none, connection: .none)
29+
static var none: Action {
30+
Action(request: .none, connection: .none)
31+
}
3032
}
3133

3234
enum ConnectionAction {
@@ -397,7 +399,9 @@ extension HTTPConnectionPool.StateMachine {
397399
}
398400

399401
struct EstablishedAction {
400-
static let none: Self = .init(request: .none, connection: .none)
402+
static var none: Self {
403+
Self(request: .none, connection: .none)
404+
}
401405
let request: HTTPConnectionPool.StateMachine.RequestAction
402406
let connection: EstablishedConnectionAction
403407
}

Sources/AsyncHTTPClient/HTTPClient+HTTPCookie.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ extension String.UTF8View.SubSequence {
216216
}
217217
}
218218

219-
private let posixLocale: UnsafeMutableRawPointer = {
219+
nonisolated(unsafe) private let posixLocale: UnsafeMutableRawPointer = {
220220
// All POSIX systems must provide a "POSIX" locale, and its date/time formats are US English.
221221
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html#tag_07_03_05
222222
let _posixLocale = newlocale(LC_TIME_MASK | LC_NUMERIC_MASK, "POSIX", nil)!

Sources/AsyncHTTPClient/HTTPClient.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,18 @@ public class HTTPClient {
223223
)
224224
}
225225
let errorStorage: NIOLockedValueBox<Error?> = NIOLockedValueBox(nil)
226-
let continuation = DispatchWorkItem {}
226+
let dispatchGroup = DispatchGroup()
227+
dispatchGroup.enter()
227228
self.shutdown(requiresCleanClose: requiresCleanClose, queue: DispatchQueue(label: "async-http-client.shutdown"))
228229
{ error in
229230
if let error = error {
230231
errorStorage.withLockedValue { errorStorage in
231232
errorStorage = error
232233
}
233234
}
234-
continuation.perform()
235+
dispatchGroup.leave()
235236
}
236-
continuation.wait()
237+
dispatchGroup.wait()
237238
try errorStorage.withLockedValue { errorStorage in
238239
if let error = errorStorage {
239240
throw error
@@ -756,14 +757,13 @@ public class HTTPClient {
756757
delegate: delegate
757758
)
758759

759-
var deadlineSchedule: Scheduled<Void>?
760760
if let deadline = deadline {
761-
deadlineSchedule = taskEL.scheduleTask(deadline: deadline) {
761+
let deadlineSchedule = taskEL.scheduleTask(deadline: deadline) {
762762
requestBag.deadlineExceeded()
763763
}
764764

765765
task.promise.futureResult.whenComplete { _ in
766-
deadlineSchedule?.cancel()
766+
deadlineSchedule.cancel()
767767
}
768768
}
769769

Sources/AsyncHTTPClient/NIOTransportServices/TLSConfiguration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ extension TLSVersion {
6060
@available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 5.0, *)
6161
extension TLSConfiguration {
6262
/// Dispatch queue used by Network framework TLS to control certificate verification
63-
static var tlsDispatchQueue = DispatchQueue(label: "TLSDispatch")
63+
static let tlsDispatchQueue = DispatchQueue(label: "TLSDispatch")
6464

6565
/// create NWProtocolTLS.Options for use with NIOTransportServices from the NIOSSL TLSConfiguration
6666
///

0 commit comments

Comments
 (0)