[JAVA-5247] Pruning thread of global PowerOfTwoBufferPool causes Maven exec plugin to block Created: 21/Nov/23  Updated: 11/Dec/23  Resolved: 11/Dec/23

Status: Closed
Project: Java Driver
Component/s: Connection Management
Affects Version/s: 4.6.0, 4.11.1
Fix Version/s: None

Type: Improvement Priority: Unknown
Reporter: Maxime Beugnet Assignee: Unassigned
Resolution: Won't Do Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

try-with-resources with a classic/basic MongoClient works fine in 4.5.0 but doesn't in 4.6.0.

In 4.5.0 I can execute my main class with a simple Maven command:

mvn clean compile exec:java -Dexec.mainClass="com.mongodb.quickstart.transactions.Transactions" -Dmongodb.uri="mongodb://localhost"

In 4.6.0+ I have to add "-Dexec.cleanupDaemonThreads=false" to avoid blocking for 15 seconds + java.lang.IllegalThreadStateException.

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

Reproduced with a local ephemeral single node RS in 7.0.3 using Docker.
OK in 4.5.0.
KO in 4.6.0 and current 4.11.1.

How to Reproduce

docker run --rm -d -p 27017:27017 -h $(hostname) --name mongo mongo:7.0.3 --replSet=RS && sleep 3 && docker exec mongo mongosh --quiet --eval "rs.initiate();"
cd /tmp
git clone git@github.com:mongodb-developer/java-quick-start.git
cd java-quick-start
# Change Java Driver Sync version in pom.xml depending if you want to pass or fail.
mvn clean compile exec:java -Dexec.mainClass="com.mongodb.quickstart.Connection" -Dmongodb.uri="mongodb://localhost"

If version >= 4.6.0 then we get:

[WARNING] thread Thread[BufferPoolPruner-1-thread-1,5,com.mongodb.quickstart.Connection] was interrupted but is still alive after waiting at least 15000msecs
[WARNING] thread Thread[BufferPoolPruner-1-thread-1,5,com.mongodb.quickstart.Connection] will linger despite being asked to die via interruption
[WARNING] NOTE: 1 thread(s) did not finish despite being asked to via interruption. This is not a problem with exec:java, it is a problem with the running code. Although not serious, it should be remedied.
[WARNING] Couldn't destroy threadgroup org.codehaus.mojo.exec.ExecJavaMojo$IsolatedThreadGroup[name=com.mongodb.quickstart.Connection,maxpri=10]
java.lang.IllegalThreadStateException
    at java.lang.ThreadGroup.destroy (ThreadGroup.java:803)
    at org.codehaus.mojo.exec.ExecJavaMojo.execute (ExecJavaMojo.java:319)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:370)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:298)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:77)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:568)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:283)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:226)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:407)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:348)

Can ignore the problem by running like this:

mvn clean compile exec:java -Dexec.mainClass="com.mongodb.quickstart.Connection" -Dmongodb.uri="mongodb://localhost" -Dexec.cleanupDaemonThreads=false

 

$ java --version 
openjdk 17.0.9 2023-10-17
OpenJDK Runtime Environment (build 17.0.9+9-Debian-2)
OpenJDK 64-Bit Server VM (build 17.0.9+9-Debian-2, mixed mode, sharing)

 

$ mvn --version 
Apache Maven 3.8.7
Maven home: /usr/share/maven
Java version: 17.0.9, vendor: Debian, runtime: /usr/lib/jvm/java-17-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "6.5.0-4-amd64", arch: "amd64", family: "unix"



 Comments   
Comment by Jeffrey Yemin [ 21/Nov/23 ]

Conclusion in Slack thread is that this is working as designed. Given the way that the Maven exec plugin interacts with ScheduledExecutorService, and the fact that we have a global, prunable buffer pool, there is no way to fix this unless we get rid of the global buffer pool or at least don't prune it. This seems less desirable than the current behavior.

Comment by Jeffrey Yemin [ 21/Nov/23 ]

It's the global buffer pool pruner which seems to be the issue (and also a Log4J thread?):

[WARNING] thread Thread[#35,Log4j2-TF-2-Scheduled-1,5,com.mongodb.quickstart.Connection] was interrupted but is still alive after waiting at least 15000msecs
[WARNING] thread Thread[#35,Log4j2-TF-2-Scheduled-1,5,com.mongodb.quickstart.Connection] will linger despite being asked to die via interruption
[WARNING] thread Thread[#36,BufferPoolPruner-1-thread-1,5,com.mongodb.quickstart.Connection] will linger despite being asked to die via interruption

Generated at Thu Feb 08 09:04:06 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.