Spot the bug
This consumer can lose messages on a crash. Which line causes it?
def handle(batch: list[str], db: list[str],commits: list[int], offset: int) -> None:commits.append(offset + len(batch))for msg in batch:db.append(msg.upper())db: list[str] = []commits: list[int] = []handle(["a", "b"], db, commits, 0)print(commits, db)
Hint 1 of 1
What if the process dies inside the loop?
Choose an answer, then open Show answer below to check it.
Show answer
- Answer: line 3
commits.append(offset + len(batch))
The offset is committed before the messages are processed. A crash during the loop leaves them committed but never written: at-most-once. Commit after the loop.