You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cancel requests on the server-side when client aborts #2349
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.
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.
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.
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).
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!
Activity
michaz commentedon Aug 20, 2021
I remember the following (broad strokes):
When you stream your response (i.e. write to a
StreamingResponserather than building a big expensive object and then returning it), you get this automatically. TheStreamingResponsethrows 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.karussell commentedon Aug 29, 2022
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 methodThread.currentThread().isInterrupted()always returned false. Even when I loweredapplication_connectors[0].idle_timeoutandidle_thread_timeoutto2 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.
boldtrn commentedon Aug 29, 2022
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.
karussell commentedon Aug 29, 2022
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 alwaysfalse. Also the other attributes did not change:closed: false, bound: true, connected: true, input down: false, output down: falseEven 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.
michaz commentedon Aug 30, 2022
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!
michaz commentedon Aug 30, 2022
So what now, do we have to reply with some kind of progress bar in JSON?
So that we notice when the connection drops?
(Just kidding)