Andrew Rueckert
Andrew Rueckert

Reputation: 5215

Does Javascript's Abortable Fetch close the HTTP connection?

As of about 2017, it has been possible to abort a fetch and immediately move-on in front-end javascript. I'm having trouble finding any information on what this does to the HTTP connection, though. Does it close it prematurely, or does the browser keep the request open, and just discard the response as it arrives?

I have a use-case where a user is making a (potentially) expensive database calls from a webapp frontend. Sometimes, they notice that a request is taking too long, and manually cancel it. I would love to be able to take that signal and cancel my expensive database query, since they're no longer interested in the results.

Is there any way that my REST server can tell that the fetch has been aborted? (My server is a Java Jersey/Grizzly.)

Upvotes: 2

Views: 1265

Answers (1)

ZachB
ZachB

Reputation: 15366

It's generally supposed to:

https://fetch.spec.whatwg.org/#http-network-fetch step 17.2.3/4

  1. If aborted, then:

    1. If fetchParams is aborted, then:

      1. Set response’s aborted flag.

      2. If stream is readable, then error stream with the result of deserialize a serialized abort reason given fetchParams’s controller’s serialized abort reason and an implementation-defined realm.

    2. Otherwise, if stream is readable, error stream with a TypeError.

    3. If connection uses HTTP/2, then transmit an RST_STREAM frame.

    4. Otherwise, the user agent should close connection unless it would be bad for performance to do so.

      For instance, the user agent could keep the connection open if it knows there’s only a few bytes of transfer remaining on a reusable connection. In this case it could be worse to close the connection and go through the handshake process again for the next fetch.

However, Firefox currently might not in some cases (https://bugzilla.mozilla.org/show_bug.cgi?id=1568422).

Upvotes: 5

Related Questions