While using streaming responses using a RestEasy + Tomcat server, dnagarajan89 observed that the responses at times seems to gets truncated on the client side.
On Looking into it I observed that this seems to happen as asyncWhile stalls during execution. As one of futures (AsyncOperation) in the chain of futures setup in the writeTo method, is lost during the execution of the flushStream operation in DeferredOutputStream, under the following conditions.
Consider that an AsyncOperation, in the DeferredOutputStream, is not completed in doWork (its promise is not resolved, because the stream is no longer ready) it also happens to have dependent operations waiting on its chain(setup in writeTo). And it gets completed subsequently by the onWritePossible listener.
But, its completion triggers the execution of the dependent AsyncOperations (until the stream is blocked again). And when it resumes post that, the ref it holds for the lastAsyncOperation is no longer the one it called complete on but the last async-operation in the dependent chain that got blocked by the stream. Which it incorrectly resets causing the ref to an operation to be lost and stalling the pipeline.