Oh, this sounds surprising for me, I was sure that callback will be functional until request processing completion. Using this bundle of knowledge, I've put HttpAsyncExchange.submitResponse() after heavy-weight task execution, and got required behaviour — Cancellable.cancel() was called ≈1ms after connection termination.
BasicAsyncResponseProcuder.close() overriding also makes sense for me, as it a much more handy that IOException. Frankly speaking I haven't noticed any differences in HttpCore behaviour between 4.2.1 and r1358777: both works fine with HttpAsyncExchange.submitResponse() called right before response generation and BasicAsyncResponseProcuder.close() overloaded to call Cancellable.cancel() -- Thank you for your help and quick responses. On Sun, Jul 8, 2012 at 9:35 PM, Oleg Kalnichevski <[email protected]> wrote: > On Fri, 2012-07-06 at 11:11 +0400, Dmitry Potapov wrote: >> On Fri, Jul 6, 2012 at 1:30 AM, Oleg Kalnichevski <[email protected]> wrote: >> > On Thu, 2012-07-05 at 22:25 +0400, Dmitry Potapov wrote: >> >> Hello everyone, >> >> >> >> I develop streaming HTTP server for my project and used HttpCore NIO >> >> for this purpose. >> >> Interruptible heavy-weight task takes place before streaming start, >> >> so, I'm interesting in any mechanism that provides connection status >> >> check, because I have tons of requests which terminates connection >> >> before I start streaming. >> >> APIs I've already tried: >> >> >> >> 1. HttpAsyncExchange.setCallback() — doesn't work, cancel() function >> >> of the registered callback was never called. Doesn't matter how >> >> connection was terminated, with RST or FIN. >> > >> > Dmitry >> > >> > There is a test case specifically intended to test Cancellable callback >> > for HTTP exchanges >> > >> > https://svn.apache.org/repos/asf/httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncHandlerCancellable.java >> This was a first file I've read before implementing my own Cancellable. >> >> > >> > If this functionality is broken I would like to know how the problem can >> > be reproduced. >> >> I've prepared complete test for this. >> Source code and output are placed on github: https://gist.github.com/3058489 >> It can be compiler and run using: >> javac -cp lib/httpcore-4.2.1.jar:lib/httpcore-nio-4.2.1.jar HttpServer.java >> java -cp lib/httpcore-4.2.1.jar:lib/httpcore-nio-4.2.1.jar:. HttpServer >> >> Test structure: >> 1. Create async server with DefaultListeningIOReactor and start it in >> separate thread >> 2. Sleep for 1 second >> 3. Send request as it done in TestHttpAsyncHandlerCancellable.java >> 4. Close request socket >> >> What is done in request handler: >> 1. Set chunked input stream entity in response object. >> 1.a. InputStreamEntiry is created on top of PipedInputStream, which >> corresponing PipedOutputStream will be passed to worker thread in step >> 4. >> 2. Set callback for the HttpAsyncExchange. Callback will be passed to >> worker thread in step 4. >> 3. Submit basic async response producer >> 4. Start separate thread which will do some work and then produce output >> 4.a. (Heavy-weight task emulation) Three times check if connection is >> still alive and wait 1 second >> 4.b. (Heave-weight output emulation) Three times check if connection >> is still alive and write response chunk >> 4.c. Close output stream, which causes InputStreamEntity to send >> null-length chunk >> >> I hope this will help to locate problem. > > Hi Dmitry > > The problem turned out to be two-fold. Firstly, your expectation was > wrong. Once a response has been submitted the HTTP exchange is > considered completed and the Cancellable callback is no longer used. All > important protocol events including premature connection termination are > propagated to the HttpAsyncResponseProducer (see the test case > #testResponsePrematureTermination [1]). Secondly I also discovered that > the async protocol handler disabled input event notifications if no > input is to be expected. This is not an unreasonable strategy. However > that prevented the protocol hander from being able to react to > unexpected events such permature connection shutdown by the opposite > endpoint. I fixed the problem in SVN trunk and the 4.2.x branch. Please > re-test your application against the latest 4.2.x snapshot. > > Oleg > > [1] > https://svn.apache.org/repos/asf/httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncHandlerCancellable.java > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
