Skip to content

Run CompletableResultCode completion action outside the lock - #8773

Open
amit306 wants to merge 1 commit into
open-telemetry:mainfrom
amit306:fix/8771-completable-result-code-callbacks
Open

Run CompletableResultCode completion action outside the lock#8773
amit306 wants to merge 1 commit into
open-telemetry:mainfrom
amit306:fix/8771-completable-result-code-callbacks

Conversation

@amit306

@amit306 amit306 commented Sep 6, 2026

Copy link
Copy Markdown

Fixes #8771

Problem

CompletableResultCode#succeed and failInternal run completion actions while holding a lock. This can cause a deadlock if two actions try to complete each other. Also, if an action throws an exception, the remaining actions are skipped and the result may never complete.

Solution

Both methods now use a shared complete method. It updates the result and copies the actions while holding the lock, then releases the lock before running them. runActions logs each exception, continues running the remaining actions, and rethrows the first exception at the end. whenComplete also uses runActions when the result is already complete.

@amit306
amit306 requested a review from a team as a code owner September 6, 2026 19:17
@codecov

codecov Bot commented Sep 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 91.28%. Comparing base (c87b50e) to head (e3c69b7).
⚠️ Report is 31 commits behind head on main.

Files with missing lines Patch % Lines
...pentelemetry/sdk/common/CompletableResultCode.java 96.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #8773      +/-   ##
============================================
- Coverage     91.29%   91.28%   -0.01%     
- Complexity    10498    10533      +35     
============================================
  Files          1006     1008       +2     
  Lines         28338    28476     +138     
  Branches       3581     3620      +39     
============================================
+ Hits          25870    25994     +124     
- Misses         1675     1685      +10     
- Partials        793      797       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Sep 6, 2026

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-09-13 22:30 UTC

Review the latest changes.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@amit306
amit306 force-pushed the fix/8771-completable-result-code-callbacks branch from 28f8686 to ee78eb3 Compare September 6, 2026 21:41
@jack-berg

Copy link
Copy Markdown
Member

Another option is to run all actions first and then throw the first exception. This would fix the issue while keeping the current behavior of succeed. Let me know your opinion.

We should do this, plus logging for all exceptions. Also, let's catch Exception, not Throwable. That will mean that errors (i.e. Throwables which are not Exceptions) bubble up and violate the "allOf" completion guarantee. But this seems fine and defensible.

Also, your PR description is too verbose. The key details are watered down by the noise of details like which specific test cases were added, which is self explanatory from the code. Less is more. I recommend writing PR descriptions/comments yourself, rather than relying on AI.

@amit306

amit306 commented Sep 10, 2026

Copy link
Copy Markdown
Author

Another option is to run all actions first and then throw the first exception. This would fix the issue while keeping the current behavior of succeed. Let me know your opinion.

We should do this, plus logging for all exceptions. Also, let's catch Exception, not Throwable. That will mean that errors (i.e. Throwables which are not Exceptions) bubble up and violate the "allOf" completion guarantee. But this seems fine and defensible.

Also, your PR description is too verbose. The key details are watered down by the noise of details like which specific test cases were added, which is self explanatory from the code. Less is more. I recommend writing PR descriptions/comments yourself, rather than relying on AI.

thanks @jack-berg for comment. I have updated the code and description. Please check

}

@Test
void completionActionExceptionDoesNotEscapeWhenAlreadyComplete() {

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.

Test name is inaccurate - it not asserts the exception does escape.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

yes, missed to change. rename it

/**
* Perform an action on completion. Actions are guaranteed to be called only once.
* Perform an action on completion. Actions are guaranteed to be called only once. Actions are not
* invoked while internal locks are held. Every action runs even if an earlier one throws. Each

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.

Suggested change
* invoked while internal locks are held. Every action runs even if an earlier one throws. Each
* invoked while internal locks are held. Every action runs even if an earlier one throws a RuntimeException. Each

Give a hint that its only runtime exceptions that are caught

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

Fixes open-telemetry#8771

Signed-off-by: amit306 <amit.anand0312@gmail.com>
@amit306
amit306 force-pushed the fix/8771-completable-result-code-callbacks branch from 4afc49d to e3c69b7 Compare September 12, 2026 20:58
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.

CompletableResultCode completion callbacks can deadlock or prevent aggregate completion

2 participants