From a0d00971e654dc75dde1a00b532603a7062b2c51 Mon Sep 17 00:00:00 2001 From: TimurRakhmatullin Date: Mon, 30 Nov 2026 08:31:35 -0800 Subject: [PATCH] Return a completed result from BatchLogRecordProcessor#forceFlush after shutdown Same hang pattern as BatchSpanProcessor: after shutdown() the worker thread has flipped isShutdown and exited. A later call to forceFlush() CASes a fresh flushRequested and offers the signal, but nothing is left to drain it or call succeed() on the returned future. Guard the public forceFlush() with the worker's isShutdown atomic so it returns CompletableResultCode.ofSuccess() when the processor is already shut down. The internal Worker.forceFlush() path that shutdown() itself uses stays untouched, because shutdown() still needs the final flush after it sets 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 the assertion. --- .../logs/export/BatchLogRecordProcessor.java | 11 ++++++++++ .../export/BatchLogRecordProcessorTest.java | 21 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/export/BatchLogRecordProcessor.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/export/BatchLogRecordProcessor.java index de691a77394..a12dd726d3a 100644 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/export/BatchLogRecordProcessor.java +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/export/BatchLogRecordProcessor.java @@ -95,6 +95,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(); } @@ -284,6 +291,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/logs/src/test/java/io/opentelemetry/sdk/logs/export/BatchLogRecordProcessorTest.java b/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/export/BatchLogRecordProcessorTest.java index 1b86ba63dee..9434dabdde7 100644 --- a/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/export/BatchLogRecordProcessorTest.java +++ b/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/export/BatchLogRecordProcessorTest.java @@ -491,6 +491,27 @@ 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. + BatchLogRecordProcessor processor = + BatchLogRecordProcessor.builder(mockLogRecordExporter).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 getLogRecordExporter() { assertThat(