net::ERR_CONNECTION_RESET with service worker in Chrome

Viewed 1246

I have a very simple service worker to add offline support. The fetch handler looks like

self.addEventListener("fetch", function (event) {
  var url = event.request.url;
  event.respondWith(fetch(event.request).then(function (response) {
    //var cacheResponse: Response = response.clone();
    //caches.open(CURRENT_CACHES.offline).then((cache: Cache) => {
    //  cache.put(url, cacheResponse).catch(() => {
    //    // ignore error
    //  });
    //});
    return response;
 }).catch(function () {
    // check the cache
    return getCachedContent(event.request);
}));

});

Intermittently we are seeing a net::ERR_CONNECTION_RESET error for a particular script we load into the page when online. The error is not coming from the server as the service worker is picking up the file from the browser cache. Chrome's network tab shows the service worker has successfully fetched the file from the disk cache but the request from the browser to the service worker shows as (failed)

Does anyone know the underlying issue causing this? Is there a problem with my service worker implementation?

1 Answers

This is likely due to a bug in Chrome (and potentially other browsers as well) that could result in a garbage collection event removing a reference to the response stream while it's still being read.

Its fix in Chrome is being tracked at https://bugs.chromium.org/p/chromium/issues/detail?id=934386.

Related