Skip to content

Return a completed result from BatchSpanProcessor#forceFlush after shutdown - #9082

Open
TimurRakhmatullin86 wants to merge 1 commit into
open-telemetry:mainfrom
TimurRakhmatullin86:fix/bsp-force-flush-after-shutdown
Open

TimurRakhmatullin86 wants to merge 1 commit into
open-telemetry:mainfrom
TimurRakhmatullin86:fix/bsp-force-flush-after-shutdown

Conversation

@TimurRakhmatullin86

Copy link
Copy Markdown
Contributor

Summary

BatchSpanProcessor.forceFlush() invoked after shutdown() returns a CompletableResultCode that never completes.

Call sequence

  1. shutdown() runs, which calls worker.shutdown(). Worker flips isShutdown to true, performs its final flush, and sets continueWork = false. The worker thread's while (continueWork) loop exits.
  2. A caller invokes processor.forceFlush() afterwards. This delegates to worker.forceFlush(), which CASes flushRequested from null to a fresh CompletableResultCode and signal.offer(true).
  3. No thread is left to read flushRequested or call succeed() on it. The future stays pending.
  4. Callers that block on it (join(), whenComplete(...).join(...)) wait until their own user-supplied timeout.

This pattern bites users who flush on shutdown hooks or trigger forceFlush() from a cleanup path that runs after a programmatic shutdown().

Fix

Guard the public forceFlush() on worker.isShutdown:

@Override
public CompletableResultCode forceFlush() {
  if (worker.isShutdown()) {
    return CompletableResultCode.ofSuccess();
  }
  return worker.forceFlush();
}
  • Mirrors the no-op success behavior that Worker.shutdown() already uses when called twice.
  • Does not touch the internal Worker.forceFlush() path that Worker.shutdown() itself calls — the shutdown routine still runs its final flush even after it sets isShutdown = true.

Test

forceFlushAfterShutdownCompletes shuts the processor down, then calls forceFlush() and verifies the returned result is already done and successful. Without the fix the result stays pending and the test blocks on join() until its own timeout.

@TimurRakhmatullin86
TimurRakhmatullin86 requested a review from a team as a code owner November 30, 2026 16:29
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-11-30 16:57 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.
  • Should this be with the author? Comment /dashboard route:author to route it back.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

…utdown

After shutdown() the worker thread has flipped isShutdown and exited.
A later call to forceFlush() would CAS a fresh flushRequested and
offer a signal, but nothing is left to drain it or call succeed()
on the returned future, so callers that block on it (for example
join()) wait until their own user-supplied timeout.

Guard forceFlush() with the worker's isShutdown atomic so it returns
CompletableResultCode.ofSuccess() when the processor is already shut
down. Mirrors the no-op success behavior already used by the
shutdown re-entry guard. The internal Worker.forceFlush() path that
shutdown() uses itself stays untouched, because shutdown() still
needs the final flush even after it set isShutdown.

Test: shutdown() first, then processor.forceFlush() returns an
already-done successful result. Without the fix the result stays
pending and the test blocks on join().
@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.53%. Comparing base (2aec7ba) to head (d48cb86).

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #9082   +/-   ##
=========================================
  Coverage     90.52%   90.53%           
- Complexity    11243    11244    +1     
=========================================
  Files          1039     1039           
  Lines         30678    30681    +3     
  Branches       4015     4015           
=========================================
+ Hits          27772    27776    +4     
  Misses         1928     1928           
+ Partials        978      977    -1     

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

This branch has not been deployed

No deployments
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