The Phantom Unread Badge

Medium

· Frontend

Incident

SEV-3

Time to fix: 20 min

Failing checks on the shipped build

  • re-delivered messages count once

    id 1 was re-delivered: expected 2 but got 3

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the value the screen shows back through the state and the pure helpers that compute it; the view is usually only the messenger.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Inspired by

Slack

Inspired by the eternally-wrong unread badges of chat apps. The socket re-delivers messages after reconnects, and marking a channel read somehow leaves exactly one message behind.

unread.js folds the event stream into a badge count — two bugs, one function.

Code map

// Folds the socket event stream into unread counts per channel.
exports.unreadCounts = function (events) {
  var perChannel = {};

  for (var i = 0; i < events.length; i++) {
    var e = events[i];
    if (e.type === "message") {
      if (!perChannel[e.channel]) perChannel[e.channel] = [];
      perChannel[e.channel].push(e.id);
    } else if (e.type === "read") {
      var list = perChannel[e.channel] || [];
      perChannel[e.channel] = list.filter(function (id) {
        return id > e.upToId + 1;
      });
    }
  }

  var out = {};
  for (var ch in perChannel) out[ch] = perChannel[ch].length;
  return out;
};

Sign in to edit the file, run the tests and submit a fix

A free account — no card, no trial clock. You come straight back here.

The Phantom Unread Badge — JavaScript Bug Hunt

Inspired by the eternally-wrong unread badges of chat apps. The socket re-delivers messages after reconnects, and marking a channel read somehow leaves…

  • Language: JavaScript
  • Layer: Frontend
  • Difficulty: Medium
  • Concepts: State, Dedupe
  • Modelled on: Slack
  • Visible tests: re-delivered messages count once; read clears everything up to and including upToId; messages after the read marker stay unread
  • Reward: 50 XP for a complete fix

Incident

  • Severity: SEV-3
  • Time to fix: 20 minutes, the target for a medium incident

Failing checks on the shipped build

  • re-delivered messages count once — id 1 was re-delivered: expected 2 but got 3

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the value the screen shows back through the state and the pure helpers that compute it; the view is usually only the messenger.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Briefing

Inspired by the eternally-wrong unread badges of chat apps. The socket re-delivers messages after reconnects, and marking a channel read somehow leaves exactly one message behind.

unread.js folds the event stream into a badge count — two bugs, one function.

Bug report

BUG-1337 · Reported by: mobile team

Event stream contract:

  • { type: "message", id, channel } — ids are unique but MAY be re-delivered
  • { type: "read", channel, upToId } — clears every message with id <= upToId

Observed: badge shows 5 after a reconnect that re-sent 2 messages, and after "mark as read" one unread always remains.

Logs

[badge] general: events=6 unique=4 badge=6
[badge] after read upTo=42 -> badge=1 (expected 0)

The code as shipped

src/chat/unread.js (editable)

// Folds the socket event stream into unread counts per channel.
exports.unreadCounts = function (events) {
  var perChannel = {};

  for (var i = 0; i < events.length; i++) {
    var e = events[i];
    if (e.type === "message") {
      if (!perChannel[e.channel]) perChannel[e.channel] = [];
      perChannel[e.channel].push(e.id);
    } else if (e.type === "read") {
      var list = perChannel[e.channel] || [];
      perChannel[e.channel] = list.filter(function (id) {
        return id > e.upToId + 1;
      });
    }
  }

  var out = {};
  for (var ch in perChannel) out[ch] = perChannel[ch].length;
  return out;
};

Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More JavaScript bug hunts.