-
Notifications
You must be signed in to change notification settings - Fork 579
fix(consumers): prevent await deadlock on ContextCallable failure #2941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
imbajin
merged 5 commits into
apache:master
from
bitflicker64:fix/consumers-latch-deadlock
Jan 26, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8f3578c
fix(consumers): prevent await deadlock on ContextCallable failure
bitflicker64 b7d845d
test(consumers): add unit tests for start/await
bitflicker64 260e4ea
Fix Consumers await() latch deadlock when ContextCallable fails
bitflicker64 0c4a0ce
Add regression test to prevent await() deadlock on ContextCallable fa…
bitflicker64 ec746c9
Update ConsumersTest.java
bitflicker64 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...aph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/util/ConsumersTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.hugegraph.unit.util; | ||
|
|
||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import org.apache.hugegraph.testutil.Assert; | ||
| import org.apache.hugegraph.util.Consumers; | ||
| import org.junit.Test; | ||
|
|
||
| public class ConsumersTest { | ||
|
|
||
| @Test(timeout = 1000) | ||
| public void testStartProvideAwaitNormal() throws Throwable { | ||
| ExecutorService executor = Executors.newFixedThreadPool(2); | ||
| try { | ||
| AtomicInteger processed = new AtomicInteger(); | ||
|
|
||
| Consumers<Integer> consumers = new Consumers<>(executor, v -> { | ||
| processed.incrementAndGet(); | ||
| }); | ||
|
|
||
| consumers.start("test"); | ||
| for (int i = 0; i < 50; i++) { | ||
| consumers.provide(i); | ||
| } | ||
| consumers.await(); | ||
|
|
||
| Assert.assertEquals("Should process all provided elements", | ||
| 50, processed.get()); | ||
| } finally { | ||
| executor.shutdownNow(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Regression test for deadlock: | ||
| * | ||
| * ContextCallable fails before entering runAndDone(). | ||
| * await() must still return because latch is decremented in safeRun(). | ||
| */ | ||
| @Test(timeout = 1000) | ||
| public void testAwaitDoesNotHangWhenContextCallableFails() throws Throwable { | ||
| ExecutorService executor = Executors.newFixedThreadPool(1); | ||
| try { | ||
| // Use AssertionError to bypass the inner catch(Exception) loop in runAndDone() | ||
| // This simulates a scenario where an exception escapes the task logic | ||
| // (similar to how a ContextCallable failure would behave from safeRun's perspective) | ||
| Consumers<Integer> consumers = new Consumers<>(executor, v -> { | ||
| throw new AssertionError("Simulated fatal error (OOM/StackOverflow/etc)"); | ||
| }); | ||
| consumers.start("test-fatal-error"); | ||
| consumers.provide(1); | ||
| // Verification: | ||
| // Without the fix, the latch would never be decremented (because runAndDone crashes), causing await() to hang. | ||
| // With the fix (safeRun wrapper), the finally block ensures latch.countDown() is called. | ||
| consumers.await(); | ||
|
|
||
| // Note: consumer.exception will be null because safeRun only catches Exception, not Error. | ||
| // This is acceptable behavior for fatal errors, as long as it doesn't deadlock. | ||
| } finally { | ||
| executor.shutdownNow(); | ||
| } | ||
| } | ||
bitflicker64 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Test(timeout = 1000) | ||
| public void testAwaitThrowsWhenConsumerThrows() throws Throwable { | ||
| ExecutorService executor = Executors.newFixedThreadPool(2); | ||
| try { | ||
| final String msg = "Injected exception for test"; | ||
|
|
||
| Consumers<Integer> consumers = new Consumers<>(executor, v -> { | ||
| throw new RuntimeException(msg); | ||
| }); | ||
|
|
||
| consumers.start("test"); | ||
| consumers.provide(1); | ||
|
|
||
| try { | ||
| consumers.await(); | ||
| Assert.fail("Expected await() to throw when consumer throws"); | ||
| } catch (Throwable t) { | ||
| Throwable root = t.getCause() != null ? t.getCause() : t; | ||
| Assert.assertTrue("Expected RuntimeException, but got: " + root, | ||
| root instanceof RuntimeException); | ||
| Assert.assertTrue("Exception message should contain injected message", | ||
| root.getMessage() != null && | ||
| root.getMessage().contains(msg)); | ||
| } | ||
| } finally { | ||
| executor.shutdownNow(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.