Skip to content

Commit 94e55c9

Browse files
Yury Samkevichfacebook-github-bot
Yury Samkevich
authored andcommitted
fix 'failed to start transient scope unit' in daemon start
Reviewed By: blackm00n Differential Revision: D71620633 fbshipit-source-id: 42149beaa9ec4ee792bc9f563cd012f533d8c15c
1 parent 061496c commit 94e55c9

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

app/buck2_client_ctx/src/daemon/client/connect.rs

+3
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ impl<'a> BuckdLifecycle<'a> {
396396
ParentSlice::Root(slice_name.clone()),
397397
))?;
398398
let mut cmd = if let Some(systemd_runner) = &systemd_runner {
399+
systemd_runner
400+
.ensure_scope_stopped(&format!("{}.scope", &slice_name))
401+
.await?;
399402
systemd_runner
400403
.background_command_linux(daemon_exe, &slice_name, &project_dir.root())
401404
.into()

app/buck2_common/src/systemd.rs

+23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::sync::OnceLock;
1515

1616
use buck2_core::fs::paths::abs_norm_path::AbsNormPath;
1717
use buck2_util::process;
18+
use tracing::warn;
1819

1920
use crate::init::ResourceControlConfig;
2021
use crate::init::ResourceControlStatus;
@@ -219,6 +220,28 @@ impl SystemdRunner {
219220
}
220221
Ok(())
221222
}
223+
224+
pub async fn ensure_scope_stopped(&self, scope: &str) -> buck2_error::Result<()> {
225+
let mut cmd = process::async_background_command("systemctl");
226+
cmd.arg("is-active").arg("--quiet").arg("--user").arg(scope);
227+
// systemctl returns no error if scope is active
228+
let is_active = cmd.status().await?.success();
229+
if is_active {
230+
warn!(
231+
"Transient scope unit {} is already active. Stopping before start a new one.",
232+
scope
233+
);
234+
self.stop_scope(scope).await?;
235+
}
236+
Ok(())
237+
}
238+
239+
async fn stop_scope(&self, scope: &str) -> buck2_error::Result<()> {
240+
let mut cmd = process::async_background_command("systemctl");
241+
cmd.arg("stop").arg("--user").arg(scope);
242+
cmd.spawn()?.wait().await?;
243+
Ok(())
244+
}
222245
}
223246

224247
// Helper function to replace a special characters in a cgroup unit name

0 commit comments

Comments
 (0)