How to prevent net::ERR_INCOMPLETE_CHUNKED_ENCODING when using HTML5 Server events and Java Servlets? How to prevent net::ERR_INCOMPLETE_CHUNKED_ENCODING when using HTML5 Server events and Java Servlets? google-chrome google-chrome

How to prevent net::ERR_INCOMPLETE_CHUNKED_ENCODING when using HTML5 Server events and Java Servlets?


Ok, so i couldn't stand still and look closer whats happening.

The AsyncContext object has a setTimeout(...) method. Per default in my version of tomcat (Tomcat embedded 8) the value is set to 30,000 ms (30 seconds). That's exactly the duration after i got the net::ERR_INCOMPLETE_CHUNKED_ENCODING error in my chrome console.

i checked using:

System.out.println("Current Timeout is: " + asynCtx.getTimeout() + " ms");

which showed:

Current Timeout is: 30000 ms

so to avoid the net:ERR message someone could set the timeout to 0. But than the event thread keeps running forever (unfortunately). Another solution, which i used, is to add a AsyncListener to the AsyncContext and call the complete() method inside the onTimeout() method.

from the API doc of the complete() method:

Completes the asynchronous operation that was started on the request that was used to initialze this AsyncContext, closing the response that was used to initialize this AsyncContext. Any listeners of type AsyncListener that were registered with the ServletRequest for which this AsyncContext was created will be invoked at their onComplete method.

the source code of my listener:

asynCtx.addListener(new AsyncListener(){  @Override  public void onComplete(AsyncEvent asyncEvent) throws IOException  {    System.out.println("onComplete(...)");  }  @Override  public void onTimeout(AsyncEvent asyncEvent) throws IOException  {    // this will close the request and the context gracefully    // and the net:ERR is gone.    asyncEvent.getAsyncContext().complete();    System.out.println("onTimeout(...)");  }  @Override  public void onError(AsyncEvent asyncEvent) throws IOException  {    System.out.println("onError(...)");  }  @Override  public void onStartAsync(AsyncEvent asyncEvent) throws IOException  {    System.out.println("onStart(...)");  }});

so yes, it was due lack of knowledge. i hope this is helpful for someone.


You can send small "no operation" (noop) chunks to keep the connection alive every 10 seconds or so.