Skip to content

Commit 2ecf399

Browse files
Implement network locking in terms of icp::fs::lock
1 parent b7b7fd7 commit 2ecf399

File tree

10 files changed

+287
-622
lines changed

10 files changed

+287
-622
lines changed

crates/icp-cli/src/commands/network/run.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ pub(crate) async fn exec(ctx: &Context, args: &RunArgs) -> Result<(), CommandErr
109109

110110
if args.background {
111111
let mut child = run_in_background()?;
112-
nd.save_background_network_runner_pid(Pid::from(child.id() as usize))?;
112+
nd.save_background_network_runner_pid(Pid::from(child.id() as usize))
113+
.await?;
113114
relay_child_output_until_healthy(ctx, &mut child, &nd).await?;
114115
} else {
115116
run_network(
@@ -221,7 +222,7 @@ async fn wait_for_healthy_network(nd: &NetworkDirectory) -> Result<(), CommandEr
221222

222223
// Wait for network descriptor to be written
223224
let network = retry_with_timeout(
224-
|| async move { nd.load_network_descriptor().unwrap_or(None) },
225+
|| async move { nd.load_network_descriptor().await.unwrap_or(None) },
225226
max_retries,
226227
delay_ms,
227228
)

crates/icp-cli/src/commands/network/stop.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
use std::time::Duration;
22

33
use clap::Parser;
4-
use icp::{fs::remove_file, manifest, network::NetworkDirectory};
4+
use icp::{
5+
fs::{lock::LockError, remove_file},
6+
manifest,
7+
network::NetworkDirectory,
8+
};
59
use sysinfo::{Pid, ProcessesToUpdate, Signal, System};
610

711
use crate::commands::Context;
@@ -35,6 +39,9 @@ pub enum CommandError {
3539

3640
#[error("process {pid} did not exit within {timeout} seconds")]
3741
Timeout { pid: Pid, timeout: u64 },
42+
43+
#[error(transparent)]
44+
LockFile(#[from] LockError),
3845
}
3946

4047
pub async fn exec(ctx: &Context, cmd: &Cmd) -> Result<(), CommandError> {
@@ -59,7 +66,8 @@ pub async fn exec(ctx: &Context, cmd: &Cmd) -> Result<(), CommandError> {
5966

6067
// Load PID from file
6168
let pid = nd
62-
.load_background_network_runner_pid()?
69+
.load_background_network_runner_pid()
70+
.await?
6371
.ok_or(CommandError::NotRunning {
6472
name: cmd.name.clone(),
6573
})?;
@@ -71,8 +79,13 @@ pub async fn exec(ctx: &Context, cmd: &Cmd) -> Result<(), CommandError> {
7179
send_sigint(pid);
7280
wait_for_process_exit(pid)?;
7381

74-
let pid_file = nd.structure.background_network_runner_pid_file();
75-
let _ = remove_file(&pid_file); // Cleanup is nice, but optional
82+
nd.root()?
83+
.with_write(async |root| {
84+
let pid_file = root.background_network_runner_pid_file();
85+
let _ = remove_file(&pid_file); // Cleanup is nice, but optional
86+
Ok::<_, CommandError>(())
87+
})
88+
.await??;
7689

7790
let _ = ctx.term.write_line("Network stopped successfully");
7891

crates/icp/src/network/access.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ use snafu::{OptionExt, ResultExt, Snafu};
33

44
use crate::{
55
Network,
6-
fs::json,
7-
network::{Configuration, NetworkDirectory, access::GetNetworkAccessError::DecodeRootKey},
6+
network::{
7+
Configuration, NetworkDirectory, access::GetNetworkAccessError::DecodeRootKey,
8+
directory::LoadNetworkFileError,
9+
},
810
prelude::*,
911
};
1012

@@ -41,11 +43,11 @@ pub enum GetNetworkAccessError {
4143
#[snafu(display("failed to decode root key"))]
4244
DecodeRootKey { source: hex::FromHexError },
4345

44-
#[snafu(transparent)]
45-
LoadJsonWithLock { source: json::Error },
46-
4746
#[snafu(display("failed to load port {port} descriptor"))]
48-
LoadPortDescriptor { port: u16, source: json::Error },
47+
LoadPortDescriptor {
48+
port: u16,
49+
source: LoadNetworkFileError,
50+
},
4951

5052
#[snafu(display("the {network} network for this project is not running"))]
5153
NetworkNotRunning { network: String },
@@ -61,9 +63,12 @@ pub enum GetNetworkAccessError {
6163

6264
#[snafu(display("no descriptor found for port {port}"))]
6365
NoPortDescriptor { port: u16 },
66+
67+
#[snafu(display("failed to load network descriptor"))]
68+
LoadNetworkDescriptor { source: LoadNetworkFileError },
6469
}
6570

66-
pub fn get_network_access(
71+
pub async fn get_network_access(
6772
nd: NetworkDirectory,
6873
network: &Network,
6974
) -> Result<NetworkAccess, GetNetworkAccessError> {
@@ -72,11 +77,13 @@ pub fn get_network_access(
7277
// Managed
7378
Configuration::Managed { managed: _ } => {
7479
// Load network descriptor
75-
let desc =
76-
nd.load_network_descriptor()?
77-
.ok_or(GetNetworkAccessError::NetworkNotRunning {
78-
network: nd.network_name.to_owned(),
79-
})?;
80+
let desc = nd
81+
.load_network_descriptor()
82+
.await
83+
.context(LoadNetworkDescriptorSnafu)?
84+
.ok_or(GetNetworkAccessError::NetworkNotRunning {
85+
network: nd.network_name.to_owned(),
86+
})?;
8087

8188
// Specify port
8289
let port = desc.gateway.port;
@@ -85,6 +92,7 @@ pub fn get_network_access(
8592
if desc.gateway.fixed {
8693
let pdesc = nd
8794
.load_port_descriptor(port)
95+
.await
8896
.context(LoadPortDescriptorSnafu { port })?
8997
.context(NoPortDescriptorSnafu { port })?;
9098

0 commit comments

Comments
 (0)