Repository navigation
Conversation
The Stop hook's wall time (median ~28 s) far exceeds its logged work (<1.5 s), and Claude Code waits on it at the end of every turn. Both state locks are released before the flush, so running it async does not cause lock contention. SessionEnd stays synchronous (langfuse#13). 🤖 Generated with Claude Code Co-Authored-By: Claude <claude@anthropic.com>
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.
The
Stophook blocks the end of every turn for far longer than its work takes. This marks itasyncso Claude Code no longer waits on it.Problem
Stopis a synchronous command hook, so Claude Code waits for the process to exit before the turn finishes. On one Linux machine, Claude Code's own hook records (hook_successattachments in the session transcripts) for 42Stopruns over ~5 days show:durationMs)For the same runs
langfuse_hook.logreportsProcessed N turns in 0.15s–1.45s. A colduv run --quiet --script hooks/langfuse_hook.py </dev/nulltakes 0.47 s, andflush_and_shutdown_langfuse_client()caps flush+shutdown at 5 s. Roughly 20+ s per run therefore goes somewhere after the logged work, presumably in interpreter exit or exporter teardown. I have not pinned down where; doing so would have meant instrumenting real exports. Either way, the turn should not wait on it.Fix
Add
"async": trueto theStopcommand hook inhooks/hooks.json.FileLocksections inemit_new_turns_from_transcript()are released beforemain()reachesflush_and_shutdown_langfuse_client()in itsfinally, so the slow tail runs without holding a lock and cannot make an overlapping run time out.emitted_keys, so a run that sees nothing new emits nothing.SessionEndstays synchronous, because of SessionEnd hook is killed during CLI shutdown — "Hook cancelled" shown on every exit #13.Tradeoff: progress is saved before the SDK flush (the known limitation noted beside the second lock, and #103). If Claude Code ends a still-running async
Stopwhen the session exits, that turn's spans can be lost where today the user would have waited for them. Bounding process exit itself, for exampleos._exitafter the capped flush, would shrink that window; I have left it out to keep this change to one line.Testing
Processed N turnsruns at 0.13–0.30 s with nolock timeoutlines, and Claude Code no longer records a blockingStophook for those turns.