EventSource / eventsource

EventSource client for Node.js and Browser (polyfill)
MIT License
911 stars 252 forks source link

No connection closed event when stopping Tomcat app #82

Closed skrishnankutty closed 7 years ago

skrishnankutty commented 7 years ago

We are having an issue with the following setup:

The following test behaves as expected:

  1. We start Tomcat and start our app
  2. We access our client app which, on startup, initializes the EventSource object
  3. We shutdown Tomcat completely Result: the "onConnectionClosed" method is called which calls the error handler method on our client app with the following event:

currentTarget: EventSource onerror:f() onmessage:ƒ () onopen:ƒ () readyState:0 url:"http://localhost:8080/myapp/register" withCredentials:true proto:EventSource defaultPrevented:false eventPhase:2 isTrusted:true

path:[] returnValue:true srcElement: EventSource {url: "http://localhost:8080/myapp/register", withCredentials: true, readyState: 0, onopen: ƒ, onmessage: ƒ, …} target:EventSource {url: "http://localhost:8080/myapp/register", withCredentials: true, readyState: 0, onopen: ƒ, onmessage: ƒ, …} type:"error"

But in this case we don't get the expected behaviour:

  1. We start Tomcat and start our app
  2. We access our client app which, on startup, initializes the EventSource object
  3. We stop our server app but the tomcat server remains started Result: On our client app, the error handler method is never called, so it thinks the connection is still active. We believe EventSource should fire an error event in this case.

Tested on eventsource-polyfill.js versions: 1.0.4, 0.2.1

aslakhellesoy commented 7 years ago

If I understand correctly, a full shutdown of tomcat will trigger the onerror event in the client. However, only stopping the server app, but not tomcat, does not trigger the onerror event in the client.

From the EventSource's perspective, the client either has an open http connection or it does not. When the server interrupts the http connection, the onerror event is triggered.

It sounds like stopping the server app doesn't close the http connection, and therefore, onerror isn't triggered in the client.

EventSource connections are long-lived http connections. It's not uncommon for web servers to not shut down until all HTTP connections have finished. With EventSource connections, this never happens, so the shutdown never completes.

You probably need to explicitly close all open EventSource connections before you shut down. Or figure out a way to make that happen automatically.

In Node.js there is https://github.com/isaacs/server-destroy to solve this problem - I'm not sure what's available for Tomcat.