From d48cb8605a1344a76261b9dfebb38a7bf9e3dfa6 Mon Sep 17 00:00:00 2001 From: TimurRakhmatullin Date: Mon, 30 Nov 2026 08:29:52 -0800 Subject: [PATCH] Return a completed result from BatchSpanProcessor#forceFlush after shutdown 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(). --- .../sdk/trace/export/BatchSpanProcessor.java | 11 ++++++++++ .../trace/export/BatchSpanProcessorTest.java | 20 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/export/BatchSpanProcessor.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/export/BatchSpanProcessor.java index 0480cb09bbf..a5b41fe78ed 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/export/BatchSpanProcessor.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/export/BatchSpanProcessor.java @@ -131,6 +131,13 @@ public CompletableResultCode shutdown() { @Override public CompletableResultCode forceFlush() { + // Once shutdown has flipped the worker's isShutdown the worker thread is on its way out + // (or already exited). A fresh forceFlush would install a flushRequested that nothing is + // left to complete, and callers blocking on the returned result would wait forever. + // Mirror the no-op success behavior already used by the shutdown re-entry guard. + if (worker.isShutdown()) { + return CompletableResultCode.ofSuccess(); + } return worker.forceFlush(); } @@ -331,6 +338,10 @@ private CompletableResultCode forceFlush() { return possibleResult == null ? CompletableResultCode.ofSuccess() : possibleResult; } + private boolean isShutdown() { + return isShutdown.get(); + } + private void exportCurrentBatch() { if (batch.isEmpty()) { return; diff --git a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/export/BatchSpanProcessorTest.java b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/export/BatchSpanProcessorTest.java index 2f1b3d005e4..f4b197f54f7 100644 --- a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/export/BatchSpanProcessorTest.java +++ b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/export/BatchSpanProcessorTest.java @@ -687,6 +687,26 @@ void shutdownPropagatesFailure() { assertThat(result.isSuccess()).isFalse(); } + @Test + void forceFlushAfterShutdownCompletes() { + // forceFlush() invoked after shutdown used to install a flushRequested that no live worker + // could complete, so callers blocking on the returned future would hang until their own + // timeout. The processor should now return an already-successful result instead. + BatchSpanProcessor processor = BatchSpanProcessor.builder(mockSpanExporter).build(); + + CompletableResultCode shutdown = processor.shutdown(); + shutdown.join(5, TimeUnit.SECONDS); + assertThat(shutdown.isDone()).isTrue(); + + CompletableResultCode flush = processor.forceFlush(); + assertThat(flush.isDone()) + .withFailMessage( + "forceFlush() after shutdown must return an already-completed result; otherwise" + + " callers block on a future the dead worker cannot complete") + .isTrue(); + assertThat(flush.isSuccess()).isTrue(); + } + @Test void getSpanExporter() { assertThat(BatchSpanProcessor.builder(mockSpanExporter).build().getSpanExporter())