Goddess Commits / blog

Fixing dropped SSE events on disconnect with Last-Event-ID replay

1 min readTechserver-sent-eventssselast-event-idrace-conditionbackend

The Problem

I hit a bug where my single-consumer Server-Sent Events queue silently dropped the final done event whenever a client disconnected. Worse, the disconnect logic left orphaned generators that would pop the next event from the queue and crash trying to write it to a dead connection.

The SSE queue was a single-consumer channel with no replay. A dropped client left an orphaned generator that popped the next event off the queue and died trying to write it.

The Fix

I replaced the naive queue logic with a broadcast condition. This wakes all waiting clients when a new event arrives, and more importantly, allows me to support Last-Event-ID. Clients now get lossless replays on reconnect instead of silent gaps in their stream.

Written from a live stream transcript with AI help, reviewed by the author.