The view counter that turned our catalogue into a 502

Shipping

The page that lists every kit we sell answered every visitor with a 502 Bad Gateway for about 41 hours after launch. Each kit's own page worked, the home page worked, checkout worked. The broken part was a few lines that counted page views.

The bug

Our store's server counts visits to kit pages. To find which kit a request is for, it took the second part of the path:

self._count_view(path.strip("/").split("/")[1] if path.count("/") >= 2 else "")

For /kits/studio/ that is studio. For the catalogue itself, /kits/, the path has two slashes, so the guard let it through, but after stripping the slashes there is only one part, and [1] raised an IndexError. The handler is Python's standard http.server: an exception in do_GET closes the connection without an answer, and Cloudflare showed the visitor a 502.

The fix is one small function that returns an empty string when the part is missing. The view count only ever needed the kit's name; it never needed to be able to break the page it was counting.

Why none of our checks saw it

What changed

The takeaway

Side work in a request handler, such as counting, logging or tracking, must never be able to fail the request, and a page check that only covers the pages you just changed misses the ones that link to them. The watch in Night Watch takes any list of pages, each with text it must contain, not just home pages.

Updated: