Skip to content

Throw an appropriate error from the writer when the channel closed #2744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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()
}

Expand Down
18 changes: 18 additions & 0 deletions Tests/NIOCoreTests/AsyncChannel/AsyncChannelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down