Repository navigation
fix(web-fetch): prove native CDP server ownership before connecting - #228
Conversation
There was a problem hiding this comment.
This review found 1 issue(s), including 1 blocking issue(s).
| browser = playwright.chromium.connect_over_cdp( | ||
| f'http://{_NATIVE_CDP_HOST}:{port}', timeout=remaining() * 1000) |
There was a problem hiding this comment.
P2 — Ownership observation does not reserve the port until the handshake. If the launched server exits after _process_listens_on returns true, another process can bind the released address before connect_over_cdp sends its HTTP request. Keeping the child unreaped prevents PID reuse, not socket-address reuse. The subsequent process.poll() rejects the browser but cannot undo requests sent to that foreign listener, so the stated “another listener is never contacted” requirement remains incomplete. The existing exit-during-handshake test connects before stopping the server and does not cover this ordering. Protect the connection against port replacement (another pre-connect poll alone is insufficient), and add a deterministic regression that stops the verified server, binds a foreign listener before the handshake, and asserts zero foreign requests. This is P2 because it requires a narrow server-exit/port-rebinding race.
There was a problem hiding this comment.
✅ Re-review complete · a4d8d46
No findings in this incremental review.
There was a problem hiding this comment.
Independent review, round 1
Head reviewed: a4d8d460. No defects found (no P0, P1, P2 or P3 findings).
What I verified
The fix itself
- I read
_connect_native_cdp,_process_listens_on, and the Linux and macOS helpers against the code they replace. Nothing touches the port before the ownership check passes, because the old TCP probe is gone. The/proc/<pid>/net/tcpcolumns, the byte order of the address and thelsof -Fnoutput match use. NATIVE_CDP_BIND_EXIT_GRACE_SECONDSand the handshake-error return path have no remaining references. In Web Fetch,native_cdp_browseris the only code that launches a native server, andheadless_worker.py:261calls it. Browser Trajectory Replay launches Moli, Lightpanda and Obscura separately inremote_execution.py. Issue #170 is scoped tonative_cdp_browserand this PR does not change that track, so I did not review it here.- The worker has no
waitpidor other reaper, so the claim that the pid cannot be reused holds.
Do the tests fail without the fix?
Restoring the old headless.py outright fails 15 of 16 tests, but only because the harness replaces the socket module, so that says nothing about the fix. I used two targeted mutations instead:
| Mutation | Result |
|---|---|
| Replace the proof with a TCP probe | The second-listener, lingering-server, silent-listener and wildcard tests fail, plus the two forced-port restart tests |
| Accept any address on the port | Only the wildcard test fails |
Real server: I ran Moli 1.1.17 with real Playwright on macOS. A free port worked. In 5 of 5 rounds with a second Moli holding the forced port, the call restarted on a new port. The second Moli was then stopped and a page still loaded through the returned browser.
Test runs
tests/test_native_cdp_port.pypassed 16 of 16 in three repeat runs.test_native_cdp_port.py,test_golden_reports.py,test_headless_engine_routes.pyandtest_headless.pytogether gave 86 passed, 1 failed. The failure istest_converter_shared_and_nonzero_not_converted, on the known local failure list. The golden route tests pass and the diff touches no snapshots.- Both ruff commands pass.
- CI is green on this head. The Linux job ran all of
tests, which includes the/procpath through the stand-in servers (19866 passed, 157 skipped).
PR description and repository rules
headless_worker.py,worker_sha256,execution_sha256andadapter_input_sha256are unchanged, and those hashes cover only the worker file.ContainerResourceRunner.validate()compares only the worker hash, andresource.Dockerfilestill hasCOPY harness ./harness. The resource image section is therefore accurate.uv.lockis unchanged, the diff touches onlyheadless.pyand its test file, and I found no attribution lines.
Points for the owner (not defects)
Closes #170and the port-replacement race: the bot's inline comment on the handshake describes a real window. If the proven server exits and another process binds the address before the handshake, that process is contacted. The issue's condition is that a connection is used only after proof. That still holds, because this browser is closed before a caller receives it, andtest_listener_that_takes_the_port_after_the_proof_is_never_given_to_the_callercovers it. I agree with the closure, and the PR correctly leaves the choice of a relay to the owner.- Not verified on the image: there is no Docker daemon here, so I did not run the Linux binaries. The image pins Moli 1.1.1 and Obscura v0.2.1, which may differ from the macOS versions measured. If one of them listens from a child process or ignores
--host, it fails with aTimeoutErrorrather than a silent wrong result. The first rebuilt-image cohort will show this.
Wait until the kernel reports the launched server as the owner of the listening socket on its exact loopback address before any connection to that port. Linux reads /proc and macOS asks lsof, so no dependency is added. A second listener is never contacted, so the handshake no longer needs the post-failure exit grace.
State what the proof does not reserve: a server that exits after it can lose the address before the handshake. The browser from that handshake is discarded before a caller gets it, and a test now forces that order.
a4d8d46 to
f76fb41
Compare
Issues
Closes #170
Summary
native_cdp_browsernow connects only after the kernel reports the launched server as the owner of the listening socket on127.0.0.1:<port>. Until then nothing touches the port, including the old TCP probe, so a listener already on that port is never contacted./proc/<pid>/net/tcplistening rows to the socket inodes in/proc/<pid>/fd./usr/sbin/lsoffor that process and port.psutilis not needed anduv.lockis unchanged.Behavior changes:
TimeoutErrorat the deadline, no handshakeNATIVE_CDP_BIND_EXIT_GRACE_SECONDSand the handshake-error return path are removed, since they only existed to tell a foreign rejection apart. The check for a server that exits while its browser is in use stays, and its message no longer says the result may come from another process.Limits
SO_REUSEPORT: two sockets can share an address only if both set it. All three servers exit on a taken port (measured below), so this does not apply to them./procpath is exercised by the stand-in tests in CI, not by real Linux servers.Resource image
headless_worker.pyis unchanged, soworker_sha256,execution_sha256andadapter_input_sha256keep their values.headless.py, and the worker imports it from there.ContainerResourceRunner.validate()compares only the worker hash, so a stale image still passes and keeps the old behavior. Local runs pick up the fix at once./proc, and the worker and server run as the same user.resource.Dockerfilestill hasCOPY harness ./harness, butharness/moved tosrc/harness/. I could not rundocker buildhere, and this PR does not fix it.Test plan
Everything below ran on macOS. Each of the two result tables was one run.
tests/test_native_cdp_port.py,test_golden_reports.py,test_headless_engine_routes.py,test_headless.py,test_headless_files.py,test_network_dns.pyandtest_container_resources.pygave 108 passed, 8 skipped, 1 failed. The failure istest_converter_shared_and_nonzero_not_converted, on the known local failure list. Golden route hashes are unchanged.pytest -o pythonpath="src ."rather than thePYTHONPATHenvironment variable, which this environment's command guard refused. Both import this checkout. CI runs the full suite.test_native_cdp_port.py,test_golden_reports.pyandtest_headless_engine_routes.pygave 40 passed. CI passed on the final head.Real servers and real Playwright, with a second real server of the same engine holding the forced port. That second server is then stopped, and a local page is loaded through the returned browser:
The 2 Lightpanda errors came from my script's own 5 s wait for the second server to stop (
TimeoutExpiredon that process). No result was taken from those two rounds, so they do not show the ownership path passing or failing. Lightpanda also takes 1.9 to 3.9 s to exit on a taken port, which is why entry takes longer in that case.Median time to enter
native_cdp_browseron a free port, 15 rounds each with a median of one run, so differences of this size are close to noise: