From 37ffc4b8d575d9e0d560c7410d764b4e6ea578bb Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Tue, 27 Jan 2026 12:06:55 +0000 Subject: [PATCH] Add API to enable throwing in `EmbeddedChannel.getOption` and `.setOption` if channel is closed (#3495) ### Motivation: Channels based on `BaseSocketChannel` throw in both `getOption` and `setOption` if the channel has been closed, since the `setsockopt` will fail. However, the current behavior of `EmbeddedChannel` is options remain writable and readable on closed channels. There are situations where we'd like to be able to model the runtime behaviour of the real channel in tests, e.g. to test this fix: https://github.com/apple/swift-nio-extras/pull/304. ### Modifications: - Add API to enable throwing in `EmbeddedChannel.getOption` and `.setOption` if channel is closed. - Add a test for this new behavior. ### Result: - New API to enable throwing in `EmbeddedChannel.getOption` and `.setOption` if channel is closed. - No observable change for existing users of `EmbeddedChannel`. --- .../tests_04_performance/Thresholds/6.0.json | 6 +-- .../tests_04_performance/Thresholds/6.1.json | 6 +-- .../tests_04_performance/Thresholds/6.2.json | 6 +-- .../Thresholds/nightly-main.json | 6 +-- .../Thresholds/nightly-next.json | 6 +-- Sources/NIOEmbedded/Embedded.swift | 46 ++++++++++++++++--- .../EmbeddedChannelTest.swift | 13 ++++++ 7 files changed, 67 insertions(+), 22 deletions(-) diff --git a/IntegrationTests/tests_04_performance/Thresholds/6.0.json b/IntegrationTests/tests_04_performance/Thresholds/6.0.json index df7495108..0c9a265b6 100644 --- a/IntegrationTests/tests_04_performance/Thresholds/6.0.json +++ b/IntegrationTests/tests_04_performance/Thresholds/6.0.json @@ -1,7 +1,7 @@ { "1_reqs_1000_conn": 375050, - "1000_addHandlers_sync": 38050, - "1000_addHandlers": 45050, + "1000_addHandlers_sync": 39050, + "1000_addHandlers": 46050, "1000_addRemoveHandlers_handlercontext": 8050, "1000_addRemoveHandlers_handlername": 8050, "1000_addRemoveHandlers_handlertype": 8050, @@ -9,7 +9,7 @@ "1000_autoReadGetAndSet": 18050, "1000_copying_bytebufferview_to_array": 1050, "1000_copying_circularbuffer_to_array": 1050, - "1000_getHandlers_sync": 36, + "1000_getHandlers_sync": 37, "1000_getHandlers": 8050, "1000_reqs_1_conn": 26350, "1000_rst_connections": 138050, diff --git a/IntegrationTests/tests_04_performance/Thresholds/6.1.json b/IntegrationTests/tests_04_performance/Thresholds/6.1.json index df7495108..0c9a265b6 100644 --- a/IntegrationTests/tests_04_performance/Thresholds/6.1.json +++ b/IntegrationTests/tests_04_performance/Thresholds/6.1.json @@ -1,7 +1,7 @@ { "1_reqs_1000_conn": 375050, - "1000_addHandlers_sync": 38050, - "1000_addHandlers": 45050, + "1000_addHandlers_sync": 39050, + "1000_addHandlers": 46050, "1000_addRemoveHandlers_handlercontext": 8050, "1000_addRemoveHandlers_handlername": 8050, "1000_addRemoveHandlers_handlertype": 8050, @@ -9,7 +9,7 @@ "1000_autoReadGetAndSet": 18050, "1000_copying_bytebufferview_to_array": 1050, "1000_copying_circularbuffer_to_array": 1050, - "1000_getHandlers_sync": 36, + "1000_getHandlers_sync": 37, "1000_getHandlers": 8050, "1000_reqs_1_conn": 26350, "1000_rst_connections": 138050, diff --git a/IntegrationTests/tests_04_performance/Thresholds/6.2.json b/IntegrationTests/tests_04_performance/Thresholds/6.2.json index e44742bd9..f49ffc818 100644 --- a/IntegrationTests/tests_04_performance/Thresholds/6.2.json +++ b/IntegrationTests/tests_04_performance/Thresholds/6.2.json @@ -1,7 +1,7 @@ { "1_reqs_1000_conn": 375050, - "1000_addHandlers_sync": 38050, - "1000_addHandlers": 45050, + "1000_addHandlers_sync": 39050, + "1000_addHandlers": 46050, "1000_addRemoveHandlers_handlercontext": 8050, "1000_addRemoveHandlers_handlername": 8050, "1000_addRemoveHandlers_handlertype": 8050, @@ -9,7 +9,7 @@ "1000_autoReadGetAndSet": 18050, "1000_copying_bytebufferview_to_array": 1050, "1000_copying_circularbuffer_to_array": 1050, - "1000_getHandlers_sync": 36, + "1000_getHandlers_sync": 37, "1000_getHandlers": 8050, "1000_reqs_1_conn": 26350, "1000_rst_connections": 138050, diff --git a/IntegrationTests/tests_04_performance/Thresholds/nightly-main.json b/IntegrationTests/tests_04_performance/Thresholds/nightly-main.json index 4116b6010..1abccc744 100644 --- a/IntegrationTests/tests_04_performance/Thresholds/nightly-main.json +++ b/IntegrationTests/tests_04_performance/Thresholds/nightly-main.json @@ -1,7 +1,7 @@ { "1_reqs_1000_conn": 372050, - "1000_addHandlers_sync": 38050, - "1000_addHandlers": 45050, + "1000_addHandlers_sync": 39050, + "1000_addHandlers": 46050, "1000_addRemoveHandlers_handlercontext": 8050, "1000_addRemoveHandlers_handlername": 8050, "1000_addRemoveHandlers_handlertype": 8050, @@ -9,7 +9,7 @@ "1000_autoReadGetAndSet": 18050, "1000_copying_bytebufferview_to_array": 1050, "1000_copying_circularbuffer_to_array": 1050, - "1000_getHandlers_sync": 36, + "1000_getHandlers_sync": 37, "1000_getHandlers": 8050, "1000_reqs_1_conn": 26350, "1000_rst_connections": 136050, diff --git a/IntegrationTests/tests_04_performance/Thresholds/nightly-next.json b/IntegrationTests/tests_04_performance/Thresholds/nightly-next.json index e44742bd9..f49ffc818 100644 --- a/IntegrationTests/tests_04_performance/Thresholds/nightly-next.json +++ b/IntegrationTests/tests_04_performance/Thresholds/nightly-next.json @@ -1,7 +1,7 @@ { "1_reqs_1000_conn": 375050, - "1000_addHandlers_sync": 38050, - "1000_addHandlers": 45050, + "1000_addHandlers_sync": 39050, + "1000_addHandlers": 46050, "1000_addRemoveHandlers_handlercontext": 8050, "1000_addRemoveHandlers_handlername": 8050, "1000_addRemoveHandlers_handlertype": 8050, @@ -9,7 +9,7 @@ "1000_autoReadGetAndSet": 18050, "1000_copying_bytebufferview_to_array": 1050, "1000_copying_circularbuffer_to_array": 1050, - "1000_getHandlers_sync": 36, + "1000_getHandlers_sync": 37, "1000_getHandlers": 8050, "1000_reqs_1_conn": 26350, "1000_rst_connections": 138050, diff --git a/Sources/NIOEmbedded/Embedded.swift b/Sources/NIOEmbedded/Embedded.swift index 826514a94..719e82d22 100644 --- a/Sources/NIOEmbedded/Embedded.swift +++ b/Sources/NIOEmbedded/Embedded.swift @@ -456,9 +456,19 @@ class EmbeddedChannelCore: ChannelCore { } } + var allowOptionsWhenClosed: Bool { + get { + self._allowOptionsWhenClosed.load(ordering: .sequentiallyConsistent) + } + set { + self._allowOptionsWhenClosed.store(newValue, ordering: .sequentiallyConsistent) + } + } + private let _isOpen = ManagedAtomic(true) private let _isActive = ManagedAtomic(false) private let _allowRemoteHalfClosure = ManagedAtomic(false) + private let _allowOptionsWhenClosed = ManagedAtomic(true) let eventLoop: EventLoop let closePromise: EventLoopPromise @@ -825,6 +835,9 @@ public final class EmbeddedChannel: Channel { /// - Note: An `EmbeddedChannel` starts _inactive_ and can be activated, for example by calling `connect`. public var isActive: Bool { channelcore.isActive } + @usableFromInline + internal var isOpen: Bool { channelcore.isOpen } + /// - see: `ChannelOptions.Types.AllowRemoteHalfClosureOption` public var allowRemoteHalfClosure: Bool { get { @@ -837,6 +850,17 @@ public final class EmbeddedChannel: Channel { } } + public var allowOptionsWhenClosed: Bool { + get { + self.embeddedEventLoop.checkCorrectThread() + return channelcore.allowOptionsWhenClosed + } + set { + self.embeddedEventLoop.checkCorrectThread() + channelcore.allowOptionsWhenClosed = newValue + } + } + /// - see: `Channel.closeFuture` public var closeFuture: EventLoopFuture { channelcore.closePromise.futureResult } @@ -1089,14 +1113,17 @@ public final class EmbeddedChannel: Channel { @inlinable public func setOption(_ option: Option, value: Option.Value) -> EventLoopFuture { self.embeddedEventLoop.checkCorrectThread() - self.setOptionSync(option, value: value) - return self.eventLoop.makeSucceededVoidFuture() + return self.eventLoop.makeCompletedFuture { try self.setOptionSync(option, value: value) } } @inlinable - internal func setOptionSync(_ option: Option, value: Option.Value) { + internal func setOptionSync(_ option: Option, value: Option.Value) throws { self.embeddedEventLoop.checkCorrectThread() + guard self.isOpen || self.allowOptionsWhenClosed else { + throw ChannelError.alreadyClosed + } + self.addOption(option, value: value) if option is ChannelOptions.Types.AllowRemoteHalfClosureOption { @@ -1109,12 +1136,17 @@ public final class EmbeddedChannel: Channel { @inlinable public func getOption(_ option: Option) -> EventLoopFuture { self.embeddedEventLoop.checkCorrectThread() - return self.eventLoop.makeSucceededFuture(self.getOptionSync(option)) + return self.eventLoop.makeCompletedFuture { try self.getOptionSync(option) } } @inlinable - internal func getOptionSync(_ option: Option) -> Option.Value { + internal func getOptionSync(_ option: Option) throws -> Option.Value { self.embeddedEventLoop.checkCorrectThread() + + guard self.isOpen || self.allowOptionsWhenClosed else { + throw ChannelError.alreadyClosed + } + if option is ChannelOptions.Types.AutoReadOption { return true as! Option.Value } @@ -1231,12 +1263,12 @@ extension EmbeddedChannel { @inlinable public func setOption(_ option: Option, value: Option.Value) throws { - self.channel.setOptionSync(option, value: value) + try self.channel.setOptionSync(option, value: value) } @inlinable public func getOption(_ option: Option) throws -> Option.Value { - self.channel.getOptionSync(option) + try self.channel.getOptionSync(option) } } diff --git a/Tests/NIOEmbeddedTests/EmbeddedChannelTest.swift b/Tests/NIOEmbeddedTests/EmbeddedChannelTest.swift index 36a79c2f0..c3ea10c11 100644 --- a/Tests/NIOEmbeddedTests/EmbeddedChannelTest.swift +++ b/Tests/NIOEmbeddedTests/EmbeddedChannelTest.swift @@ -836,4 +836,17 @@ class EmbeddedChannelTest: XCTestCase { let optionValue2 = try channel.getOption(option).wait() XCTAssertEqual(2, optionValue2) } + + func testGetSetOptionOptInThrowing() { + let channel = EmbeddedChannel() + XCTAssertNoThrow(try channel.close().wait()) + + XCTAssert(channel.allowOptionsWhenClosed) + XCTAssertNoThrow(try channel.setOption(.connectTimeout, value: .milliseconds(42)).wait()) + XCTAssertNoThrow(XCTAssertEqual(try channel.getOption(.connectTimeout).wait(), .milliseconds(42))) + + channel.allowOptionsWhenClosed = false + XCTAssertThrowsError(try channel.setOption(.connectTimeout, value: .milliseconds(42)).wait()) + XCTAssertThrowsError(try channel.getOption(.connectTimeout).wait()) + } }