LogbookServerHandler - attempt to write logs when the handler is prematurely removed from the ReactorNetty pipeline #2064
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
LogbookServerHandler
'schannelRead()
method is expecting to be invoked at least once with message object that is instance ofio.netty.handler.codec.http.LastHttpContent
.When the handler is added via
reactor.netty.Connection.addHandlerLast()
method, ReactorNetty registers the handler for removal on context termination, and this termination is sometimes triggered before the lastLastHttpContent
message is passed to the read method. Leaving theLogbookServerHandler
's Sequence object only with response written.This change proposes to add a trigger on
handlerRemoved()
method ofio.netty.channel.ChannelHandlerAdapter
to see if the response writing stage was already submitted to theSequence
and add the request writing stage and trigger the runnables for both stages.Below is the initial thought process, which proved to be wrong. The issue turned out to be not in the order of the LogbookServerHandler (it's added in the same place eventually for the bare spring boot webflux app between
HttpTrafficHandler
andReactiveBridge
), but in the way it is added.This happens when the handler is added usingaddHandlerLast()
, which positioned it too late in the pipeline. By that point, other handlers (like content aggregators) had already consumed theLastHttpContent
messages. When LogbookServerHandler was positioned incorrectly, it only saw the write() events but missed the complete read() events.This change proposes to position theLogbookServerHandler
in the pipeline at the point when the message is readable and bufferable, but before handlers that may skip lead to skipping the pipeline calls for the last closing message.The solution also offers the following order of checks as none of the netty handlers that the solution relies on are mandatory (see NettyPipeline):First priority: After HttpTrafficHandler (where HTTP messages are fully decoded but not yet consumed)Second priority: After HttpCodec (when HttpTrafficHandler isn't available)
Last resort: At the end of the pipeline (as it works now)
Motivation and Context
Relates to #2053, #1555, #1926
Types of changes
Checklist: