java_stub_template.txt leaves orphan Java process when terminated
- Dominant language
- Java
- Stars
- 25.8k
- Forks
- 4.6k
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 75
Description
### Description of the bug:
`java_binary`s with long classpaths are not properly terminated when the parent bash process receives a TERM or INT signal.
When bash receives a TERM signal, it does not forward that signal to the child process, and instead just exits, leaving the orphaned process behind.
When possible, it's best to `exec` the process, but since we want to clean up the temporary files created in `create_and_run_classpath_jar`, we can't `exec $JAVABIN` like we do in the short-classpath case. bazelbuild/rules_nodejs and aspect-build/rules_js handle this case in their launcher scripts (https://github.com/bazelbuild/rules_nodejs/pull/246, https://github.com/aspect-build/rules_js/blob/2dfdcbf08fda897a2280c686ed03e4a8b22e6286/js/private/js_binary.sh.tpl#L404-L423) by explicitly trapping the signals and forwarding them to the child process. I believe the Java launcher should do the same.
### What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
1. `bazel run //src/java_tools/buildjar/java/com/google/devtools/build/buildjar:BazelJavaBuilder -- --classpath_limit=0 --persistent_worker`
2. From another terminal `pgrep -fa BazelJavaBuilder`
3. Notice there are 3 processes
1. The `bazel run ...` process
2. The `bash .../BazelJavaBuilder ...` process
3. The `java ... com.google.devtools.build.buildjar.BazelJavaBuilder ...` process
5. Run `kill -TERM `
6. `pgrep -fa BazelJavaBuilder`
7. Notice the `java` process is still running even though its parents have exited
### Which operating system are you running Bazel on?
linux
### What is the output of `bazel info release`?
release 6.2.1
### If `bazel info release` returns `development version` or `(@non-git)`, tell us how you built Bazel.
_No response_
### What's the output of `git remote get-url origin; git rev-parse master; git rev-parse HEAD` ?
```text
git@github.com:bazelbuild/bazel.git
f48c0f6504f29fd37ba966202b3c502d4b636d96
f48c0f6504f29fd37ba966202b3c502d4b636d96
```
### Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.
I believe this bug has existed since `create_and_run_classpath_jar` was added in 102ce6d.
### Have you found anything relevant by searching the web?
_No response_
### Any other information, logs, or outputs that you want to share?
_No response_
Contributor guide
Research direction
Start by reading java_stub_template.txt and the create_and_run_classpath_jar path, then reproduce the issue with the provided BazelJavaBuilder command on Linux. Verify the launcher behavior when its bash process receives TERM or INT. Done means terminating that process also terminates the Java child without leaving an orphan behind.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- bash, java
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100