fix task group

This commit is contained in:
anlicheng 2025-08-03 12:26:54 +08:00
parent 2d6adba8a8
commit 6cb8641869
2 changed files with 21 additions and 13 deletions

View File

@ -63,13 +63,14 @@ actor SDLSuperClient {
group.addTask {
try await self.asyncChannel.channel.closeFuture.get()
NSLog("[SDLSuperClient] socket closed")
self.writeContinuation.finish()
self.inboundContinuation.finish()
throw SDLError.socketClosed
}
group.addTask {
defer {
self.inboundContinuation.finish()
}
for try await var packet in inbound {
if let message = SDLSuperClientDecoder.decode(buffer: &packet) {
SDLLogger.log("[SDLSuperTransport] read message: \(message)", level: .warning)
@ -87,6 +88,10 @@ actor SDLSuperClient {
}
group.addTask {
defer {
self.writeContinuation.finish()
}
for try await message in self.writeStream {
var buffer = self.asyncChannel.channel.allocator.buffer(capacity: message.data.count + 5)
buffer.writeInteger(message.packetId, as: UInt32.self)
@ -100,22 +105,21 @@ actor SDLSuperClient {
// --MARK:
group.addTask {
while true {
do {
try Task.checkCancellation()
//await self.ping()
try Task.checkCancellation()
await self.ping()
try await Task.sleep(nanoseconds: 5 * 1_000_000_000)
try Task.checkCancellation()
} catch let err {
NSLog("[SDLSuperClient] heartbeat cancelled")
throw err
NSLog("[SDLSuperClient] heartbeat cancelled with error: \(err)")
break
}
}
}
try await group.waitForAll()
// 退,
for try await _ in group {
}
NSLog("[SDLSuperClient] group closed")
}
}

View File

@ -100,7 +100,11 @@ actor SDLUDPHole {
}
}
try await group.waitForAll()
for try await _ in group {
}
SDLLogger.log("[SDLUDPHole] group closed", level: .debug)
}
}
}