Skip to content

Commit b40e642

Browse files
committed
Fix Xcode 14 hang risk warnings
1 parent d6bd6ff commit b40e642

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

templates/swift/Sources/Services/Service.swift.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ open class {{ service.name | caseUcfirst }}: Service {
3131
{%~ if 'multipart/form-data' in method.consumes %}
3232
onProgress: ((UploadProgress) -> Void)? = nil
3333
{%~ endif %}
34-
) async throws -> {{ method | returnType(spec) | raw }} {
34+
){%~ if not method.type == "webAuth" %} async{% endif %} throws -> {{ method | returnType(spec) | raw }} {
3535
{{~ include('swift/base/params.twig') }}
3636
{%~ if method.type == 'webAuth' %}
3737
{{~ include('swift/base/requests/OAuth.twig') }}
@@ -109,4 +109,4 @@ open class {{ service.name | caseUcfirst }}: Service {
109109

110110
{% endfor %}
111111

112-
}
112+
%}

templates/swift/Sources/WebSockets/WebSocketClient.swift.twig

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ public class WebSocketClient {
2626
var channel: Channel? = nil
2727
var tlsEnabled: Bool = false
2828
var closeSent: Bool = false
29-
30-
let upgradedSignalled = DispatchSemaphore(value: 0)
31-
let locker = DispatchQueue(label: WEBSOCKET_LOCKER_QUEUE)
32-
let threadGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
29+
30+
let locker = DispatchQueue(label: WEBSOCKET_LOCKER_QUEUE, qos: .background)
31+
32+
var threadGroup: MultiThreadedEventLoopGroup? = nil
3333

3434
weak var delegate: WebSocketClientDelegate? = nil
3535

@@ -184,6 +184,10 @@ public class WebSocketClient {
184184
self.maxFrameSize = maxFrameSize
185185
self.tlsEnabled = tlsEnabled
186186
self.delegate = delegate
187+
188+
DispatchQueue.global(qos: .background).async {
189+
self.threadGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
190+
}
187191
}
188192

189193
/// Create a new `WebSocketClient`.
@@ -207,10 +211,14 @@ public class WebSocketClient {
207211
self.maxFrameSize = 24
208212
self.tlsEnabled = tlsEnabled
209213
self.delegate = delegate
214+
215+
DispatchQueue.global(qos: .background).async {
216+
self.threadGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
217+
}
210218
}
211219

212220
deinit {
213-
try! threadGroup.syncShutdownGracefully()
221+
try! threadGroup!.syncShutdownGracefully()
214222
}
215223

216224
// MARK: - Open connection
@@ -221,16 +229,16 @@ public class WebSocketClient {
221229
SocketOptionLevel(SOL_SOCKET),
222230
SO_REUSEPORT
223231
)
232+
233+
while(threadGroup == nil) {}
224234

225-
let bootstrap = ClientBootstrap(group: threadGroup)
235+
let bootstrap = ClientBootstrap(group: threadGroup!)
226236
.channelOption(socketOptions, value: 1)
227237
.channelInitializer(self.openChannel)
228238

229239
_ = try bootstrap
230240
.connect(host: self.host, port: self.port)
231241
.wait()
232-
233-
self.upgradedSignalled.wait()
234242
}
235243

236244
private func openChannel(channel: Channel) -> EventLoopFuture<Void> {
@@ -260,13 +268,10 @@ public class WebSocketClient {
260268
}
261269

262270
private func upgradePipelineHandler(channel: Channel, response: HTTPResponseHead) -> EventLoopFuture<Void> {
263-
self.onOpen(channel)
264-
265271
let handler = MessageHandler(client: self)
266272

267273
if response.status == .switchingProtocols {
268274
self.channel = channel
269-
self.upgradedSignalled.signal()
270275
}
271276

272277
return channel.pipeline.addHandler(handler)

0 commit comments

Comments
 (0)