The "Should not happen" exception happens because of a race condition
in AsyncWriteQueue which may occur between consumeAction.accept(...)
and state.get() (between lines 159 and 160 in the original file).
There is a small window of opportunity here, where the
WriteEvent registered by consumeAction.accept() may already
have been triggered and entered the flushLoop() and switched
the state to FLUSHING before the already running flushLoop()
had a chance to observe the DELAYED state and running.
This will cause two flushLoop() to run concurrently in the
critical section, and this can cause anything to happen.
The solution is to change the consumeAction() to return a boolean
(false) if it wasn't able to flush all the data.