Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/cli/src/commands/agent_kill.rs
13383 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
use std::fs;
7
8
use crate::log;
9
use crate::util::command::kill_tree;
10
use crate::util::errors::{wrap, AnyError};
11
use crate::util::machine::process_exists;
12
13
use super::agent_host::AgentHostLockData;
14
use super::CommandContext;
15
16
/// Forcefully kills the running agent host process tree and cleans up.
17
pub async fn agent_kill(ctx: CommandContext) -> Result<i32, AnyError> {
18
let lockfile_path = ctx.paths.agent_host_lockfile();
19
20
let data = fs::read_to_string(&lockfile_path).map_err(|e| {
21
wrap(
22
e,
23
"No running agent host found. Start one with `code agent host`",
24
)
25
})?;
26
27
let lock: AgentHostLockData = serde_json::from_str(&data).map_err(|e| {
28
wrap(
29
e,
30
format!("Corrupt agent host lockfile at {}", lockfile_path.display()),
31
)
32
})?;
33
34
if !process_exists(lock.pid) {
35
let _ = fs::remove_file(&lockfile_path);
36
ctx.log
37
.result("Agent host is not running (stale lockfile cleaned up).");
38
return Ok(0);
39
}
40
41
debug!(
42
ctx.log,
43
"Killing agent host process tree (pid {})", lock.pid
44
);
45
46
kill_tree(lock.pid)
47
.await
48
.map_err(|e| wrap(e, "Failed to kill agent host process tree"))?;
49
50
let _ = fs::remove_file(&lockfile_path);
51
52
ctx.log
53
.result(format!("Killed agent host (pid {}).", lock.pid));
54
55
Ok(0)
56
}
57
58