Skip to content

fix(dispatch): persist analytics before exit (os.Exit race)#78

Merged
vishnujayvel merged 1 commit into
mainfrom
fix/dispatch-analytics-race
Jun 13, 2026
Merged

fix(dispatch): persist analytics before exit (os.Exit race)#78
vishnujayvel merged 1 commit into
mainfrom
fix/dispatch-analytics-race

Conversation

@vishnujayvel

Copy link
Copy Markdown
Owner

Summary

Dogfooding caught a real bug: hookwise dispatch almost never persisted analytics. It launched go recordAnalytics(...) then immediately called os.Exit(), which terminates all goroutines — so the SQLite open+insert lost the race with process exit every time. The whole analytics half of hookwise was effectively dark in the live hook path.

Fix

Run recordAnalytics in a goroutine (so a locked/slow DB can't hang the hook forever) but wait up to analyticsRecordTimeout (2s) for it before exiting. Fail-open on timeout (ARCH-1). A WAL insert is single-digit ms, so the wait is invisible in practice.

Verified live (end-to-end)

After wiring hookwise into ~/.claude/settings.json, real PostToolUse hooks now record events — the events table grew 1330 → 1333 from this session's own tool calls. A manual PostToolUse dispatch also persists (POSTPROOF row landed). Before this fix, the identical dispatches recorded nothing. (PreToolUse correctly doesn't create an event row — events are recorded on PostToolUse by design.)

Note on testing

The bug is a process-level race (os.Exit vs goroutine), not unit-testable in package main. The verification is the live end-to-end proof above. Unit/contract gates green.

Found while bringing hookwise live for daily use (the post-Dolt dogfooding effort).

🤖 Generated with Claude Code

Dogfooding revealed PostToolUse analytics almost never persisted: dispatch
launched `go recordAnalytics(...)` then immediately called os.Exit(), which
kills the goroutine before its SQLite open+insert completes — the write lost
the race every time. As a result the events table never grew via the live
hook path.

Now dispatch runs recordAnalytics in a goroutine but WAITS up to
analyticsRecordTimeout (2s) for it before exiting; on timeout it proceeds
fail-open (ARCH-1). A WAL insert is single-digit ms, so the wait is invisible
in practice and only bounds a locked/slow DB so a stuck write can't hang the
tool call.

Verified live: after wiring hookwise into settings.json, real PostToolUse
hooks now record events (events table grew 1330→1333 from this session's
tool calls); a manual PostToolUse dispatch also persists. Before this fix the
same dispatches recorded nothing.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 13, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@vishnujayvel, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 44 minutes and 47 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more credits in the billing tab to continue.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 741c9890-4686-4b29-9313-10a2852de3af

📥 Commits

Reviewing files that changed from the base of the PR and between f07d582 and f827362.

📒 Files selected for processing (1)
  • cmd/hookwise/cmd_dispatch.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/dispatch-analytics-race

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

Copy link
Copy Markdown
Warnings
⚠️

No issue reference found in PR description. Add Closes #XX or No issue.

Generated by 🚫 dangerJS against f827362

@vishnujayvel vishnujayvel merged commit cf0fe93 into main Jun 13, 2026
4 checks passed
@vishnujayvel vishnujayvel deleted the fix/dispatch-analytics-race branch June 13, 2026 18:55
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