Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No connection closed event when stopping Tomcat app #82

Closed
skrishnankutty opened this issue Aug 7, 2017 · 1 comment
Closed

No connection closed event when stopping Tomcat app #82

skrishnankutty opened this issue Aug 7, 2017 · 1 comment

Comments

@skrishnankutty
Copy link

We are having an issue with the following setup:

  • We have a server application that uses Spring's implementation of SSE. This app is deployed on a Tomcat 8.5 server, say on http://localhost:8080/myapp. The 'register' method on this app creates the SseEmitter like this:
    SseEmitter sseEmitter = new SseEmitter(Long.valueOf(this.TIMEOUT)); SseEventBuilder sseEventBuilder = SseEmitter.event(); sseEventBuilder.reconnectTime(30000); sseEmitter.send(sseEventBuilder);

  • We have a client application that uses the eventsource-polyfill.js example. the EventSource object is initialized with an endpoint of the server app url and the corresponding open, message and error handlers:

      var emitterUrl = 'http://localhost:8080/myapp/register';
      this.eventSource=new EventSource(emitterUrl,{withCredentials: true});
      //set up EventHandlers
      this.eventSource.onopen =this.openHandler;
      this.eventSource.onmessage =this.messageHandler;
      this.eventSource.onerror =this.errorHandler;
      //set up listeners
      let list = (event && this.listeners[event.type]) || [];
      list.forEach(listener =>this.eventSource.addEventListener(type,event));
    

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
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants