reactor-netty icon indicating copy to clipboard operation
reactor-netty copied to clipboard

Potential out of memory error if writing too fast and having slow receiver

Open xiazuojie opened this issue 5 years ago • 2 comments

Currently the code does not check whether channel is writable before invoking ChannelHandlerContext.write(). Instead, it checks whether channel is writable after writing to it, and flushes the channel in case it's not writable.

In the case of writing fast and having slow receiver, flushing may not make a channel writable immediately. However, the code always writes incoming messages to the channel. Messages may pile up in the internal queue of netty, and worse, exhaust memory if the situation (writing fast and having slow receiver) never changes.

Expected Behavior

No out of memory error when having slow receivers.

Actual Behavior

Messages piles up in the internal queue of netty, and worse, exhausts memory if the situation (writing fast and having slow receiver) never changes

Steps to Reproduce

Possible Solution

Need to check whether channel is writable (buffer not full) before writing to it. In case it's not writable, discard new messages with an error.

Your Environment

  • Reactor version(s) used:0.8.9
  • Other relevant libraries versions (eg. netty, ...):rsocket-java
  • JVM version (javar -version):java version "1.8.0_101"
  • OS and version (eg uname -a):Darwin *** 19.0.0 Darwin Kernel Version 19.0.0: Wed Sep 25 20:18:50 PDT 2019; root:xnu-6153.11.26~2/RELEASE_X86_64 x86_64

xiazuojie avatar Oct 30 '19 08:10 xiazuojie

@xiazuojie can you support a simple demo to reproduce this issue?

ctlove0523 avatar Apr 26 '21 03:04 ctlove0523

You can emulate this by pausing (through debug) the receiver process while the sender continues writing messages. The sender will be out of memory eventually.

It's a pitfall warned by Norman Maurer (one of the core developers of Netty) here.

xiazuojie avatar Apr 28 '21 02:04 xiazuojie