diff --git a/core/src/main/java/com/linecorp/armeria/internal/common/AbstractContextAwareFuture.java b/core/src/main/java/com/linecorp/armeria/internal/common/AbstractContextAwareFuture.java index 8774c7e587d..765309b3cd9 100644 --- a/core/src/main/java/com/linecorp/armeria/internal/common/AbstractContextAwareFuture.java +++ b/core/src/main/java/com/linecorp/armeria/internal/common/AbstractContextAwareFuture.java @@ -85,22 +85,38 @@ protected final BiFunction makeContextAwareLoggingException(B return (t, u) -> makeContextAwareLoggingException0(() -> fn.apply(t, u)); } + @SuppressWarnings("MustBeClosedChecker") private void makeContextAwareLoggingException0(Runnable action) { - try (SafeCloseable ignored = context.push()) { - action.run(); + final SafeCloseable handle; + try { + handle = context.push(); } catch (Throwable th) { logger.warn("An error occurred while pushing a context", th); throw th; } + + try { + action.run(); + } finally { + handle.close(); + } } + @SuppressWarnings("MustBeClosedChecker") private V makeContextAwareLoggingException0(Supplier fn) { - try (SafeCloseable ignored = context.push()) { - return fn.get(); + final SafeCloseable handle; + try { + handle = context.push(); } catch (Throwable th) { logger.warn("An error occurred while pushing a context", th); throw th; } + + try { + return fn.get(); + } finally { + handle.close(); + } } @Override