Skip to content

Commit

Permalink
B2MH: helpful message if re-added to pipeline (#903)
Browse files Browse the repository at this point in the history
Motivation:

B2MH used to crash a nil variable if re-added to a pipeline.

Modifications:

provide good error message for the crash instead

Result:

- fixes #902
  • Loading branch information
weissi committed Mar 14, 2019
1 parent ccd386b commit 205f317
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Sources/NIO/Codec.swift
Expand Up @@ -461,6 +461,9 @@ extension ByteToMessageHandler {
extension ByteToMessageHandler: ChannelInboundHandler {

public func handlerAdded(context: ChannelHandlerContext) {
guard self.removalState == .notBeingRemoved else {
preconditionFailure("\(self) got readded to a ChannelPipeline but ByteToMessageHandler is single-use")
}
self.buffer = B2MDBuffer(emptyByteBuffer: context.channel.allocator.buffer(capacity: 0))
// here we can force it because we know that the decoder isn't in use if we're just adding this handler
self.selfAsCanDequeueWrites = self as? CanDequeueWrites // we need to cache this as it allocates.
Expand Down

0 comments on commit 205f317

Please sign in to comment.