Fix/tcp websocket error propagation (#16946)

* fix(macOS): network retry 3s if os error is EHOSTUNREACH

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(macos): detect nested network errors when retrying

Signed-off-by: fufesou <linlong1266@gmail.com>

* refactor(macos): isolate network error detection

Move EHOSTUNREACH detection into the macOS platform module and share the fast retry timeout across both retry branches.

Signed-off-by: fufesou <linlong1266@gmail.com>

* test(macos): cover host-unreachable error chains

Cover direct, context-wrapped, and nested EHOSTUNREACH errors while ensuring ECONNREFUSED does not trigger the fast-retry predicate.

Signed-off-by: fufesou <linlong1266@gmail.com>

---------

Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
fufesou authored and GitHub committed 2026-11-22 17:14:24 +08:00
1 parent 534b047525
commit be181a9d18
3 files changed
+47 -3

No files matched your search

+37
View File
@@ -140,6 +140,14 @@ pub fn major_version() -> u32 {
unsafe { majorVersion() }
}
pub(crate) fn is_host_unreachable_error(err: &hbb_common::anyhow::Error) -> bool {
err.chain().any(|cause| {
cause
.downcast_ref::<std::io::Error>()
.is_some_and(|err| err.raw_os_error() == Some(hbb_common::libc::EHOSTUNREACH))
})
}
pub fn is_process_trusted(prompt: bool) -> bool {
autoreleasepool(|| unsafe_is_process_trusted(prompt))
}
@@ -2155,6 +2163,35 @@ pub(crate) fn get_bundle_id() -> Option<String> {
}
}
#[cfg(test)]
mod network_error_tests {
use super::is_host_unreachable_error;
use hbb_common::{anyhow, libc, thiserror};
use std::io;
#[derive(Debug, thiserror::Error)]
#[error("transport error: {0}")]
struct TransportError(#[source] io::Error);
#[test]
fn host_unreachable_matches_direct_and_context_errors() {
for (code, expected) in [(libc::EHOSTUNREACH, true), (libc::ECONNREFUSED, false)] {
let err = anyhow::Error::new(io::Error::from_raw_os_error(code));
assert_eq!(is_host_unreachable_error(&err), expected);
assert_eq!(is_host_unreachable_error(&err.context("connect")), expected);
}
}
#[test]
fn host_unreachable_matches_nested_sources() {
for (code, expected) in [(libc::EHOSTUNREACH, true), (libc::ECONNREFUSED, false)] {
let err = anyhow::Error::new(TransportError(io::Error::from_raw_os_error(code)))
.context("connect");
assert_eq!(is_host_unreachable_error(&err), expected);
}
}
}
#[cfg(test)]
mod update_tests {
use super::*;
+9 -2
View File
@@ -39,6 +39,8 @@ use crate::{
type Message = RendezvousMessage;
const FAST_RETRY_TIMEOUT: u64 = 3_000;
fn connection_meta(
control_permissions: Option<ControlPermissions>,
controlled_context: Option<ControlledContext>,
@@ -357,13 +359,18 @@ impl RendezvousMediator {
let timeout = timeout.clone();
futs.push(tokio::spawn(async move {
if let Err(err) = Self::start(server, host).await {
#[cfg(target_os = "macos")]
if crate::platform::is_host_unreachable_error(&err) {
// An app update can briefly invalidate its Local Network policy.
*timeout.write().unwrap() = FAST_RETRY_TIMEOUT;
}
let err = format!("rendezvous mediator error: {err}");
// When user reboot, there might be below error, waiting too long
// (CONNECT_TIMEOUT 18s) will make user think there is bug
if err.contains("10054") || err.contains("11001") {
// No such host is known. (os error 11001)
// An existing connection was forcibly closed by the remote host. (os error 10054): also happens for UDP
*timeout.write().unwrap() = 3000;
*timeout.write().unwrap() = FAST_RETRY_TIMEOUT;
}
log::error!("{err}");
}
@@ -1710,7 +1717,7 @@ where
Ok(stream) => return Some(stream),
// Not logged one by one, but the count says which gateway it was: RST fails a
// punch at once and fits a dozen into the window, a silent drop holds the one
// punch for the whole of it. `connect_tcp_local` keeps no errno anyway.
// punch for the whole of it.
Err(_) => round += 1,
}
}