Skip to content

Commit c407638

Browse files
authored
fix: Revert "fix: bind to [::1] per default instead of 127.0.0.1 in PocketIC (dfinity#3280)" (dfinity#3597)
Reverting this [PR](dfinity#3280) since the flakiness [persists](https://github.com/dfinity/ic/actions/runs/12948633271/job/36117683459?pr=3595).
1 parent f128ac9 commit c407638

File tree

10 files changed

+46
-32
lines changed

10 files changed

+46
-32
lines changed

packages/pocket-ic/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ rust_test_suite(
9494
proc_macro_deps = MACRO_DEPENDENCIES,
9595
tags = [
9696
"cpu:16",
97+
# TODO: remove 'requires-network' tag when the root cause for sporadic error below on Apple Silicon is identified and fixed.
98+
# Failed to crate http gateway: Failed to bind to address 127.0.0.1:0: Operation not permitted (os error 1)
99+
"requires-network",
97100
"test_macos",
98101
],
99102
deps = [":pocket-ic"] + DEPENDENCIES + TEST_DEPENDENCIES,
@@ -114,6 +117,9 @@ rust_test_suite(
114117
flaky = False,
115118
proc_macro_deps = MACRO_DEPENDENCIES,
116119
tags = [
120+
# TODO: remove 'requires-network' tag when the root cause for sporadic error below on Apple Silicon is identified and fixed.
121+
# Failed to crate http gateway: Failed to bind to address 127.0.0.1:0: Operation not permitted (os error 1)
122+
"requires-network",
117123
"test_macos",
118124
],
119125
deps = [":pocket-ic"] + DEPENDENCIES + TEST_DEPENDENCIES,
@@ -137,6 +143,9 @@ rust_test_suite(
137143
proc_macro_deps = MACRO_DEPENDENCIES,
138144
tags = [
139145
"cpu:16",
146+
# TODO: remove 'requires-network' tag when the root cause for sporadic error below on Apple Silicon is identified and fixed.
147+
# Failed to crate http gateway: Failed to bind to address 127.0.0.1:0: Operation not permitted (os error 1)
148+
"requires-network",
140149
"test_macos",
141150
],
142151
deps = [":pocket-ic"] + DEPENDENCIES + TEST_DEPENDENCIES,

packages/pocket-ic/HOWTO.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ Now we create a PocketIC instance configured with the Bitcoin subnet and the `bi
525525
.with_ii_subnet() // to have tECDSA keys available
526526
.with_application_subnet() // to deploy the test dapp
527527
.with_bitcoind_addr(SocketAddr::new(
528-
IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)),
528+
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
529529
18444,
530530
))
531531
.build();
@@ -576,7 +576,7 @@ To mine blocks with rewards credited to a given `bitcoin_address: String`, you c
576576
```rust
577577
use bitcoincore_rpc::{bitcoin::Address, Auth, Client, RpcApi};
578578
let btc_rpc = Client::new(
579-
"http://[::1]:18443",
579+
"http://127.0.0.1:18443",
580580
Auth::UserPass(
581581
"ic-btc-integration".to_string(),
582582
"QPQiNaph19FqUsCrBRN0FII7lyM26B51fAMeBQzCb-E=".to_string(),

packages/pocket-ic/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ const EXPECTED_SERVER_VERSION: &str = "pocket-ic-server 7.0.0";
9999
// the default timeout of a PocketIC operation
100100
const DEFAULT_MAX_REQUEST_TIME_MS: u64 = 300_000;
101101

102-
const LOCALHOST: &str = "[::1]";
102+
const LOCALHOST: &str = "127.0.0.1";
103103

104104
pub struct PocketIcBuilder {
105105
config: Option<ExtendedSubnetConfigSet>,

rs/pocket_ic_server/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,17 @@ rust_test(
192192
},
193193
tags = [
194194
"cpu:8",
195+
# TODO: remove 'requires-network' tag when the root cause for sporadic error below on Apple Silicon is identified and fixed.
196+
# ---- test_http_gateway stdout ----
197+
# thread 'test_http_gateway' panicked at rs/pocket_ic_server/tests/test.rs:383:48:
198+
# called `Result::unwrap()` on an `Err` value: reqwest::Error {
199+
# kind: Request, url: "http://7tjcv-pp777-77776-qaaaa-cai.raw.localhost:49380/",
200+
# source: hyper_util::client::legacy::Error(
201+
# Connect,
202+
# ConnectError("tcp connect error", Os { code: 1, kind: PermissionDenied, message: "Operation not permitted" })
203+
# )
204+
# }
205+
"requires-network",
195206
"test_macos",
196207
],
197208
deps = TEST_DEPENDENCIES,
@@ -244,6 +255,9 @@ rust_test(
244255
"SSL_CERT_FILE": "$(rootpath @mozilla_root_ca_store//file)",
245256
},
246257
tags = [
258+
# TODO: remove 'requires-network' tag when the root cause for sporadic error below on Apple Silicon is identified and fixed.
259+
# Failed to crate http gateway: Failed to bind to address 127.0.0.1:0: Operation not permitted (os error 1)
260+
"requires-network",
247261
"test_macos",
248262
],
249263
deps = TEST_DEPENDENCIES,

rs/pocket_ic_server/CHANGELOG.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- New endpoint `/instances/<instance_id>/read/ingress_status` to fetch the status of an update call submitted through an ingress message.
1616
If an optional caller is provided, the status of the update call is known, but the update call was submitted by a different caller, then an error is returned.
1717

18-
### Changed
19-
- The default IP address to which the PocketIC server and HTTP gateway bind is [::1] instead of 127.0.0.1.
20-
2118
### Fixed
2219
- Canisters created via `provisional_create_canister_with_cycles` with the management canister ID as the effective canister ID
2320
are created on an arbitrary subnet.

rs/pocket_ic_server/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const LOG_DIR_LEVELS_ENV_NAME: &str = "POCKET_IC_LOG_DIR_LEVELS";
5656
#[derive(Parser)]
5757
#[clap(version = "7.0.0")]
5858
struct Args {
59-
/// The IP address to which the PocketIC server should bind (defaults to [::1])
59+
/// The IP address to which the PocketIC server should bind (defaults to 127.0.0.1)
6060
#[clap(long, short)]
6161
ip_addr: Option<String>,
6262
/// Log levels for PocketIC server logs (defaults to `pocket_ic_server=info,tower_http=info,axum::rejection=trace`).
@@ -131,7 +131,7 @@ async fn start(runtime: Arc<Runtime>) {
131131
None
132132
};
133133

134-
let ip_addr = args.ip_addr.unwrap_or("[::1]".to_string());
134+
let ip_addr = args.ip_addr.unwrap_or("127.0.0.1".to_string());
135135
let addr = format!("{}:{}", ip_addr, args.port);
136136
let listener = std::net::TcpListener::bind(addr.clone())
137137
.unwrap_or_else(|_| panic!("Failed to bind PocketIC server to address {}", addr));

rs/pocket_ic_server/src/state_api/canister_id.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ use crate::state_api::state::HandlerState;
22
use async_trait::async_trait;
33
use axum::extract::FromRequestParts;
44
use candid::Principal;
5-
use fqdn::{Fqdn, FQDN};
5+
use fqdn::{fqdn, Fqdn, FQDN};
66
use hyper::{
77
header::{HOST, REFERER},
88
http::request::Parts,
99
Uri,
1010
};
1111
use std::collections::BTreeMap;
12-
use std::str::FromStr;
1312
use std::sync::Arc;
1413

1514
// ADAPTED from ic-gateway
@@ -190,10 +189,8 @@ impl FromRequestParts<DomainResolver> for HostHeader {
190189
.rsplit_once(':')
191190
.map(|(host, _port)| host)
192191
.unwrap_or(host);
193-
let fqdn =
194-
FQDN::from_str(host).map_err(|_| "Could not parse domain name from Host header")?;
195192
resolver
196-
.resolve_domain(&fqdn)
193+
.resolve_domain(&fqdn!(host))
197194
.map(|d| d.canister_id)
198195
.ok_or(BAD_HOST)?
199196
.ok_or(BAD_HOST)
@@ -231,10 +228,8 @@ impl FromRequestParts<DomainResolver> for RefererHeaderHost {
231228
let referer = referer.to_str().map_err(|_| BAD_REFERER)?;
232229
let referer: Uri = referer.parse().map_err(|_| BAD_REFERER)?;
233230
let referer = referer.authority().ok_or(BAD_REFERER)?;
234-
let fqdn = FQDN::from_str(referer.host())
235-
.map_err(|_| "Could not parse domain name from Referer header")?;
236231
resolver
237-
.resolve_domain(&fqdn)
232+
.resolve_domain(&fqdn!(referer.host()))
238233
.map(|d| d.canister_id)
239234
.ok_or(BAD_REFERER)?
240235
.ok_or(BAD_REFERER)

rs/pocket_ic_server/src/state_api/state.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use axum::{
1818
use axum_server::tls_rustls::RustlsConfig;
1919
use axum_server::Handle;
2020
use base64;
21-
use fqdn::FQDN;
21+
use fqdn::{fqdn, FQDN};
2222
use futures::future::Shared;
2323
use http::{
2424
header::{
@@ -795,7 +795,7 @@ impl ApiState {
795795
let ip_addr = http_gateway_config
796796
.ip_addr
797797
.clone()
798-
.unwrap_or("[::1]".to_string());
798+
.unwrap_or("127.0.0.1".to_string());
799799
let port = http_gateway_config.port.unwrap_or_default();
800800
let addr = format!("{}:{}", ip_addr, port);
801801
let listener = std::net::TcpListener::bind(&addr)
@@ -829,9 +829,8 @@ impl ApiState {
829829
.clone()
830830
.unwrap_or(vec!["localhost".to_string()])
831831
.iter()
832-
.map(|d| FQDN::from_str(d))
833-
.collect::<Result<Vec<_>, _>>()
834-
.map_err(|e| e.to_string())?;
832+
.map(|d| fqdn!(d))
833+
.collect();
835834
spawn(async move {
836835
let http_gateway_client = ic_http_gateway::HttpGatewayClientBuilder::new()
837836
.with_agent(agent)

rs/pocket_ic_server/tests/bitcoin_integration_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ic_nns_constants::ROOT_CANISTER_ID;
66
use pocket_ic::{update_candid, PocketIc, PocketIcBuilder};
77
use std::fs::{create_dir, File};
88
use std::io::Write;
9-
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
9+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
1010
use std::process::Command;
1111
use std::str::FromStr;
1212
use std::time::SystemTime;
@@ -105,7 +105,7 @@ rpcauth=ic-btc-integration:cdf2741387f3a12438f69092f0fdad8e$62081498c98bee09a0dc
105105
.with_ii_subnet()
106106
.with_application_subnet()
107107
.with_bitcoind_addr(SocketAddr::new(
108-
IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)),
108+
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
109109
18444,
110110
))
111111
.build();
@@ -130,7 +130,7 @@ rpcauth=ic-btc-integration:cdf2741387f3a12438f69092f0fdad8e$62081498c98bee09a0dc
130130
.0;
131131

132132
let btc_rpc = Client::new(
133-
"http://[::1]:18443",
133+
"http://127.0.0.1:18443",
134134
Auth::UserPass(
135135
"ic-btc-integration".to_string(),
136136
"QPQiNaph19FqUsCrBRN0FII7lyM26B51fAMeBQzCb-E=".to_string(),

rs/pocket_ic_server/tests/test.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ use reqwest::{StatusCode, Url};
2525
use slog::Level;
2626
use std::io::Read;
2727
use std::io::Write;
28-
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
28+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
2929
use std::path::PathBuf;
3030
use std::process::{Child, Command};
3131
use std::time::Duration;
3232
use tempfile::{NamedTempFile, TempDir};
3333

34-
pub const LOCALHOST: &str = "[::1]";
34+
pub const LOCALHOST: &str = "127.0.0.1";
3535

3636
fn start_server_helper(
3737
test_driver_pid: Option<u32>,
@@ -290,7 +290,7 @@ async fn test_gateway(server_url: Url, https: bool) {
290290
assert_eq!(http_gateway_details.domains, domains);
291291
assert_eq!(http_gateway_details.https_config, https_config);
292292

293-
// create a non-blocking reqwest client resolving localhost/example.com and <canister-id>.(raw.)localhost/example.com to [::1]
293+
// create a non-blocking reqwest client resolving localhost/example.com and <canister-id>.(raw.)localhost/example.com to 127.0.0.1
294294
let mut builder = NonblockingClient::builder();
295295
for domain in [
296296
localhost,
@@ -302,7 +302,7 @@ async fn test_gateway(server_url: Url, https: bool) {
302302
] {
303303
builder = builder.resolve(
304304
domain,
305-
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), port),
305+
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), port),
306306
);
307307
}
308308
// add a custom root certificate
@@ -341,13 +341,13 @@ async fn test_gateway(server_url: Url, https: bool) {
341341
.await
342342
.unwrap();
343343

344-
// perform frontend asset request for the title page at http://[::1]:<port>/?canisterId=<canister-id>
344+
// perform frontend asset request for the title page at http://127.0.0.1:<port>/?canisterId=<canister-id>
345345
let mut test_urls = vec![];
346346
if !https {
347347
assert_eq!(proto, "http");
348348
let canister_url = format!(
349349
"{}://{}:{}/?canisterId={}",
350-
"http", LOCALHOST, port, canister_id
350+
"http", "127.0.0.1", port, canister_id
351351
);
352352
test_urls.push(canister_url);
353353
}
@@ -1112,7 +1112,7 @@ fn test_gateway_ip_addr_host() {
11121112

11131113
let mut endpoint = pic.make_live(None);
11141114
endpoint
1115-
.set_ip_host(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)))
1115+
.set_ip_host(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)))
11161116
.unwrap();
11171117

11181118
let rt = tokio::runtime::Builder::new_current_thread()
@@ -1474,7 +1474,7 @@ fn test_gateway_address_in_use() {
14741474
)
14751475
.unwrap_err();
14761476
assert!(err.contains(&format!(
1477-
"Failed to bind to address [::1]:{}: Address already in use",
1477+
"Failed to bind to address 127.0.0.1:{}: Address already in use",
14781478
port
14791479
)));
14801480
}

0 commit comments

Comments
 (0)