You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@curator.apache.org by "ASF GitHub Bot (Jira)" <ji...@apache.org> on 2022/06/15 11:44:00 UTC
[jira] [Work logged] (CURATOR-640) IllegalThreadStateException thrown when access PathChildrenCache in different thread groups
[ https://issues.apache.org/jira/browse/CURATOR-640?focusedWorklogId=781606&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-781606 ]
ASF GitHub Bot logged work on CURATOR-640:
------------------------------------------
Author: ASF GitHub Bot
Created on: 15/Jun/22 11:43
Start Date: 15/Jun/22 11:43
Worklog Time Spent: 10m
Work Description: eolivelli commented on code in PR #419:
URL: https://github.com/apache/curator/pull/419#discussion_r897876484
##########
curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestPathChildrenCache.java:
##########
@@ -1151,4 +1151,45 @@ public void invoke() throws Exception
TestCleanState.closeAndTestClean(client);
}
}
+
+ @Test
+ public void testIsolatedThreadGroup() throws Exception {
+ AtomicReference<Throwable> exception = new AtomicReference<>();
+
+ CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), 30000, 30000, new RetryOneTime(1));
+ client.getUnhandledErrorListenable().addListener((message, e) -> exception.set(e));
+ client.start();
+
+ ThreadGroup threadGroup1 = new ThreadGroup("testGroup1");
+ Thread thread1 = new Thread(threadGroup1, () -> {
+ try ( final PathChildrenCache cache = new PathChildrenCache(client, "/test1", true) ) {
+ cache.start();
+ Thread.sleep(1000);
+ } catch (Exception e) {
+ exception.set(e);
+ }
+ });
+
+ thread1.start();
+ thread1.join();
+ assertNull(exception.get());
Review Comment:
nit: I suggest to use CompletableFuture, not a big deal in any case
Issue Time Tracking
-------------------
Worklog Id: (was: 781606)
Time Spent: 40m (was: 0.5h)
> IllegalThreadStateException thrown when access PathChildrenCache in different thread groups
> -------------------------------------------------------------------------------------------
>
> Key: CURATOR-640
> URL: https://issues.apache.org/jira/browse/CURATOR-640
> Project: Apache Curator
> Issue Type: Bug
> Components: Recipes
> Affects Versions: 5.2.1
> Reporter: Xiaogang Shi
> Assignee: Zili Chen
> Priority: Minor
> Time Spent: 40m
> Remaining Estimate: 0h
>
> When we create PathChildrenCache instances without specifying thead factory, the {{defaultThreadFactory}} will be used to create threads. The {{defaultThreadFactory}} is typically backed by {{java.util.concurrent.DefaultThreadFactory}} which will add newly created threads into the thread group where it is instantiated. That is, the {{defaultThreadFactory}} will always add newly created threads into the first thread group that loads the {{PathChildrenCache}} class.
> In cases where {{PathChildrenCache}} is accessed in different thread groups, if the first thread group that load the {{PathChildrenCache}} is destroy, then all attempts to start the {{PathChildrenCache}} will fail.
> The problem can be reproduced using the following code:
> {code:java}
> ThreadGroup threadGroup1 = new ThreadGroup("testGroup1");
> Thread thread1 = new Thread(threadGroup1, () -> {
> PathChildrenCache cache1 =
> new PathChildrenCache(client, "/test1", true);
> try {
> cache1.start();
> Thread.sleep(1000);
> } catch (Throwable t) {
> t.printStackTrace();
> } finally {
> try {
> cache1.close();
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
> });
> thread1.start();
> thread1.join();
> // After the thread group is destroyed, all PathChildrenCache instances
> // will fail to start due to inability to add new threads into the first thread group
> threadGroup1.destroy();
> ThreadGroup threadGroup2 = new ThreadGroup("testGroup2");
> Thread thread2 = new Thread(threadGroup2, () -> {
> PathChildrenCache cache2 =
> new PathChildrenCache(client, "/test1", true);
> try {
> cache2.start();
> Thread.sleep(1000);
> } catch (Throwable t) {
> t.printStackTrace();
> } finally {
> try {
> cache2.close();
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
> });
> thread2.start();
> thread2.join();
> {code}
> When starting {{cache2}}, the following exception is thrown:
> {code}
> java.lang.IllegalThreadStateException
> at java.base/java.lang.ThreadGroup.addUnstarted(ThreadGroup.java:892)
> at java.base/java.lang.Thread.<init>(Thread.java:434)
> at java.base/java.lang.Thread.<init>(Thread.java:708)
> at java.base/java.util.concurrent.Executors$DefaultThreadFactory.newThread(Executors.java:660)
> at org.apache.curator.shaded.com.google.common.util.concurrent.ThreadFactoryBuilder$1.newThread(ThreadFactoryBuilder.java:163)
> at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.<init>(ThreadPoolExecutor.java:630)
> at java.base/java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:920)
> at java.base/java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1353)
> at java.base/java.util.concurrent.Executors$DelegatedExecutorService.execute(Executors.java:721)
> at org.apache.curator.utils.CloseableExecutorService.submit(CloseableExecutorService.java:191)
> at org.apache.curator.framework.recipes.cache.PathChildrenCache.submitToExecutor(PathChildrenCache.java:841)
> at org.apache.curator.framework.recipes.cache.PathChildrenCache.offerOperation(PathChildrenCache.java:792)
> at org.apache.curator.framework.recipes.cache.PathChildrenCache.start(PathChildrenCache.java:300)
> at org.apache.curator.framework.recipes.cache.PathChildrenCache.start(PathChildrenCache.java:239)
> {code}
--
This message was sent by Atlassian Jira
(v8.20.7#820007)