Repository navigation
fix(backend): report a closed loopback port immediately on Windows - #205
LuviaSeraphina wants to merge 2 commits into
Conversation
The managed-backend preflight added in AstrBotDevs#191 only accepts a refused connection as proof that the endpoint is free. On Windows a refused connection to a closed loopback port is reported only after the SYN retransmission budget expires, which takes about two seconds, and that is longer than the 800 ms default probe timeout. Every managed start was therefore refused with "connection timed out", so Desktop 4.30.0 could not start the backend at all on Windows (AstrBotDevs#203). Probe loopback targets on a raw Winsock socket with SIO_TCP_INITIAL_RTO / TCP_INITIAL_RTO_NO_SYN_RETRANSMISSIONS, the same opt-out that Chromium, curl and libuv use. A closed port now reports WSAECONNREFUSED in about 10 ms instead of about 2 s. The behaviour around it is unchanged: an endpoint that answers still blocks a respawn, an inconclusive probe still refuses to start another backend, non-loopback addresses keep using TcpStream::connect_timeout, and every failure to prepare the raw socket falls back to that path. On Windows 11 the nine endpoint-probe tests that fail on main pass with this change: the three probes in backend::http, backend::restart::tests::failed_endpoint_confirmation_retains_the_pid_for_a_retry and the five bridge::commands::tests::run_native_update_install_* cases.
Linked issuesChecked at commit bc10839 #203 [Bug] 更新4.30.0桌面版无法打开 · 0 of 1 met · 1 to check by hand
|
There was a problem hiding this comment.
Hey - I've found 4 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="201" />
<code_context>
+ }
+ };
+
+ let raw = unsafe { socket(family, SOCK_STREAM, IPPROTO_TCP) };
+ if raw == INVALID_SOCKET {
+ return None;
</code_context>
<issue_to_address>
**First probe falls back to slow connect**
When the managed-start preflight performs the process's first Winsock operation on a numeric loopback endpoint, `socket` fails with `WSANOTINITIALISED`, so `probe` returns `None` and `connect_probe` falls back to `TcpStream::connect_timeout`; the normal SYN retransmission delay blocks the managed start instead of using the fast probe.
Initialize Winsock before creating the raw socket so the probe can use its fast path.
</issue_to_address>
### Comment 2
<location path="src-tauri/src/backend/http.rs" line_range="239" />
<code_context>
+ };
+ let mut returned = 0u32;
+ unsafe {
+ WSAIoctl(
+ socket,
+ SIO_TCP_INITIAL_RTO,
</code_context>
<issue_to_address>
**Rejected RTO option stalls the probe**
When the OS or socket provider rejects `SIO_TCP_INITIAL_RTO`, `disable_syn_retransmissions` discards the `WSAIoctl` result, so `probe` continues with default SYN retransmissions; `wait_writable` reaches the probe timeout before the connection is reported, and the endpoint check refuses to start the backend.
Check the `WSAIoctl` result and handle failure without continuing the probe with SYN retransmissions enabled.
</issue_to_address>
### Comment 3
<location path="src-tauri/src/backend/http.rs" line_range="908" />
<code_context>
+ drop(listener);
+
+ let started = Instant::now();
+ let error = connect_probe(address, Duration::from_millis(5_000))
+ .expect_err("a closed loopback port must be reported as refused");
+ let elapsed = started.elapsed();
</code_context>
<issue_to_address>
**Windows regression passes on Linux**
When `cargo test` runs on the configured Ubuntu CI runner, the test `loopback_probe_reports_a_closed_port_without_waiting_for_syn_retransmissions` calls `connect_probe` at `http.rs:908`, but that function's Windows probe is behind `#[cfg(windows)]`. On the configured Ubuntu CI runner, the ordinary Linux connect path refuses quickly, so the test passes even if the Windows-specific fix is broken.
Add a Windows CI test run for this regression test.
</issue_to_address>
### Comment 4
<location path="src-tauri/src/backend/http.rs" line_range="914" />
<code_context>
+
+ assert_eq!(error.kind(), ErrorKind::ConnectionRefused, "{error}");
+ assert!(
+ elapsed < Duration::from_millis(1_000),
+ "a closed loopback port must not wait for the SYN retransmission budget: {elapsed:?}"
+ );
</code_context>
<issue_to_address>
**Slow probes still pass**
When a closed-port probe returns `ConnectionRefused` after 800–999 ms, the test `loopback_probe_reports_a_closed_port_without_waiting_for_syn_retransmissions` allows elapsed time under 1,000 ms at `http.rs:914`, while passing a 5,000 ms timeout at line 908. A probe that refuses after 900 ms passes the test but times out when the endpoint check uses its normal 800 ms budget.
Use the normal 800 ms probe timeout in the test and assert that the closed port is refused within that budget.
</issue_to_address>Sourcery assessment
Approval pending. 4 findings to address first.
Blocking findings: src-tauri/src/backend/http.rs:201, src-tauri/src/backend/http.rs:239, src-tauri/src/backend/http.rs:908, src-tauri/src/backend/http.rs:914
| drop(listener); | ||
|
|
||
| let started = Instant::now(); | ||
| let error = connect_probe(address, Duration::from_millis(5_000)) |
There was a problem hiding this comment.
🟡 Medium · Windows regression passes on Linux
When cargo test runs on the configured Ubuntu CI runner, the test loopback_probe_reports_a_closed_port_without_waiting_for_syn_retransmissions calls connect_probe at http.rs:908, but that function's Windows probe is behind #[cfg(windows)]. On the configured Ubuntu CI runner, the ordinary Linux connect path refuses quickly, so the test passes even if the Windows-specific fix is broken.
Add a Windows CI test run for this regression test.
Prompt for AI agents
In `src-tauri/src/backend/http.rs` at line 908:
**Windows regression passes on Linux**
When `cargo test` runs on the configured Ubuntu CI runner, the test `loopback_probe_reports_a_closed_port_without_waiting_for_syn_retransmissions` calls `connect_probe` at `http.rs:908`, but that function's Windows probe is behind `#[cfg(windows)]`. On the configured Ubuntu CI runner, the ordinary Linux connect path refuses quickly, so the test passes even if the Windows-specific fix is broken.
Add a Windows CI test run for this regression test.Address the review on AstrBotDevs#205: - Call WSAStartup once before the first raw socket. A probe can be the process's first Winsock user, and socket() would then fail with WSANOTINITIALISED and drop the preflight back to the slow path. - Treat a rejected SIO_TCP_INITIAL_RTO as "raw probe unavailable" and fall back to TcpStream::connect_timeout, instead of waiting for a refusal that the default SYN retransmissions will not deliver inside the budget. - Run the regression test on Windows only: the Linux connect path always refuses at once, so it cannot regress there. The test now uses the normal 800 ms probe budget and asserts the refusal arrives well inside it.
|
All four points are addressed in bc10839:
Full suite on Windows 10 26H2 (build 26300.9974) is unchanged at 232 passed / 48 failed, and the 48 are the same environmental One gap I cannot close from this PR: |
|
Windows verification update for bc10839: |
|
Thank you for investigating the Winsock behavior, benchmarking the loopback probe, and rerunning the Windows test suite. The reported drop from 2,043 ms to 14 ms for the individual check makes this worth pursuing as a startup performance improvement. #207 has already merged the 5-second default for #203. Please update this branch from
Please verify the updated branch on Windows with both a closed port and an active listener, and include the cold-start timings. A focused Windows CI test would also cover the Winsock path that the current Ubuntu Rust job skips. |
Fixes #203.
Desktop 4.30.0 refuses to start the backend on Windows. Since #191 the endpoint check runs before every managed start, and it only accepts a refused connection as proof that the port is free. Windows does not refuse a connection to a closed loopback port immediately: the stack retransmits the SYN and only then reports
WSAECONNREFUSED, which takes about two seconds, while the probe timeout defaults to 800 ms. The check therefore always times out on a free port and Desktop answers withCannot confirm backend endpoint ... connection timed out. Refusing to start another backend., so the backend is never spawned.Summary / 改动概述
src-tauri/src/backend/http.rs: the per-address probe now goes throughconnect_probe, which keepsTcpStream::connect_timeoutsemantics but, on Windows loopback targets, connects on a raw Winsock socket withSIO_TCP_INITIAL_RTOset toTCP_INITIAL_RTO_NO_SYN_RETRANSMISSIONS— the same opt-out Chromium, curl and libuv use for loopback. A closed port is reported in about 10 ms instead of about 2 s.already answering), a timeout is still inconclusive and refuses to start a second backend, non-loopback addresses keep usingTcpStream::connect_timeout, and any failure to prepare the raw socket falls back to that same path.src-tauri/Cargo.toml: enable theWin32_Networking_WinSockandWin32_System_IOfeatures ofwindows-sys, which is already a Windows-only dependency. No new crate and noCargo.lockchange.Relationship to #204: that PR raises the probe floor to 3 s so the timeout absorbs the two seconds. This change removes the two seconds instead, so the probe keeps its configured timeout and every managed start stays fast. They are alternatives rather than additions — happy to fold this into #204 if you would rather keep one thread.
Verification / 验证方式
Windows 10 26H2 (build 26300.9974), Desktop 4.30.0, rustc 1.100.1,
cargo test --manifest-path src-tauri/Cargo.toml --locked.Timing of a probe against a closed loopback port (
127.0.0.1:6187) on the same machine:TcpStream::connect_timeout(before)ConnectionRefusedafter 2 043 msSIO_TCP_INITIAL_RTOprobe (after)ConnectionRefusedafter 14 msSame suite, same machine:
main@ d7a97c9 (before)The nine tests this fixes are exactly the endpoint-probe ones that fail on
mainon Windows:The remaining 48 failures are environmental and unrelated: they fail identically on
main, becausetempfilecannot create directories under this machine's%TEMP%(PermissionDenied). They are the same set on both sides — no test regresses with this change, and the new test passes.cargo fmt --all -- --checkandcargo clippy --locked --all-targetsare clean.I did not build and run a packaged installer with this change; the verification above is the unit suite plus the timing measurement.
Checklist / 检查清单
windows-sysfeatures only,Cargo.lockunchanged. / 未引入新依赖,仅启用windows-sysfeature,Cargo.lock无变化。Summary by Sourcery
Restore managed backend startup on Windows when the configured loopback port is closed.
Bug Fixes:
Build:
windows-sysWinsock and system I/O features.Tests: