Repository navigation
Give violations event ids and deliver them before exit - #142
Merged
Merged
Conversation
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().
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Duplicate deliveries. Each violation the reporter buffers now gets a unique
eventId(a UUID). It is assigned inViolationReporter.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:server.close()then natural exitfastify.close()executionCtxpresentwaitUntiltask)Fixes:
onClosehook callssdk.destroy(), sofastify.close()flushes immediately.c.executionCtx.waitUntil, when the runtime has one, through a new optionallifecycleargument onFetchGuard.check(). This is the same mechanism the Next.js adapter already uses.A process ended abruptly (
process.exit(), SIGKILL) still loses what it holds. Callingsdk.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.tsdestroy()delivers its event.hono/middleware.test.ts:app.fetch(req, env, ctx)hands one task toctx.waitUntil, and the event, with its id, reaches the server.fastify/violation-delivery.test.ts: nothing is sent beforeclose(), and the event is delivered byclose().Also checked:
turbo run build lint testpasses for all packages.npm run check:edgepasses. Noprocesslisteners 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.