Repository navigation
fix(backend): wait for the Windows loopback refusal before refusing to start - #204
liquidtoy001 wants to merge 2 commits into
Conversation
…o start The pre-spawn endpoint check added in AstrBotDevs#191 only treats a refused connection as a free port and reused the 800 ms ping timeout. Windows reports the refusal for a closed loopback port after about two seconds, so the probe always timed out and Desktop 4.30.0 refused to start its backend on a free port. Give the check a 3 s floor. An answering endpoint still returns immediately.
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src-tauri/src/backend/http.rs" line_range="142" />
<code_context>
&self.backend_url,
&self.endpoint_resolver,
- Duration::from_millis(timeout_ms.max(1)),
+ Duration::from_millis(timeout_ms.max(BACKEND_ENDPOINT_PROBE_MIN_TIMEOUT_MS)),
|host, port| {
(host.as_str(), port)
</code_context>
<issue_to_address>
**Free backend port is rejected**
When a hostname lookup is slow or resolves to multiple closed loopback addresses on Windows, `confirm_backend_endpoint_available_with_resolver` shares one three-second deadline across name resolution and sequential address checks, so a slow lookup or an earlier refusal leaves too little time for a later refusal; `connect_timeout` returns `TimedOut`, and startup rejects a free port.
Give address checks sufficient time without sharing the three-second deadline with name resolution and earlier attempts.
Also at `src-tauri/src/app_constants.rs:21`.
</issue_to_address>
### Comment 2
<location path="src-tauri/src/backend/http.rs" line_range="729-737" />
<code_context>
+ fn restart_endpoint_probe_waits_for_a_slow_loopback_refusal() {
+ let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind listener");
+ let address = listener.local_addr().expect("listener address");
+ drop(listener);
+ let state = BackendState {
+ backend_url: format!("http://{address}/"),
+ ..BackendState::default()
+ };
+
+ // Windows reports the refusal after about two seconds, longer than the default ping.
+ assert_eq!(
+ state.confirm_backend_endpoint_available(crate::DEFAULT_BACKEND_PING_TIMEOUT_MS),
+ Ok(())
+ );
</code_context>
<issue_to_address>
**Loopback probe test is flaky**
When another local process claims the released ephemeral port before the probe connects, another local process can bind the released ephemeral port before the probe connects, so `restart_endpoint_probe_waits_for_a_slow_loopback_refusal` sees an answering endpoint and its `Ok(())` assertion fails even though the implementation is correct.
Make the probe outcome deterministic, for example by injecting a controllable connector rather than relying on an unreserved local port.
</issue_to_address>Sourcery assessment
Approval pending. 2 findings to address first.
Blocking findings: src-tauri/src/backend/http.rs:142, src-tauri/src/backend/http.rs:737
|
Thank you for reproducing the startup failure on Windows, measuring the delayed loopback refusal, and adding regression coverage. Those measurements helped explain why Desktop could reject a free port before launching the backend. The remaining concern is the endpoint check's total timeout. At #207 has already merged the 5-second default for #203. I'm closing this PR as superseded and keeping the shared endpoint deadline in |
Fixes #203.
After updating to Desktop 4.30.0 on Windows the backend never starts. The startup page shows:
Three users report this in #203. One of them checked that no backend process was left and nothing was listening on the port, found that a reboot did not help, and got Desktop to start by setting
ASTRBOT_BACKEND_PING_TIMEOUT_MS=5000. A user in the community group reported the same message after the 4.30.0 update, also with no backend process left.Cause. The endpoint check added in #191 runs before every managed backend start. It accepts only a refused connection as proof that the port is free and treats a timeout as inconclusive. It reused the ping timeout, which defaults to 800 ms. Windows does not refuse a connection to a closed loopback port at once: it retries for about two seconds first. Measured on Windows 11:
127.0.0.1portSo on Windows the check times out on a free port and the backend is never spawned. Linux refuses at once, which is why CI did not catch it.
Summary / 改动概述
src-tauri/src/app_constants.rs: addBACKEND_ENDPOINT_PROBE_MIN_TIMEOUT_MS = 3_000.src-tauri/src/backend/http.rs:confirm_backend_endpoint_availablewaits at least that long. An endpoint that answers still returns at once, and a timeout is still treated as inconclusive, so what fix(backend): confirm the previous backend exited before respawning on restart #191 protects against is unchanged. A free port now costs about two seconds on Windows before the first spawn.Verification / 验证方式
Windows 11,
cargo test --manifest-path src-tauri/Cargo.toml --locked:main(d7a97c9)The nine failures on
mainare the three endpoint probe tests inbackend::http,backend::restart::tests::failed_endpoint_confirmation_retains_the_pid_for_a_retry, and fivebridge::commands::tests::run_native_update_install_*tests. All of them wait for a closed loopback port to be reported as free.cargo fmt --all -- --checkpasses.Follow-up commit
3fee1c2(review feedback): each resolved address now gets the whole timeout instead of sharing one deadline, with a test that resolves to two closed loopback addresses. Windows 11 after it: 281 passed, 0 failed.Linux (Docker,
rust:1-bookworm, rustc 1.100.1, the same steps ascheck-rust.yml) at3fee1c2:cargo fmt --checkpasses,cargo clippy --all-targets -- -D warningspasses,cargo test290 passed, 0 failed.Not verified: I did not build and run a packaged Desktop installer with this change, and I did not run it on macOS.
cargo clippyon Windows (rustc 1.100.0) reports two errors inshutdown_wndproc, which this change does not touch.Checklist / 检查清单
src-tauri/Cargo.lock, lockfiles under changed package dirs). / 如果引入或升级依赖,已同步更新相关 lock 文件(如src-tauri/Cargo.lock、对应包目录中的 lock 文件)。Models used: Claude Fable 5.2 (Claude Code).
Summary by Sourcery
Wait long enough for Windows to confirm that a free backend endpoint is available before blocking a managed backend start.
Bug Fixes:
Enhancements:
Tests: