r/Backend • • 1d ago

Two quiet bugs that bit our Python webhook and intake pipelines in prod

Sharing two unglamorous bugs from building deterministic Python backends for small operations teams, because neither showed up on clean happy-path test data.

1. Asset ID regex grabbing fault codes and ISO standards

In a bilingual Spanish and English chat intake workflow where plant technicians report machine faults to pull the exact equipment manual section, clean test prompts like CNC-402 spindle overheat parsed fine every time.

Once real shop-floor messages arrived (like fault E-404 per ISO-9001 on CNC-402), our generic alphanumeric asset matcher grabbed E-404 or ISO-9001 first as the machine ID, looked up a nonexistent asset, and missed CNC-402 completely.

The fix needed zero LLM calls on intake:

  • Consume labeled and standalone fault codes before running the asset tag matcher.
  • Keep a small hard blocklist of standards prefixes (ISO, IEC, DIN, OSHA, NFPA, SEC).

2. Webhook retry storms slipping past SELECT-then-INSERT

During brief network blips, checking if a message ID was already processed with a SELECT followed by an INSERT (or an in-memory Python set that resets on container restart) still produced duplicate replies whenever two retries landed in the same millisecond.

Moving deduplication into a single SQLite WAL table keyed on the provider event ID inside a BEGIN IMMEDIATE transaction turned duplicate bursts into a clean sub-millisecond no-op:

BEGIN IMMEDIATE;
INSERT INTO processed_events (event_id, received_at)
VALUES (?, ?)
ON CONFLICT(event_id) DO NOTHING;
COMMIT;

Curious what quiet production edge cases have bitten your own workflows lately.

1 Upvotes

5 comments sorted by

1

u/karolisrusenas 1d ago

Are you checking that the row was actually new? With ON CONFLICT DO NOTHING the insert succeeds either way, so look at cursor.rowcount right after, or changes() if you're on SQLite. Otherwise a dup can still slip through and kick off the work twice. fwiw I built Webhook Relay and its stored requests have caught this exact dup for me.

1

u/Free-Good9203 1d ago

Good catch, and yes, checking if cur.rowcount == 0: return False right after the INSERT ... ON CONFLICT DO NOTHING inside the BEGIN IMMEDIATE transaction is the crucial half of the fix. Without that check, SQLite succeeds silently and the handler still runs twice. Nice that Webhook Relay caught that for you too, stored replayable requests save so much time when debugging duplicate delivery bugs.

1

u/Discordbothost 1d ago

A crash after that commit can mark the event processed before the reply goes out. Kill the worker there and replay the webhook. If the reply disappears, save a pending send in the same transaction and let a worker retry it.

1

u/Free-Good9203 1d ago

Spot on. Marking the inbound event processed without persisting the outbound intent in the same transaction just trades duplicate replies for silently dropped ones if the worker dies right after commit.

Writing the dedup row and a pending outbox row together inside the same BEGIN IMMEDIATE transaction is the clean fix, then a dispatcher claims and sends it and flips the status to sent once the provider acks. The only tricky edge left is a crash mid-HTTP call after the provider accepted the message but before the local sent update lands, where we flag the row as uncertain instead of blindly re-sending on cold outbound.