Skip to content

fix(backend): wait for the Windows loopback refusal before refusing to start - #204

Closed
liquidtoy001 wants to merge 2 commits into
AstrBotDevs:mainfrom
liquidtoy001:fix/endpoint-probe-windows-refusal-delay
Closed

liquidtoy001 wants to merge 2 commits into
AstrBotDevs:mainfrom
liquidtoy001:fix/endpoint-probe-windows-refusal-delay

Conversation

@liquidtoy001

@liquidtoy001 liquidtoy001 commented Dec 2, 2026 •

Copy link
Copy Markdown

Fixes #203.

After updating to Desktop 4.30.0 on Windows the backend never starts. The startup page shows:

Cannot confirm backend endpoint http://127.0.0.1:6185 is available: connection to 127.0.0.1:6185 failed: connection timed out. Refusing to start another backend.

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:

connect timeout result for a closed 127.0.0.1 port
0.8 s timed out after 0.80 s
5 s connection refused (10061) after 2.04 s

So 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: add BACKEND_ENDPOINT_PROBE_MIN_TIMEOUT_MS = 3_000.
  • src-tauri/src/backend/http.rs: confirm_backend_endpoint_available waits 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.
  • Tests: one new test for the default ping timeout against a closed loopback port. Three existing tests that hard-coded 200 ms now use the same constant; they failed on Windows before this change.

Verification / 验证方式

Windows 11, cargo test --manifest-path src-tauri/Cargo.toml --locked:

passed failed
main (d7a97c9) 270 9
this branch 280 0

The nine failures on main are the three endpoint probe tests in backend::http, backend::restart::tests::failed_endpoint_confirmation_retains_the_pid_for_a_retry, and five bridge::commands::tests::run_native_update_install_* tests. All of them wait for a closed loopback port to be reported as free.

cargo fmt --all -- --check passes.

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 as check-rust.yml) at 3fee1c2: cargo fmt --check passes, cargo clippy --all-targets -- -D warnings passes, cargo test 290 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 clippy on Windows (rustc 1.100.0) reports two errors in shutdown_wndproc, which this change does not touch.


Checklist / 检查清单

  • This change is not a breaking change. / 此改动不是破坏性变更。
  • I have verified the change locally (and provided logs/screenshots above). / 我已在本地验证并在上方提供日志/截图。
  • If workflows are changed, I attached at least one related Actions run link. / 如果修改了工作流,我已附上至少一个相关 Actions 运行链接。
  • If dependencies are updated, lockfiles are updated accordingly (src-tauri/Cargo.lock, lockfiles under changed package dirs). / 如果引入或升级依赖,已同步更新相关 lock 文件(如 src-tauri/Cargo.lock、对应包目录中的 lock 文件)。
  • This PR does not include malicious code. / 此 PR 不包含恶意代码。

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:

  • Prevent backend startup from being incorrectly refused when Windows takes longer to report that a closed loopback port is unavailable.

Enhancements:

  • Allow each resolved endpoint address the full probe timeout, so multiple closed addresses can be checked without prematurely timing out.

Tests:

  • Add regression coverage for slow Windows loopback refusals and multiple closed addresses, and update existing endpoint probe tests to use the minimum timeout.

…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.
@sourcery-ai

sourcery-ai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Linked issues

Checked at commit 3fee1c2

#203 [Bug] 更新4.30.0桌面版无法打开 · 1 of 1 met

# Requirement Status Notes
1 桌面版更新后正常启动。 ✅ Met The endpoint probe now waits at least 3 seconds for a closed port to report refusal, allowing the Windows pre-start check to pass on a free port.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src-tauri/src/backend/http.rs Resolved
Comment thread src-tauri/src/backend/http.rs Resolved
@zouyonghe

Copy link
Copy Markdown
Member

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 backend/http.rs:101, DNS can consume its budget and each resolved address then receives another full timeout. This can extend startup or restart well beyond the configured timeout. The 3-second floor also overrides explicitly shorter values for this check.

#207 has already merged the 5-second default for #203. I'm closing this PR as superseded and keeping the shared endpoint deadline in main. The Windows test results are useful for validating the startup path. Thanks again for investigating it.

@zouyonghe zouyonghe closed this Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 更新4.30.0桌面版无法打开

2 participants