Skip to content
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

Do not fail when compressing empty HttpContent #13655

Merged
merged 2 commits into from Oct 10, 2023

Conversation

yawkat
Copy link
Contributor

@yawkat yawkat commented Oct 9, 2023

Motivation:

HttpContentCompressor fails with an exception when trying to write an empty HttpContent:

io.netty.handler.codec.EncoderException: MessageToMessageCodec$1 must produce at least one message.
	at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:99)
	at io.netty.handler.codec.MessageToMessageCodec.write(MessageToMessageCodec.java:116)
	at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:879)
	at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:863)
	at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:968)
	at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:856)
	at io.netty.channel.ChannelDuplexHandler.write(ChannelDuplexHandler.java:115)

Modification:

If the compression has no output, write an empty dummy HttpContent. This is not an ideal solution, but this should not happen often anyway.

Result:

HttpContentCompressor does not fail anymore.

Motivation:

HttpContentCompressor fails with an exception when trying to write an empty HttpContent:

```
io.netty.handler.codec.EncoderException: MessageToMessageCodec$1 must produce at least one message.
	at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:99)
	at io.netty.handler.codec.MessageToMessageCodec.write(MessageToMessageCodec.java:116)
	at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:879)
	at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:863)
	at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:968)
	at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:856)
	at io.netty.channel.ChannelDuplexHandler.write(ChannelDuplexHandler.java:115)
```

Modification:

If the compression has no output, write an empty dummy HttpContent. This is not an ideal solution, but this should not happen often anyway.

Result:

HttpContentCompressor does not fail anymore.
@@ -208,6 +209,9 @@ protected void encode(ChannelHandlerContext ctx, HttpObject msg, List<Object> ou
ensureContent(msg);
if (encodeContent((HttpContent) msg, out)) {
state = State.AWAIT_HEADERS;
} else if (out.isEmpty()) {
// MessageToMessageCodec needs at least one output message
out.add(new DefaultHttpContent(Unpooled.EMPTY_BUFFER));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should better move this to encodeContent WDYT ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yawkat wdyt ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry didn't have time to try this yesterday.

I think it makes more sense to have this in encode. It's only necessary if no further items are added after the call to encodeContent, and in encode, it's more clear that this is the case. In both branches where encodeContent is called (AWAIT_CONTENT and AWAIT_HEADERS + FullHttpResponse) it turns out that encodeContent is only called once and is the last one to add to out as well, so moving the patch would work, but imo this is much harder to see so the check makes more sense in encode.

…ntCompressorTest.java

Co-authored-by: Norman Maurer <norman_maurer@apple.com>
@normanmaurer normanmaurer added this to the 4.1.100.Final milestone Oct 9, 2023
@normanmaurer normanmaurer merged commit 4911448 into netty:4.1 Oct 10, 2023
14 checks passed
normanmaurer added a commit that referenced this pull request Oct 11, 2023
Motivation:

HttpContentCompressor fails with an exception when trying to write an
empty HttpContent:

```
io.netty.handler.codec.EncoderException: MessageToMessageCodec$1 must produce at least one message.
	at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:99)
	at io.netty.handler.codec.MessageToMessageCodec.write(MessageToMessageCodec.java:116)
	at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:879)
	at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:863)
	at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:968)
	at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:856)
	at io.netty.channel.ChannelDuplexHandler.write(ChannelDuplexHandler.java:115)
```

Modification:

If the compression has no output, write an empty dummy HttpContent. This
is not an ideal solution, but this should not happen often anyway.

Result:

HttpContentCompressor does not fail anymore.

---------

Co-authored-by: Norman Maurer <norman_maurer@apple.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants