title: How to prevent net::ERR_INCOMPLETE_CHUNKED_ENCODING when using HTML5 Server events and Java Servlets?
body: <p>i just started to play around with Server Events and i run into a chrome error message i would like to understand. i searched the web real quick but didn't find an explanation so i guess i may do something terribly wrong.</p>
  
  <p>On the server side i have a simple servlet that accepts requests and creates a dummy event creator task:</p>
  
  <pre><code>private Executor executor = Executors.newSingleThreadExecutor();
  
  public void doGet(final HttpServletRequest request, final HttpServletResponse response)
  {
    final AsyncContext asynCtx = request.startAsync(request, response);
  
    response.setHeader("Cache-Control", "no-cache");
    response.setContentType("text/event-stream");
    response.setCharacterEncoding("utf-8");
  
    executor.execute(() -&gt; {
      boolean run = true;
      try
      {
        while (run)
        {
          final ServletResponse resp = asynCtx.getResponse();
          run = resp != null;
  
          if (resp != null)
          {
            System.out.println("pushing a server event.");
            final PrintWriter writer = asynCtx.getResponse().getWriter();
            writer.println("data: {time: " + System.currentTimeMillis() + "}\n");
            writer.flush();
          }
          else
          {
            System.out.println("stopping beeper, no response object available anymore.");
            break; // do not run anymore, we got no response
          }
  
          Thread.sleep(2000);
        }
      }
      catch (final Exception e)
      {
        e.printStackTrace();
      }
    });
  
  }
  </code></pre>
  
  <p>on the client i simply:</p>
  
  <pre><code>$(document).ready(function ()
  {
  
     var source = new EventSource("/events");
     source.onmessage = function (event)
     {
       console.log("received event: " + JSON.stringify(event));
       document.getElementById("eventContainer").innerHTML += event.data + "&lt;br/&gt;";
     };
  
     console.log("start to receive events...")
  });
  </code></pre>
  
  <p>when i load the HTML file it works fine, events are received and written to the console. But <strong>after 30 seconds</strong> i get an error message:</p>
  
  <p><strong>GET [HttpOfLocalhost]/events net::ERR_INCOMPLETE_CHUNKED_ENCODING</strong> </p>
  
  <p>why?</p>
  
  <p>the request than gets killed and a new one is started immediately so it doesn't kill the application but error messages on the console aren't nice.</p>
  
  <p>screenshot of my developer console:</p>
  
  <p><img src="https://i.stack.imgur.com/oMlBb.jpg" alt="enter image description here"></p>
  
  <p>request/resposne details:</p>
  
  <p><img src="https://i.stack.imgur.com/Ddoju.jpg" alt="enter image description here"></p>
  
  <p>timing, this shows that it always occurs after 30 seconds:</p>
  
  <p><img src="https://i.stack.imgur.com/bMy7b.jpg" alt="enter image description here"></p>
  
  <p>Thanks!</p>