Skip to content

update claude code tracing to not block claude from finishing#2841

Open
markjm wants to merge 3 commits intolangfuse:mainfrom
markjm:markjm/background
Open

update claude code tracing to not block claude from finishing#2841
markjm wants to merge 3 commits intolangfuse:mainfrom
markjm:markjm/background

Conversation

@markjm
Copy link
Copy Markdown

@markjm markjm commented Apr 22, 2026

When we run this stopping hook, claude does not finish until the hook is complete (and the traces are flushed). This will prevent claude from starting to process the next prompt.

This change updates the script to how we have been using it to spin off the flush into the background so claude can quickly process the stop hook and then keep churning to the next prompt.

There is an env variable to control this so the hook is debuggable by unbackgrounding it

Figured id submit this upstream because its been valuable to us but if its not of interest to include, no worries!

Disclaimer: Experimental PR review

Greptile Summary

This PR updates the Claude Code Langfuse Stop-hook script to fork a background child process (os.fork() + os.setsid()) before doing the heavy Langfuse flush work, so the parent exits immediately and no longer blocks Claude Code from processing the next prompt. A new CC_LANGFUSE_BACKGROUND env var (default \"true\") lets users opt back into synchronous mode for debugging.

Confidence Score: 4/5

Safe to merge after addressing the os.fork() Windows/WSL portability gap, or after confirming the target audience is Unix-only.

The forking logic is correct on Unix — parent exits immediately, child detaches with setsid() and runs to completion. The only open concern is that os.fork() does not exist on Windows, and with BACKGROUND=true as the default, this will crash the hook rather than silently fall back to sync mode. A one-line hasattr guard resolves it. All other findings are P2 or below.

content/integrations/other/claude-code.mdx — the os.fork() call at line 560 needs a hasattr(os, 'fork') guard.

Important Files Changed

Filename Overview
content/integrations/other/claude-code.mdx Adds os.fork() + os.setsid() to background the Stop-hook flush; defaults CC_LANGFUSE_BACKGROUND=true. os.fork() is Unix-only and will raise AttributeError on Windows, crashing the hook instead of falling back to sync mode.

Sequence Diagram

sequenceDiagram
    participant CC as Claude Code
    participant Parent as Hook Process (parent)
    participant Child as Forked Child

    CC->>Parent: Run Stop hook
    Parent->>Parent: Validate keys, session, transcript
    alt BACKGROUND=true (default)
        Parent->>Child: os.fork()
        Parent-->>CC: return 0 (exits immediately)
        Child->>Child: os.setsid() (detach session)
        Child->>Child: Langfuse.init()
        Child->>Child: FileLock → read transcript → emit turns
        Child->>Child: langfuse.flush()
        Child->>Child: langfuse.shutdown()
        Child-->>Child: exit
    else BACKGROUND=false (debug)
        Parent->>Parent: Langfuse.init()
        Parent->>Parent: FileLock → read transcript → emit turns
        Parent->>Parent: langfuse.flush()
        Parent->>Parent: langfuse.shutdown()
        Parent-->>CC: return 0
    end
Loading
Prompt To Fix All With AI
This is a comment left during a code review.
Path: content/integrations/other/claude-code.mdx
Line: 560-567

Comment:
**`os.fork()` unavailable on Windows/WSL**

`os.fork()` raises `AttributeError` on Windows (including some WSL configurations). With `BACKGROUND` defaulting to `"true"`, any user on such a platform will get an unhandled exception that crashes the hook instead of a graceful no-op. A guard like `if hasattr(os, 'fork'):` (falling back to synchronous execution) would protect against this:

```suggestion
    # Fork to run in background - parent exits immediately, child does the work
    if BACKGROUND and hasattr(os, "fork"):
        pid = os.fork()
        if pid > 0:
            # Parent: exit immediately so Claude Code isn't blocked
            debug(f"Forked child {pid}, parent exiting")
            return 0
        # Child: detach from parent session
        os.setsid()
        debug("Child process running in background")
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "update claude code tracing to not block ..." | Re-trigger Greptile

Greptile also left 1 inline comment on this PR.

When we run this stopping hook, claude does not finish until the hook is complete (and the traces are flushed). This will prevent claude from starting to process the next prompt.

This change updates the script to how we have been using it to spin off the flush into the background so claude can quickly process the stop hook and then keep churning to the next prompt.

There is an env variable to control this so the hook is debuggable by unbackgrounding it

Figured id submit this upstream because its been valuable to us but if its not of interest to include, no worries!
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 22, 2026

@markjm is attempting to deploy a commit to the langfuse Team on Vercel.

A member of the Team first needs to authorize it.

@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Apr 22, 2026
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 22, 2026

CLA assistant check
All committers have signed the CLA.

@dosubot dosubot Bot added the documentation Improvements or additions to documentation label Apr 22, 2026
Comment thread content/integrations/other/claude-code.mdx
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Comment thread content/integrations/other/claude-code.mdx Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants