From dd85c4e12640b12fd7519d39c307102d94fdf3cd Mon Sep 17 00:00:00 2001 From: mraszyk <31483726+mraszyk@users.noreply.github.com> Date: Wed, 20 Nov 2024 22:44:03 +0100 Subject: [PATCH 1/2] chore: stop HTTP gateway gracefully (#4009) * chore: stop HTTP gateway gracefully * windows --- src/dfx/src/actors/pocketic_proxy.rs | 60 ++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/src/dfx/src/actors/pocketic_proxy.rs b/src/dfx/src/actors/pocketic_proxy.rs index b41a04454f..8d5cc16526 100644 --- a/src/dfx/src/actors/pocketic_proxy.rs +++ b/src/dfx/src/actors/pocketic_proxy.rs @@ -284,23 +284,26 @@ fn pocketic_proxy_start_thread( } } }; - if let Err(e) = initialize_gateway( + let instance = match initialize_gateway( format!("http://localhost:{port}").parse().unwrap(), replica_url.clone(), domains.clone(), address, logger.clone(), ) { - error!(logger, "Failed to initialize HTTP gateway: {e:#}"); - let _ = child.kill(); - let _ = child.wait(); - if receiver.try_recv().is_ok() { - debug!(logger, "Got signal to stop."); - break; - } else { - continue; + Err(e) => { + error!(logger, "Failed to initialize HTTP gateway: {e:#}"); + let _ = child.kill(); + let _ = child.wait(); + if receiver.try_recv().is_ok() { + debug!(logger, "Got signal to stop."); + break; + } else { + continue; + } } - } + Ok(i) => i, + }; info!(logger, "Replica API running on {address}"); // Send PocketIcProxyReadySignal to PocketIcProxy. @@ -314,6 +317,9 @@ fn pocketic_proxy_start_thread( logger, "Got signal to stop. Killing pocket-ic gateway process..." ); + if let Err(e) = shutdown_pocketic_proxy(port, instance, logger.clone()) { + error!(logger, "Error shutting down PocketIC gracefully: {e}"); + } let _ = child.kill(); let _ = child.wait(); break; @@ -349,7 +355,7 @@ async fn initialize_gateway( domains: Option>, addr: SocketAddr, logger: Logger, -) -> DfxResult { +) -> DfxResult { use pocket_ic::common::rest::{ CreateHttpGatewayResponse, HttpGatewayBackend, HttpGatewayConfig, }; @@ -369,11 +375,12 @@ async fn initialize_gateway( .await? .error_for_status()?; let resp = resp.json::().await?; - if let CreateHttpGatewayResponse::Error { message } = resp { - bail!("Gateway init error: {message}") - } + let instance = match resp { + CreateHttpGatewayResponse::Created(info) => info.instance_id, + CreateHttpGatewayResponse::Error { message } => bail!("Gateway init error: {message}"), + }; info!(logger, "Initialized HTTP gateway."); - Ok(()) + Ok(instance) } #[cfg(not(unix))] @@ -383,6 +390,27 @@ fn initialize_gateway( _: Option>, _: SocketAddr, _: Logger, -) -> DfxResult { +) -> DfxResult { bail!("PocketIC gateway not supported on this platform") } + +#[cfg(unix)] +#[tokio::main(flavor = "current_thread")] +async fn shutdown_pocketic_proxy(port: u16, instance: usize, logger: Logger) -> DfxResult { + use reqwest::Client; + let shutdown_client = Client::new(); + debug!(logger, "Sending shutdown request to HTTP gateway"); + shutdown_client + .post(format!( + "http://localhost:{port}/http_gateway/{instance}/stop" + )) + .send() + .await? + .error_for_status()?; + Ok(()) +} + +#[cfg(not(unix))] +fn shutdown_pocketic_proxy(_: u16, _: usize, _: Logger) -> DfxResult { + bail!("PocketIC not supported on this platform") +} From 1c22db89ac1f9f96384902c91aa27bac1c1eff36 Mon Sep 17 00:00:00 2001 From: Severin Siffert Date: Thu, 21 Nov 2024 10:53:50 +0100 Subject: [PATCH 2/2] chore: fix dfx cycles convert docs (#4010) --- docs/cli-reference/dfx-cycles.mdx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/cli-reference/dfx-cycles.mdx b/docs/cli-reference/dfx-cycles.mdx index 788764808e..8982569289 100644 --- a/docs/cli-reference/dfx-cycles.mdx +++ b/docs/cli-reference/dfx-cycles.mdx @@ -18,8 +18,10 @@ The following subcommands are available: | Command | Description | |---------------------------------------|--------------------------------------------------------------------------------------| +| [`approve`](#dfx-cycles-approve) | Approves a principal to spend cycles on your behalf. | | [`balance`](#dfx-cycles-balance) | Prints the account balance of the user. | | [`convert`](#dfx-cycles-convert) | Convert some of the user's ICP balance into cycles. | +| [`top-up`](#dfx-cycles-top-up) | Deposit cycles into a canister. | | [`transfer`](#dfx-cycles-transfer) | Send cycles to another account. | | `help` | Displays usage information message for a specified subcommand. | @@ -135,7 +137,7 @@ You can specify the following arguments for the `dfx cycles convert` command. | `--e8s ` | Specify ICP token fractional units—called e8s—as a whole number, where one e8 is the smallest fraction of an ICP token. For example, 1.05000000 is 1 ICP and 5000000 e8s. You can use this option on its own or in conjunction with the `--icp` option. | | `--fee ` | Specify a transaction fee. The default is 10000 e8s. | | `--icp ` | Specify ICP tokens as a whole number. You can use this option on its own or in conjunction with `--e8s`. | -| `--memo ` | Memo used when depositing the minted cycles. | +| `--deposit-memo ` | Memo used when depositing the minted cycles. | | `--to-subaccount ` | Subaccount where the cycles are deposited. | ### Examples