reactor-netty
reactor-netty copied to clipboard
Potential out of memory error if writing too fast and having slow receiver
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 can you support a simple demo to reproduce this issue?
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.