See here for the relevant code and what is wrong with it: we first fetch an event from the queue, and then try to find a spot for it in the buffer. That's the wrong way around; we should iterate over the output buffer and only if we are sure we can store an event, then fetch one from the queue.
Cc @tiif