Skip to content

fix(observe): defer span end when a cancellation never resumed the generator - #1792

Merged
hassiebp merged 5 commits into
langfuse:mainfrom
DavidTraina:fix/defer-span-end-for-unresumed-cancellation
Jul 30, 2026
Merged

fix(observe): defer span end when a cancellation never resumed the generator#1792
hassiebp merged 5 commits into
langfuse:mainfrom
DavidTraina:fix/defer-span-end-for-unresumed-cancellation

Conversation

@DavidTraina

@DavidTraina DavidTraina commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Alternative to #1791 — same bug, see there for details and a repro. Maintainers should pick one; I'll close the other.

Includes the same core fix: aclose()/close() always close the wrapped generator, even if its span already ended, plus the same except TypeError narrowing.

On top of that: when a cancellation ends iteration before the generator ever resumed, don't end the span right away. Hold the error, close the generator first — so span updates in its finally still land — then end the span with the held error. The span outcome is the same ERROR/"CancelledError" as today; the difference is the generator's final updates are no longer dropped.

Scope: async generators only, CancelledError only, and only when the generator provably never resumed. Works on Python 3.10+ (inspect.getasyncgenstate is 3.12+, so it falls back to the ag_frame/ag_running attributes it reads).

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Refactor
  • Documentation update
  • Tooling, CI, or repo maintenance

Verification

uv run --frozen pytest -n auto --dist worksteal tests/unit   # also run on 3.10 and 3.11
uv run --frozen ruff check .
uv run --frozen ruff format --check .
uv run --frozen mypy langfuse --no-error-summary

Checklist

  • I self-reviewed the diff using code_review.md.
  • I added or updated tests for behavior changes.
  • I updated docs, examples, or .env.template if needed.
  • I did not hand-edit generated files; if generated files changed, I used the upstream regeneration path.
  • I did not commit secrets or credentials.

@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.

Comment thread langfuse/_client/observe.py Outdated
@hassiebp
hassiebp merged commit 9239fab into langfuse:main Jul 30, 2026
15 of 18 checks passed
@hassiebp

Copy link
Copy Markdown
Collaborator

Thanks for your contribution, @DavidTraina !

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