spring-projects/spring-integration — pull request #1183 by garyrussell (2014-06-24 19:45:10+00:00)
Title: INT-3453 Race Condition in NIO Connection

JIRA: https://jira.spring.io/browse/INT-3453

Race condition in `TcpNioConnection` could cause an
assembler thread to be tied up when there is not
any data to process. Thread is not freed up until
more IO occurs on the socket (or it is closed).

Race condition is caused by the `writingToPipe`
boolean; the assembler could see it `true` when
there is no data present (not yet reset by the
IO thread).

- Move `writingToPipe` to the `ChannelInputStream` (rename to `pendingData`)
- Set it before launching an assembler
- Reset it atomically under `pendingDataLock` in `write` after the `available` property has been incremented
- The assembler reads its value under the `pendingDataLock`.

It is now not possible for `pendingData` to be true when the data that was pending has been processed.

INT-3453 Test Case

Exposes the race condition when `writingToPipe` is not reset in time
and we end up tying up an assembler thread on a socket with no data.

Examines the assembler thread's stack trace to ensure it does not
contain `ChannelInputStream.getNextBuffer()`.

__cherry-pick to 4.0.x, 3.0.x__ (3.0.x will need an import fixed).