Skip to content

Cancel requests on the server-side when client aborts #2349

Closed as not planned

Description

@karussell

If client is no longer interested in the response we should cancel it on the server side. Maybe there is some jetty or dropwizard function to do this.

Activity

  1. michaz commented on Aug 20, 2021

    @michaz
    Member

    I remember the following (broad strokes):

    When you stream your response (i.e. write to a StreamingResponse rather than building a big expensive object and then returning it), you get this automatically. The StreamingResponse throws an exception when you write to it while the connection is closed, and you're done.

    (I remember seeing a fixed bug report on Dropwizard once that this was logged as an error, but it isn't an error, it's normal.)

    Otherwise you have to periodically check Thread.isInterrupted() or something in your computation code, and possibly also tell the server somehow that it should interrupt (not sure). But it shouldn't be necessary to import webserver-specific stuff into the computation code.

  2. karussell commented on Aug 29, 2022

    @karussell
    MemberAuthor

    Otherwise you have to periodically check Thread.isInterrupted() or something in your computation code

    Unfortunately this is not possible. Or at least I couldn't get it working.

    I added a 2ms sleep in runAlgo to delay any request (by a lot). And then, although I interrupted the client in the middle of a very long taking request (tried with browser and curl) the method Thread.currentThread().isInterrupted() always returned false. Even when I lowered application_connectors[0].idle_timeout and idle_thread_timeout to 2 seconds.

    The problem is likely that these threads are in a pool and jetty does not internally interrupt them when the connection is closed and reuses them.

  3. boldtrn commented on Aug 29, 2022

    @boldtrn
    Member

    AFAIK the main problem is that when a client cancels the request, the client doesn't notify the server. The client will just reject the reply from the server. So the server doesn't know about cancelled requests.

  4. karussell commented on Aug 29, 2022

    @karussell
    MemberAuthor

    Yes. I had a too naive understanding of this and I thought that if the client stops the request then at least the (low level) socket should know about this as the "output stream" towards the client closes.

    But I just tried it and the server side socket (Socket s = ss.accept();) was not informed when the client disappeared. And s.isClosed() was always false. Also the other attributes did not change:
    closed: false, bound: true, connected: true, input down: false, output down: false
    Even if the client explicitly called s.close(). But not even sure if Jetty uses Java Sockets or reimplements this.

    The only thing that seems to work was to write a byte into the output stream (leading to an exception).

    This SO answer seems to confirm this.

  5. michaz commented on Aug 30, 2022

    @michaz
    Member

    Oh interesting! My response up there was because I remembered the Shortest Path Tree endpoint, where we do notice when the connection is closed -- but there of course we write all the time because we write nodes while we are still routing!

  6. michaz commented on Aug 30, 2022

    @michaz
    Member

    So what now, do we have to reply with some kind of progress bar in JSON?

    {
      "1": "Still calculating...",
      "2": "Still calculating...",
    
    .
    .
    

    So that we notice when the connection drops?

    (Just kidding)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions