diff --git a/Sources/NIO/Channel.swift b/Sources/NIO/Channel.swift index d7eee17b7..bea52ccb0 100644 --- a/Sources/NIO/Channel.swift +++ b/Sources/NIO/Channel.swift @@ -1116,7 +1116,12 @@ class BaseSocketChannel : SelectableChannel, ChannelCore { return } - safeDeregister() + interestedEvent = .none + do { + try selectableEventLoop.deregister(channel: self) + } catch let err { + pipeline.fireErrorCaught0(error: err) + } do { try socket.close() @@ -1288,17 +1293,6 @@ class BaseSocketChannel : SelectableChannel, ChannelCore { return interestedEvent == .write || interestedEvent == .all } - // Methods only used from within this class - private func safeDeregister() { - interestedEvent = .none - do { - try selectableEventLoop.deregister(channel: self) - } catch let err { - pipeline.fireErrorCaught0(error: err) - close0(error: err, promise: nil) - } - } - private func safeReregister(interested: IOEvent) { guard !closed else { interestedEvent = .none diff --git a/Sources/NIO/EventLoop.swift b/Sources/NIO/EventLoop.swift index 512f05905..eac753a93 100644 --- a/Sources/NIO/EventLoop.swift +++ b/Sources/NIO/EventLoop.swift @@ -130,6 +130,10 @@ final class SelectableEventLoop : EventLoop { func deregister(channel: C) throws { assert(inEventLoop) + guard !closed else { + // Its possible the EventLoop was closed before we were able to call deregister, so just return in this case as there is no harm. + return + } try selector.deregister(selectable: channel.selectable) }