Skip to content

Conversation

@zaka634
Copy link

@zaka634 zaka634 commented Oct 23, 2025

Description

Attempt fix for the 2357

The context switching is causing problems, for whatever reason, the grpc streaming interceptor flow is triggered, altho most of the time when the error pop up, there is no streaming endpoint involved, the open source lib's interceptor seems having a genuine problem for handling context switching:

  1. It use the with syntax for entering context manager, opening a span which internally create context token via the context var.
  2. Inside the with block, it yields a generator to the client side, which yield the control to the client side.
  3. When client consumes the generator, it may be in a different thread or context or even be recycled, which caused the issue.

The fix here is to manually create the span and end it as necessary, thus there is no context manager and context token involved at all. Tested in production for my service, with the fix, there is no such error popes up again.

Fixes #2357

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

There is no easy way to reproduce this, the error is constantly and consistently showing up in my service, after this patch, the error is gone and all the tracing functions as before.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Oct 23, 2025

CLA Not Signed

@xrmx
Copy link
Contributor

xrmx commented Oct 31, 2025

@zaka634 thanks for the PR but you need to sign the CLA in order to contribute to OpenTelemetry.

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.

"Failed to detach context" exception for grpc instrumentation

3 participants