Random Service Worker Response Error

506 views
Skip to first unread message

Láďa Říha

unread,
Jun 1, 2016, 3:44:37 PM6/1/16
to service-worker-discuss

I'm using Service Worker and Cache API to cache static resources but randomly http request to REST API endpoint (which is not being cached in SW) fails and the only message I have is xhr.statusText with text Service Worker Response Error and response code 500.

I can't tell if this error happens only with URLs that are not being cached or not. There is not enough evidence about either. This happens only in Chrome (50.0.2661.75 - 64b) and it works in Firefox 45

I wasn't able to reproduce it manually as it happens in Selenium tests and it appears to be random. Moreover it happens on localhost (where SW should work despite plain http) but also in domain with HTTPS that has self-signed certificate and as such SW should not even work there ...

Selenium tests are often refreshing pages and closing browser window but I have no idea if it matters.

Any ideas why it could be happening or how to get more information?



var VERSION = "sdlkhdfsdfu89q3473lja";
var CACHE_NAME = "cache" + VERSION;
var CACHE_PATTERN = /\.(js|html|css|png|gif|woff|ico)\?v=\S+?$/;

function fetchedFromNetwork(response, event) {
    var cacheCopy = response.clone();
    var url = event.request.url;
    if (url.indexOf("/api/") === -1 // must not be a REST API call
        && url.indexOf(VERSION) > -1 // only versioned requests
        && VERSION !== "$CACHE_VERSION"
        && CACHE_PATTERN.test(url)) { //
        caches.open(CACHE_NAME)
            .then(function add(cache) {
                cache.put(event.request, cacheCopy);
                });
    }
    return response;
}

function unableToResolve() {
    return new Response("Service Unavailable", {
        status: 503,
        statusText: "Service Unavailable",
        headers: new Headers({
            "Content-Type": "text/plain"
        })
    });
}

this.addEventListener("fetch", function (event) {
    // cache GET only
    if (event.request.method !== "GET") {
        return;
    }

    event.respondWith(
        caches.match(event.request)
        .then(function (cached) {
            if (cached) {
                return cached;
            } else {
                return fetch(event.request)
                        .then(function (response) {
                            return fetchedFromNetwork(response, event);
                        }, unableToResolve)
                        .catch(unableToResolve);
            }
        }, function () { // in case caches.match throws error, simply fetch the request from network and rather don't cache it this time
            return fetch(event.request);
        }));
});


this.addEventListener("activate", function (event) {
    event.waitUntil(
        caches.keys()
        .then(function (keys) {
            return Promise.all(
                    keys.filter(function (key) {
                        // Filter out caches not matching current versioned name
                        return !key.startsWith(CACHE_NAME);
                    })
                    .map(function (key) {
                        // remove obsolete caches
                        return caches.delete(key);
                    }));
        }));
});

Matt Falkenhagen

unread,
Jun 2, 2016, 2:51:01 AM6/2/16
to Láďa Říha, service-worker-discuss
It would help if you can get netlog during the failure using chrome://net-internals, but that may be difficult if this doesn't reproduce manually.

Is it possible that a service worker update is occurring while a request is being handled? That can result in the service worker getting terminated and the request failing:

You might try to build Chromium and see if you can introduce logging to see when the failure occurs. The error is generated here in ServiceWorkerURLRequestJob::DeliverErrorResponse:

I'll see if I can reproduce as well, but may take a while.

--
You received this message because you are subscribed to the Google Groups "service-worker-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to service-worker-di...@chromium.org.
To post to this group, send email to service-wor...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/service-worker-discuss/a8b51870-14d0-440a-9696-00ea548f5401%40chromium.org.

Láďa Říha

unread,
Jun 2, 2016, 3:14:07 AM6/2/16
to Matt Falkenhagen, service-worker-discuss
Thanks for the reply Matt.

I don't think this happens during SW update as when I run Selenium tests, it starts Chrome with fresh new profile and SW file is not changing during the tests so I imagine there is no need for triggering the update. That being said, the tests makes a hundreds of HTTP requests and also some page reloads. It is possible that some HTTP requests are being canceled by following page reload, not sure if this is important. Is it possible that it might be caused by interrupting (due to the page reload) SW in "activate" event?

I'll try to create a reproducible case (it will take some time as I cannot share the sources so I need to create similar setup and hope for the "best") and publish it on github. I'll have a look at it over the weekend. I can pause the tests in debugger when this error happens so I can check net-internals but would there be some relevant information if I open them after the error has happened? I might try to keep them open during the tests but I'm not sure if it is possible.

Building Chromium is last a resort, but I'm not saying no :) 

Thanks
--

S pozdravem, Láďa Říha

Matt Falkenhagen

unread,
Jun 2, 2016, 3:19:13 AM6/2/16
to Láďa Říha, service-worker-discuss
I see. The page reload shouldn't interrupt the service worker in the middle of "activate", if an update is not happening.

Unfortunately, net-internals has to be started before the error happens to catch the error.

Láďa Říha

unread,
Jun 27, 2016, 2:47:16 AM6/27/16
to Matt Falkenhagen, service-worker-discuss
I've managed to create a reproducible case and reported it as a Chromium bug: https://bugs.chromium.org/p/chromedriver/issues/detail?id=1413

Thanks,
Lada
Reply all
Reply to author
Forward
0 new messages