GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Race condition in MessageToMessageEncoder if ch.write(..) is called from channelWritabilityChanged · Issue #5028 · netty/netty

Race condition in MessageToMessageEncoder if ch.write(..) is called from channelWritabilityChanged #5028

New issue
Copy link
New issue
Copy link
Open
Open
Race condition in MessageToMessageEncoder if ch.write(..) is called from channelWritabilityChanged#5028
Copy link
Assignees
normanmaurer
Labels
defect

Description

@eun-ice
eun-ice
opened on Mar 23, 2016
Issue body actions

Hi,

I triggered a race condition by calling ctx.channel().write(...) from channelWritabilityChanged.

One can argue if it's a bug or expected behavior, but since I think it is very hard to track down for new netty users it should be fixed or the user should be warned in documentation of channelWritabilityChanged.

Assuming your handler calls write in "channelWritabilityChanged" e.g. like:

        @Override
        public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
            if (ctx.channel().isWritable()) 
               ctx.channel().write(anotherChunk);
        }

and then write something to the channel

channel.write(someChunk);

you end up triggering a race condition in MessageToMessageEncoder breaking HTTP chunked transfer.

This could produce this output:

a - size(someChunk)
a - CRLF
a - someChunk
b - size(anotherChunk)
b - CRLF
b - anotherChunk
b - CRLF
a - CRLF

instead of

a - size(someChunk)
a - CRLF
a - someChunk
a - CRLF
b - size(anotherChunk)
b - CRLF
b - anotherChunk
b - CRLF

Browser won't parse that and terminate the http connection.

This is because MessageToMessageEncoder triggers multiple writes for each HTTP chunk (chunk size, CRLF, chunk content, CRLF). channelWritabilityChanged seems to get fired between these writes:

for (int i = 0; i < sizeMinusOne; i ++) {
   ...
   ctx.write(out.get(i), p);   // channelWritabilityChanged might be triggered somewhere here
}
ctx.write(out.get(sizeMinusOne), promise); // last write

Since we are still inside the eventloop, the the write inside channelWritabilityChanged is executed right away before the chunk is written completely.

I fixed it for me by no longer directly invoking write from channelWritabilityChanged:

        @Override
        public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
            if (ctx.channel().isWritable()) 
                ctx.invoker().executor().execute(() -> ctx.channel().write(anotherChunk));
        }

So in my opinion, enqueuing channelWritabilityChanged in the eventloop could fix it inside netty.

Activity

Next
  1. normanmaurer commented on Mar 23, 2016

    @normanmaurer
    normanmaurer
    on Mar 23, 2016
    Member
    More actions

    @eun-ice sounds good... let me see where we could do this in a more general way.

  2. normanmaurer
    self-assigned this
    on Mar 23, 2016
  3. normanmaurer
    added this to the 4.0.36.Final milestone on Mar 23, 2016
  4. normanmaurer
    added
    defect
    on Mar 23, 2016
  5. normanmaurer commented on Mar 23, 2016

    @normanmaurer
    normanmaurer
    on Mar 23, 2016
    Member
    More actions

    @eun-ice I need to think a bit more about this and how to solve this. I will let you know once I have something to show.

  6. eun-ice commented on Mar 23, 2016

    @eun-ice
    eun-ice
    on Mar 23, 2016
    ContributorAuthor
    More actions

    @normanmaurer thank you, since I have a workaround it is not very urgent for me.

  7. normanmaurer commented on Apr 1, 2016

    @normanmaurer
    normanmaurer
    on Apr 1, 2016
    Member
    More actions

    @eun-ice I think I have a fix... stay tuned

  8. eun-ice commented on Apr 1, 2016

    @eun-ice
    eun-ice
    on Apr 1, 2016
    ContributorAuthor
    More actions

    Thank you!

  9. normanmaurer
    modified the milestones: 4.0.36.Final, 4.0.37.Final on Apr 4, 2016
  10. normanmaurer
    added 3 commits that reference this issue on Apr 4, 2016

    [#5028] Fix re-entrance issue with channelWritabilityChanged(...) and…

    ab28f09
  11. 105 remaining items

  12. normanmaurer
    modified the milestones: 4.1.30.Final, 4.1.31.Final on Sep 25, 2018
  13. normanmaurer
    modified the milestones: 4.1.31.Final, 4.1.32.Final on Oct 30, 2018
  14. pulllock
    added 2 commits that reference this issue on Oct 19, 2023

    [netty#5028] Fix re-entrance issue with channelWritabilityChanged(...…

    2b0e48d

    Revert "[netty#5028] Fix re-entrance issue with channelWritabilityCha…

    8019447
  15. normanmaurer
    added 2 commits that reference this issue on Oct 2, 2026

    Don't corrupt ChannelOutboundBuffer when a cancelled write makes the …

    594e01c

    Port 4.1: Don't corrupt ChannelOutboundBuffer when a cancelled write …

    1777a62
  16. lltopk
    added a commit that references this issue on Oct 24, 2026

    Port 4.1: Don't corrupt ChannelOutboundBuffer when a cancelled write …

    7b576d9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @normanmaurer
    normanmaurer

Labels

defect

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.