Properly propagate errors in NetworkTransport receive stream (#4)
Changes AsyncStream to AsyncThrowingStream to ensure network errors are correctly propagated to callers. Updates receive() to handle errors from the underlying message stream.
This commit is contained in:
parent
1f5f8d64b9
commit
b761d3ea6f
1 changed files with 12 additions and 7 deletions
|
|
@ -180,8 +180,8 @@ public actor StdioTransport: Transport {
|
||||||
public nonisolated let logger: Logger
|
public nonisolated let logger: Logger
|
||||||
|
|
||||||
private var isConnected = false
|
private var isConnected = false
|
||||||
private let messageStream: AsyncStream<String>
|
private let messageStream: AsyncThrowingStream<String, Swift.Error>
|
||||||
private let messageContinuation: AsyncStream<String>.Continuation
|
private let messageContinuation: AsyncThrowingStream<String, Swift.Error>.Continuation
|
||||||
|
|
||||||
// Track connection state for continuations
|
// Track connection state for continuations
|
||||||
private var connectionContinuationResumed = false
|
private var connectionContinuationResumed = false
|
||||||
|
|
@ -196,8 +196,8 @@ public actor StdioTransport: Transport {
|
||||||
)
|
)
|
||||||
|
|
||||||
// Create message stream
|
// Create message stream
|
||||||
var continuation: AsyncStream<String>.Continuation!
|
var continuation: AsyncThrowingStream<String, Swift.Error>.Continuation!
|
||||||
self.messageStream = AsyncStream { continuation = $0 }
|
self.messageStream = AsyncThrowingStream { continuation = $0 }
|
||||||
self.messageContinuation = continuation
|
self.messageContinuation = continuation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -330,10 +330,14 @@ public actor StdioTransport: Transport {
|
||||||
public func receive() -> AsyncThrowingStream<String, Swift.Error> {
|
public func receive() -> AsyncThrowingStream<String, Swift.Error> {
|
||||||
return AsyncThrowingStream { continuation in
|
return AsyncThrowingStream { continuation in
|
||||||
Task {
|
Task {
|
||||||
for await message in messageStream {
|
do {
|
||||||
continuation.yield(message)
|
for try await message in messageStream {
|
||||||
|
continuation.yield(message)
|
||||||
|
}
|
||||||
|
continuation.finish()
|
||||||
|
} catch {
|
||||||
|
continuation.finish(throwing: error)
|
||||||
}
|
}
|
||||||
continuation.finish()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -361,6 +365,7 @@ public actor StdioTransport: Transport {
|
||||||
} catch {
|
} catch {
|
||||||
if !Task.isCancelled {
|
if !Task.isCancelled {
|
||||||
logger.error("Receive error: \(error)")
|
logger.error("Receive error: \(error)")
|
||||||
|
messageContinuation.finish(throwing: error)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue