Skip to content

Backport: end the chain span when a link raises, and nest parallel storage spans under it - #234

Merged
howethomas merged 2 commits into
production-build-0e0220a-o6from
howethomas/conserver-trace-context-prod
Dec 3, 2026
Merged

howethomas merged 2 commits into
production-build-0e0220a-o6from
howethomas/conserver-trace-context-prod

Conversation

@howethomas

@howethomas howethomas commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Backport of #233 to the production build branch. The commit is cherry-picked from it and applied cleanly; the only nearby difference on this branch is that the "Started processing vCon" line logs at INFO, and it still does.

A failed chain's span stayed open. process() exited the vcon_processing.<chain> span only on the success path, so after a link raised, the span stayed current in that worker thread and every later chain in the thread became its child, until the process restarted. That span ended with status unset, so failed chains could not be found by status. Parallel storage spans were roots, since pool threads start with an empty context.

Change

  • process() holds the span in a with block and runs the old body as _run_chain(). On failure the span records the exception, takes status ERROR, is detached and ended, and the exception reaches _handle_vcon, which moves the vCon to the DLQ as before.
  • Each parallel storage write runs in contextvars.copy_context(), so its span nests under the chain span.
  • Log lines, metrics and DLQ behavior are unchanged, apart from the debug line "Failed to end span", which went with the hand-rolled exit.

Tests

Same conserver/tests/test_chain_span_context.py as #233, run on this branch. Each test, with its result before the fix and after it:

  • test_failed_chain_detaches_and_ends_its_span_before_handle_vcon_returns: a chain whose link raises leaves no current span in the thread and has ended its chain span when _handle_vcon returns. Before: span 'vcon_processing.failing_chain' is still current in the worker thread. After: passes.
  • test_next_chain_in_the_same_thread_gets_a_root_span_in_a_new_trace: the next chain in that thread gets a root span in a new trace. Before: next chain span has parent e142416228491c2e; expected a root span. After: passes.
  • test_failed_chain_span_is_error_with_the_exception_recorded: the failed chain span is ERROR with the exception recorded. Before: the failed chain span was never ended. After: passes.
  • test_interrupt_from_a_link_still_ends_and_detaches_the_chain_span_on_its_way_out[KeyboardInterrupt] and [SystemExit] (new): the interrupt from a link has ended and detached the chain span by the time it reaches the caller. Status is not checked. Before, on the old manual enter and exit: span 'vcon_processing.failing_chain' is still current in the worker thread, for both. After: both pass.
  • test_egress_context_is_taken_inside_the_chain_span (new): the context stored for an egress list carries the chain span's trace id and span id. Before: passes, since the old code already ran egress inside the span. On a copy that calls the wrap-up after the with block: Expected 'store_context_sync' to have been called once. Called 0 times. After: passes.
  • test_parallel_storage_spans_nest_under_the_chain_span: with parallel storage and two backends, each storage.* span has the chain span as parent and shares its trace. Changed: each fake write now waits on a two-party barrier, so both writes are in flight at once on two pool threads, and the test checks that. Without it the pool reused one thread for both. Before: storage.store_b is a root span. After: passes.
  • test_successful_chain_span_is_ok_with_correlation_attributes: the success path still ends OK with vcon.uuid, tenant.id and call.session_id on the chain span. Before: passes. After: passes.

Without the fix: 6 failed, 2 passed. With it: 8 passed. common/tests/test_otel_correlation.py still passes.

Full suite on the host with Python 3.12 and a local plain Redis: 694 passed, 20 skipped, 8 failed. The 8 failures are all in common/tests/test_api.py: this branch's API stores vCons with RedisJSON (JSON.SET), which plain Redis does not have. The same 8 fail on the branch without this commit. They need Redis Stack, as docker compose run --rm conserver pytest provides.

🤖 Generated with Claude Code

howethomas and others added 2 commits December 2, 2026 22:48
…s under it.

A chain whose link raised never exited its vcon_processing span. The span
stayed attached in the worker thread, so every chain that thread ran next
became its child, and when garbage collection finally closed it, its status
was unset. process() now holds the span in a with block. On failure the span
records the exception, takes status ERROR, is detached and ended, and the
exception reaches _handle_vcon, which moves the vCon to the DLQ as before.

Parallel storage writes ran in pool threads, which start with an empty
context, so each storage span began its own trace. Each write now runs in a
copy of the caller's context and nests under the chain span.

conserver/tests/test_chain_span_context.py covers the failed chain, the next
chain in the same thread, the ERROR status, parallel storage, and the success
path with its correlation attributes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ool threads.

- The context stored for an egress list carries the chain span's trace and
  span ids. Moving the wrap-up outside the chain span makes it fail.
- A KeyboardInterrupt or SystemExit from a link has ended and detached the
  chain span by the time it reaches the caller. The old manual enter and
  exit fails it.
- The parallel storage test holds both writes in flight at once, so they
  run on two pool threads instead of one reused thread, and checks that.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@howethomas
howethomas force-pushed the howethomas/conserver-trace-context-prod branch from 164ed47 to f6943b1 Compare December 3, 2026 03:48
@howethomas
howethomas merged commit 7c18e7b into production-build-0e0220a-o6 Dec 3, 2026
1 check passed
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