mirror of
https://github.com/apple/swift-nio
synced 2026-11-28 05:14:23 +00:00
Throw an appropriate error from the writer when the channel closed (#2744)
# Motivation Currently, when the channel closes and a user tries to write something the writer throws an `AlreadyFinished` error. This error can also be thrown when calling `finish` on the writer and then trying to call `write` again. This makes it hard to distinguish if the thrown error was due to the channel being closed or due to a business logic error in handling the writer. # Modification This PR finishes the writer with a `ChannelError.ioOnClosedChannel` if the writer gets finished to due a channel inactive or handler removed. # Result Users can now distinguish if they did something wrong with the writer or if the channel closed.
This commit is contained in:
1 parent
ea6a8db87a
commit
5d7a9997e6
2 files changed
+20
-2
No files matched your search
@@ -137,7 +137,7 @@ internal final class NIOAsyncChannelOutboundWriterHandler<OutboundOut: Sendable>
|
||||
@inlinable
|
||||
func handlerRemoved(context: ChannelHandlerContext) {
|
||||
self.context = nil
|
||||
self.sink?.finish()
|
||||
self.sink?.finish(error: ChannelError.ioOnClosedChannel)
|
||||
self.writer = nil
|
||||
}
|
||||
|
||||
@@ -150,7 +150,7 @@ internal final class NIOAsyncChannelOutboundWriterHandler<OutboundOut: Sendable>
|
||||
|
||||
@inlinable
|
||||
func channelInactive(context: ChannelHandlerContext) {
|
||||
self.sink?.finish()
|
||||
self.sink?.finish(error: ChannelError.ioOnClosedChannel)
|
||||
context.fireChannelInactive()
|
||||
}
|
||||
|
||||
|
||||
@@ -81,6 +81,24 @@ final class AsyncChannelTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testAsyncChannelThrowsWhenChannelClosed() async throws {
|
||||
let channel = NIOAsyncTestingChannel()
|
||||
let wrapped = try await channel.testingEventLoop.executeInContext {
|
||||
return try NIOAsyncChannel<String, String>(wrappingChannelSynchronously: channel)
|
||||
}
|
||||
|
||||
try await channel.close(mode: .all)
|
||||
|
||||
do {
|
||||
try await wrapped.executeThenClose { _, outbound in
|
||||
try await outbound.write("Test")
|
||||
}
|
||||
XCTFail("Expected an error to be thrown")
|
||||
} catch {
|
||||
XCTAssertEqual(error as? ChannelError, ChannelError.ioOnClosedChannel)
|
||||
}
|
||||
}
|
||||
|
||||
func testFinishingTheWriterClosesTheWriteSideOfTheChannel() async throws {
|
||||
let channel = NIOAsyncTestingChannel()
|
||||
let closeRecorder = CloseRecorder()
|
||||
|
||||
Reference in new issue
Block a user