From bdb383fa0b5a0092733c47d23852f134a44b0fec Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Sun, 6 Dec 2026 21:31:19 +0800 Subject: [PATCH] fix(java): wait for the old compiler process to exit --- crates/ide-lsp/src/java_build_coordinator.rs | 43 ++++++++++++++++--- .../ide-lsp/tests/integration/mock_server.rs | 19 +++++++- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/crates/ide-lsp/src/java_build_coordinator.rs b/crates/ide-lsp/src/java_build_coordinator.rs index 5e207af092..b42038670e 100644 --- a/crates/ide-lsp/src/java_build_coordinator.rs +++ b/crates/ide-lsp/src/java_build_coordinator.rs @@ -148,9 +148,16 @@ impl JavaBuildCoordinator { // request takes the Eclipse project scheduling rule, draining a build // already in progress before the borrower touches the same outputs. for (writer, generation, roots) in paused { - let run = crate::jdtls::cancellable_wait(cancel, writer.wait_ready(Duration::from_secs(240))).await?; + let run = tokio::select! { + ready = crate::jdtls::cancellable_wait(cancel, writer.wait_ready(Duration::from_secs(240))) => ready?, + retired = self.wait_retired((writer.id, generation), cancel) => { + retired?; + continue; + } + }; let Some(run) = run.filter(|run| run.generation == generation) else { if !writer.is_live(generation) { + self.wait_retired((writer.id, generation), cancel).await?; continue; } return Err(IdeError::unavailable("Overlapping Java workspace is still starting; retry the build")); @@ -161,21 +168,45 @@ impl JavaBuildCoordinator { tokio::time::timeout(Duration::from_secs(240), writer.wait_java_ready(generation)), ) .await?; - if !matches!(ready, Ok(true)) && writer.is_live(generation) { + if !matches!(ready, Ok(true)) { + if !writer.is_live(generation) { + self.wait_retired((writer.id, generation), cancel).await?; + continue; + } return Err(IdeError::unavailable( "Overlapping Java workspace is still importing; retry the build", )); } } let result = crate::jdtls::cancellable_wait(cancel, drain(&run, &roots, Some(cancel.clone()))).await?; - if let Err(error) = result - && writer.is_live(generation) - { - return Err(error); + if let Err(error) = result { + if writer.is_live(generation) { + return Err(error); + } + self.wait_retired((writer.id, generation), cancel).await?; } } Ok(permit) } + + /// Stopping changes the server status before its JVM has released the + /// output files. Wait for reaping, not merely for the transport to close. + async fn wait_retired(&self, instance: Instance, cancel: &CancellationToken) -> IdeResult<()> { + let wait = async { + loop { + let changed = self.changed.notified(); + tokio::pin!(changed); + changed.as_mut().enable(); + if !self.state.lock().entries.iter().any(|entry| entry.instance == instance) { + return; + } + changed.await; + } + }; + crate::jdtls::cancellable_wait(cancel, tokio::time::timeout(Duration::from_secs(240), wait)) + .await? + .map_err(|_| IdeError::unavailable("Overlapping Java workspace has not stopped; retry the build")) + } } /// Query the project's classpaths under its Eclipse scheduling rule. An diff --git a/crates/ide-lsp/tests/integration/mock_server.rs b/crates/ide-lsp/tests/integration/mock_server.rs index 1b55a539bc..8f80132ad5 100644 --- a/crates/ide-lsp/tests/integration/mock_server.rs +++ b/crates/ide-lsp/tests/integration/mock_server.rs @@ -3384,7 +3384,24 @@ async fn java_overlapping_windows_coordinate_autobuild_and_explicit_builds() { .iter() .all(|p| p["settings"]["java"]["autobuild"]["enabled"] == false) ); - parent.mgr.shutdown_all().await; + // Down is published before the JVM has exited. A child build must wait + // for the old writer to release the workspace, not just for that status. + let exit = Arc::new(Gate::default()); + *primary.held.lock() = Some(("exit".into(), exit.clone())); + let shutdown = tokio::spawn({ + let mgr = parent.mgr.clone(); + async move { mgr.shutdown_all().await } + }); + eventually(|| exit.entered()).await; + let mut build = tokio::spawn({ + let (mgr, root) = (child.mgr.clone(), child.root.clone()); + async move { mgr.java_build(&root, false, &Default::default()).await } + }); + let early = tokio::time::timeout(Duration::from_millis(100), &mut build).await; + exit.open(); + shutdown.await.unwrap(); + assert!(early.is_err(), "the child must wait for the first writer to exit: {early:?}"); + assert_eq!(build.await.unwrap().unwrap(), ide_lsp::jdtls::JavaBuildStatus::Succeeded); eventually(|| { secondary .notified("workspace/didChangeConfiguration")