Skip to content

fix: propagate control-flow exceptions from async helper thread - #1868

Open
Bortlesboat wants to merge 1 commit into
langfuse:mainfrom
Bortlesboat:fix/propagate-threaded-cancellation
Open

fix: propagate control-flow exceptions from async helper thread#1868
Bortlesboat wants to merge 1 commit into
langfuse:mainfrom
Bortlesboat:fix/propagate-threaded-cancellation

Conversation

@Bortlesboat

@Bortlesboat Bortlesboat commented Sep 8, 2026

Copy link
Copy Markdown

What does this PR do?

run_async_safely now propagates cancellation and exit exceptions when it runs a coroutine in its helper thread. Previously, CancelledError, SystemExit and KeyboardInterrupt escaped the worker while the caller received None, unlike the direct asyncio.run path.

The original exception object reaches the caller in both contexts.

Type of change

  • Bug fix

Verification

python -m pytest tests/unit/test_utils.py -q
python -m pytest tests/unit -q
python -m ruff check .
python -m ruff format --check langfuse/_client/utils.py tests/unit/test_utils.py
python -m mypy langfuse --no-error-summary

All 22 utility tests pass on Python 3.13; the three new threaded cases fail before the fix. Ruff and mypy pass, and the changed files pass formatting.

The full unit suite produced 673 passes, 2 skips, 3 failures and 18 fixture errors on native Windows. A separate untouched checkout of the same base produced the same failures/errors and 667 passes: the failures are two prompt atexit subprocess tests and a Windows path assertion; the errors are prompt-client initialization fixtures. Whole-tree format checking also reports an existing discrepancy in test_media.py. Server and live-provider suites were not run because this bridge behavior is covered without external services.

Checklist

  • I self-reviewed the diff using code_review.md.
  • I added tests for behavior changes.
  • I checked docs, examples and .env.template; the existing exception contract already describes the intended behavior.
  • I did not hand-edit generated files.
  • I did not commit secrets or credentials.

No service deployment or new telemetry is involved in this SDK fix.

Agent-assisted: Codex prepared the patch and ran the local checks listed above.


Compound Engineering

Greptile Summary

This PR fixes run_async_safely so control-flow exceptions raised in its helper thread reach the caller, consistent with its direct execution path.

  • Stores helper-thread failures as BaseException rather than only Exception.
  • Propagates cancellation and process-control exceptions without replacing the original object.
  • Adds parameterized coverage for synchronous and helper-thread execution.

Confidence Score: 5/5

The PR appears safe to merge, with the implementation matching the existing exception contract and direct execution behavior.

No actionable failures remain; the worker is joined before the original captured exception is re-raised, and the new tests cover all control-flow exceptions targeted by the fix.

Reviews (1): Last reviewed commit: "fix: propagate control-flow exceptions f..." | Re-trigger Greptile

@claude claude Bot left a comment

Copy link
Copy Markdown

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.

@CLAassistant

CLAassistant commented Sep 8, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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.

2 participants