fix(java): allow a slow first java -version before excluding a JDK (#3868)

A JDK whose java -version took longer than 5 seconds was treated as unable to run and left out of the jdtls runtimes until the server restarted. The first run of an x86_64 JDK 8 under Rosetta, or of a freshly downloaded JDK being scanned, can take that long, so a Java 8 project compiled against a newer JDK and jdk.nashorn stopped resolving. The probe now waits up to 30 seconds; a java that never answers is still rejected.

Fixes #3684
This commit is contained in:
l0ng-ai
2026-11-17 09:45:28 +08:00
committed by GitHub
parent c791f2c34c
commit 8d87b0de6e
+27 -1
View File
@@ -53,6 +53,12 @@ struct JavaProbe {
static JAVA_PROBES: Mutex<BTreeMap<PathBuf, JavaProbe>> = Mutex::new(BTreeMap::new());
/// How long `java -version` may take before the JDK counts as unable to run. The first
/// run of a JDK can be slow: Rosetta translates an x86_64 JDK on Apple silicon, and
/// malware scans check a freshly downloaded one. Excluded, a project's JDK 8 leaves its
/// `JavaSE-1.8` to a newer JDK, and APIs JDK 8 has (`jdk.nashorn`) stop resolving.
const JAVA_PROBE_TIMEOUT: Duration = if cfg!(test) { Duration::from_secs(8) } else { Duration::from_secs(30) };
/// Metadata can describe a JDK built for another architecture. Probe the executable
/// before giving it to JDT, with a short cache shared by server and project selection.
pub(crate) fn java_runs(java: &Path) -> bool {
@@ -72,7 +78,7 @@ pub(crate) fn java_runs(java: &Path) -> bool {
return probe.runnable;
}
}
let runnable = crate::exec::query(java, &["-version"], Duration::from_secs(5)).is_some();
let runnable = crate::exec::query(java, &["-version"], JAVA_PROBE_TIMEOUT).is_some();
JAVA_PROBES.lock().unwrap_or_else(|e| e.into_inner()).insert(
java.to_path_buf(),
JavaProbe { modified, len: metadata.len(), permissions: metadata.permissions(), at: Instant::now(), runnable },
@@ -980,6 +986,26 @@ mod tests {
assert_eq!(std::fs::read_to_string(&probes).unwrap().lines().count(), 1, "probed once");
}
#[cfg(unix)]
#[test]
fn a_jdk_8_slow_on_its_first_run_stays_the_java_8_runtime() {
let tmp = tempfile::tempdir().unwrap();
let jdk8 = test_jdk(tmp.path(), 8, "rosetta8");
let jdk25 = test_jdk(tmp.path(), 25, "native25");
// Longer than the 5 seconds a first run under Rosetta used to be allowed.
std::fs::write(
java_executable(&jdk8.home),
"#!/bin/sh
/bin/sleep 6
echo 'openjdk version 1.8'
",
)
.unwrap();
let rt = runtimes(&[jdk8.clone(), jdk25.clone()]);
assert_eq!(rt[0], json!({ "name": "JavaSE-1.8", "path": jdk8.home }));
assert_eq!(rt[1]["name"], "JavaSE-25");
}
#[cfg(unix)]
#[test]
fn slow_java_probe_does_not_hold_the_cache_lock() {