influxdata / influxdata/influxdb-java

InfluxDB Client prevents application from exiting when batch mode is enabled

Open
#762 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.2k
Forks
469
PR merge metrics
No merged PRs in 30d

Description

Hi Team,

When an influxdb-java client is created and batch is enabled, it prevents the application from exiting normally due to the use of a default non-daemon thread pool. This prevents the application from shutting down in the event that the main thread exits/ crashes. Adding a shutdown hook would not help with closing the client as the JVM will only run shutdown hooks after all non-daemon thread exits. A small example would be running the following:

```java
public class App {
public static void main(String[] args) {
InfluxDB influxDB = InfluxDBFactory.connect("http://localhost:8086");
influxDB.enableBatch();
Runtime.getRuntime().addShutdownHook(new Thread(influxDB::close));
System.out.println(influxDB.ping());
}
}
```

Currently, the workaround is to explicitly specify a daemon thread factory:
```java
influxDB.enableBatch(
BatchOptions.DEFAULTS
.threadFactory(runnable -> {
Thread thread = new Thread(runnable);
thread.setDaemon(true);
return thread;
})
);
```

But I was wondering if it would be a better user experience to have this be the default behaviour so that the main application is allowed to exit normally. The rationale is that a common use case for influxdb-java is to write application metrics and usually one would not expect a metrics library to affect the main application (e.g. cause it to not exit properly).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the enableBatch entry point and the BatchOptions.DEFAULTS configuration shown in the report, then reproduce the lifecycle with the provided App example. Trace how the default thread factory is created and verify that the application can exit normally when batch mode is enabled, while preserving batch behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.