Skip to content

Give violations event ids and deliver them before exit - #142

Merged
cport1 merged 1 commit into
mainfrom
fix/1945-violation-event-id
Nov 27, 2026
Merged

cport1 merged 1 commit into
mainfrom
fix/1945-violation-event-id

Conversation

@cport1

@cport1 cport1 commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Duplicate deliveries. Each violation the reporter buffers now gets a unique eventId (a UUID). It is assigned in ViolationReporter.report(), and an id the caller already set is kept. Ingest uses the id to store an event once, so a batch that reaches it twice adds no second violation and no second tripwire detection. Ingest still acknowledges the repeat, so the reporter's counters stay correct. Older ingest ignores the field, so this is safe to release on its own.

Local exits. Buffered violations were lost when an app shut down before the next flush. The reporter's interval is unref'd, and the adapters own a private SDK instance that the app cannot destroy(). A loopback repro took one enforced tripwire hit (HTTP 403) per adapter, shut down normally, and counted what reached ingest:

Adapter, shutdown Before After
Express, server.close() then natural exit 0 1
Fastify, fastify.close() 0 1
Hono on Node, natural exit 0 1
Hono on Workers, executionCtx present 0 (no waitUntil task) 1

Fixes:

  • Reporter: while events are waiting, it arms a one-shot ref'd timer, so a natural exit waits at most one flush interval plus the request timeout. The timer is cancelled once the buffer drains. It is never re-armed while the client is paused, so an outage cannot hold a process open.
  • Fastify: an onClose hook calls sdk.destroy(), so fastify.close() flushes immediately.
  • Hono: the middleware passes c.executionCtx.waitUntil, when the runtime has one, through a new optional lifecycle argument on FetchGuard.check(). This is the same mechanism the Next.js adapter already uses.

A process ended abruptly (process.exit(), SIGKILL) still loses what it holds. Calling sdk.destroy() remains the explicit path for that.

Tests

Each test below fails when its fix is removed (checked by reverting each one) and passes with it.

  • violation-reporter.test.ts
    • Events get distinct v4 ids, a preset id is kept, and the caller's object is not mutated.
    • A child process that exits without destroy() delivers its event.
    • A child pointed at an unreachable ingest still exits quickly.
  • hono/middleware.test.ts: app.fetch(req, env, ctx) hands one task to ctx.waitUntil, and the event, with its id, reaches the server.
  • fastify/violation-delivery.test.ts: nothing is sent before close(), and the event is delivered by close().

Also checked:

  • turbo run build lint test passes for all packages.
  • npm run check:edge passes. No process listeners were added.

The companion ingest change in the app repo stores events once per id. Ingest should be deployed before this is released, so the ids take effect as soon as clients send them.

Each reported violation now carries a unique eventId so ingest can store
it once when a batch arrives twice.

Buffered violations were lost when an app shut down: the reporter's
interval is unref'd, so Express after server.close(), Fastify after
close() and Hono on Node exited with the last events unsent, and Hono on
Workers handed nothing to waitUntil. The reporter now arms a one-shot
ref'd timer while events are waiting (never while the client is paused),
the Fastify plugin flushes on onClose, and the Hono middleware passes
executionCtx.waitUntil through FetchGuard.check().
@cport1
cport1 merged commit 18fe3e0 into main Nov 27, 2026
2 checks passed
@cport1 cport1 mentioned this pull request Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant