Skip to content

fix(grpc): handle NotImplementedError from add_done_callback in aio client#4429

Open
alliasgher wants to merge 5 commits into
open-telemetry:mainfrom
alliasgher:fix-grpc-aio-add-done-callback
Open

fix(grpc): handle NotImplementedError from add_done_callback in aio client#4429
alliasgher wants to merge 5 commits into
open-telemetry:mainfrom
alliasgher:fix-grpc-aio-add-done-callback

Conversation

@alliasgher

@alliasgher alliasgher commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

Description

When a user-provided grpc.aio interceptor awaits the call object, the resulting type may not implement add_done_callback — calling it raises NotImplementedError. Catch the exception and invoke the callback immediately with the already-available code and details, which matches the synchronous interceptor path and keeps span finalisation working.

Fixes #3915

Type of change

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

How Has This Been Tested?

Added test_done_callback_not_implemented in test_aio_client_interceptor.py which replaces add_done_callback with a function that raises NotImplementedError and asserts the span still completes with the correct status.

Checklist

@xrmx

xrmx commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

This is missing a test for checking this is working fine now and conflict handling in the CHANGELOG

@alliasgher alliasgher force-pushed the fix-grpc-aio-add-done-callback branch from fe37463 to c788d60 Compare April 14, 2026 08:46
@alliasgher

Copy link
Copy Markdown
Contributor Author

I think the comment about #47040 was meant for a different PR (it's a collector-contrib PR fixing the azurefunctions config round-trip, unrelated to this grpc aio fix). Happy to proceed here if that's correct.

@alliasgher

Copy link
Copy Markdown
Contributor Author

Added a regression test in 2555c4d that patches grpc.aio.UnaryUnaryCall.add_done_callback to raise NotImplementedError and verifies the span is still correctly finished with all expected attributes.

@MikeGoldsmith MikeGoldsmith left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Change looks good. I've left a suggestion for updating the changelog entry.

Also, please can you update the PR description to use the template we have for consistency.

Comment thread CHANGELOG.md Outdated
alliasgher added a commit to alliasgher/opentelemetry-python-contrib that referenced this pull request Apr 15, 2026
@alliasgher alliasgher force-pushed the fix-grpc-aio-add-done-callback branch from 3b9d229 to f21c4a7 Compare April 15, 2026 20:45

@MikeGoldsmith MikeGoldsmith left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good - thanks @alliasgher.

@github-project-automation github-project-automation Bot moved this from Reviewed PRs that need fixes to Approved PRs in Python PR digest Apr 15, 2026
@github-actions

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment.
If you're still working on this, please add a comment or push new commits.

@alliasgher

Copy link
Copy Markdown
Contributor Author

Looks good - thanks @alliasgher.

@MikeGoldsmith approved but still marked stale

@github-actions github-actions Bot removed the Stale label May 1, 2026
@emdneto

emdneto commented May 12, 2026

Copy link
Copy Markdown
Member

Thanks for the PR!

Just a heads-up: we no longer update CHANGELOG.md directly. The changelog is now generated from changelog fragments using Towncrier.

Please add the appropriate changelog fragment for this change instead of editing CHANGELOG.md manually. You can find the instructions and expected format in CONTRIBUTING.md.

@aabmass aabmass moved this from Approved PRs to Reviewed PRs that need fixes in Python PR digest May 20, 2026
@github-actions

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment.
If you're still working on this, please add a comment or push new commits.

@cschanhniem

Copy link
Copy Markdown

Superseded by #4672 - fixed formatting and lint issues.

@github-actions github-actions Bot removed the Stale label Jun 9, 2026
alliasgher and others added 4 commits June 12, 2026 20:25
…lient

When a user provides a custom grpc.aio interceptor that awaits the call
object (e.g. `call = await continuation(...); return await call`), the
resulting call object may not implement add_done_callback, raising
NotImplementedError.

Catch the exception and invoke the callback immediately in that case.
The code and details are already available at that point so the span
can still be finalised correctly.

Fixes open-telemetry#3915

Signed-off-by: alliasgher <alliasgher123@gmail.com>
Verifies that spans are correctly finished when the grpc.aio call object
raises NotImplementedError from add_done_callback, exercising the
fallback path that calls the callback directly.

Signed-off-by: alliasgher <alliasgher123@gmail.com>
CHANGELOG.md is generated by towncrier now; replace the manual edit
with .changelog/4429.fixed per CONTRIBUTING.md.

Signed-off-by: Ali <alliasgher123@gmail.com>
@alliasgher alliasgher force-pushed the fix-grpc-aio-add-done-callback branch from a7747b7 to e3cec30 Compare June 12, 2026 15:26
@alliasgher

Copy link
Copy Markdown
Contributor Author

Thanks @emdneto. Rebased onto main and moved the changelog entry to a towncrier fragment (.changelog/4429.fixed) in e3cec30. Note this PR is still active; #4672 appears to be a copy of this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Reviewed PRs that need fixes

Development

Successfully merging this pull request may close these issues.

opentelemetry-instrumentation-grpc: Client unary call throws NotImplementedError

6 participants