Description
Found during bug bash on 0.10.0-bug-bash-1. Two related defects in Local Quick Start host-port selection. The second is the one that will actually bite users; the first is a correctness/diagnostics problem.
1. The chosen port is released before the image pull and only bound after it
*provision() runs these three steps in order:
const g = await this.runtime.findAvailablePort(QUICK_START_PORT); // probe: bind, then close
...
await this.runtime.pullImage(j, Z.token); // 1.22 GB download
...
Q = await this.runtime.createAndRunContainer({ hostPort: k, ... }); // port actually boundisPortFree() validates a port by binding it and immediately closing:
isPortFree(e=QUICK_START_PORT){return new Promise(g=>{const b=E.createServer();b.once("error",()=>g(!1)),b.once("listening",()=>b.close(()=>g(!0))),b.listen(e,"127.0.0.1")})}Nothing holds the reservation between the probe and createAndRunContainer. The gap spans the entire image pull. On this machine a pull measured 7s with a warm layer cache on a fast link; on a genuine first run the image is 1.22 GB, so the window scales with bandwidth into minutes.
Losing the race needs only one port taken by one process during the pull. When it happens, the friendly Port {0} is already in use… message cannot fire — that check already passed — so the failure surfaces from the daemon instead:
docker: Error response from daemon: ports are not available: exposing port TCP
127.0.0.1:10299 -> 127.0.0.1:0: listen tcp4 127.0.0.1:10299: bind: address already in use
Reproduced by holding a port with a plain listener and then creating the container on it.
Related concern from #840: if this rejection travels the spawnStreamAsync path described there, Docker's stderr is discarded and the consumer sees only Process exited with code <n>. In that case the user gets no diagnostic text at all for what is really just a taken port. Worth confirming whether the CapturingTeeWritable workaround covers the container-create path.
2. findAvailablePort samples ≤11 of 100 ports, then reports all 100 are in use
async findAvailablePort(e=10260,g=10360,b=10){
if(await this.isPortFree(e))return e;
const w=Math.max(1,g-e),C=new Set([e]);
for(let g=0;g<b;g++){
const g=e+Math.floor(Math.random()*w);
if(!C.has(g)&&(C.add(g),await this.isPortFree(g)))return g
}
} // falls through → undefinedIt probes 10260, then draws 10 random ports from the 100-port band. Repeat draws are skipped but still consume an attempt. On undefined the caller emits:
Ports 10260-10359 are all in use. Free one and retry.
That claim is false — at most 11 of the 100 were ever probed.
Measured by running the two functions verbatim from the shipped bundle against real sockets on 127.0.0.1, 200 trials per row. The verified-free column is an independent bind check taken while the trials ran, so those ports were provably available at that moment:
| busy | free | verified-free | "all in use" errors | rate |
|---|---|---|---|---|
| 1 | 99 | 99 | 0 | 0.0% |
| 50 | 50 | 50 | 0–1 | ~0.3% |
| 80 | 20 | 20 | 24 | 12.0% |
| 90 | 10 | 10 | 72 | 36.0% |
| 95 | 5 | 5 | 119 | 59.5% |
| 100 | 0 | 0 | 200 | 100% ✓ correct |
In fairness, the rows where this matters need ~80 of 100 ports in 10260–10359 occupied at once, which is not a realistic desktop state. The practical problem is the false message, not the failure rate. Filing it together with (1) because the same change removes both.
Note on reachability
This is not an edge path. The port comes from the webview form, not from documentDB.local.port:
W = "number" == typeof g?.port ? g.port : void 0With the port field left blank — the default — W is undefined and the auto-select branch runs on every provision.
Suggested fix: let Docker assign the port
Both pieces are already in the codebase.
The bundled client emits an empty host port when hostPort is falsy, which is exactly the Docker-assigns-a-port form:
let g = e.hostIp ? `${e.hostIp}:` : "";
g += `${e.hostPort||""}:`; // undefined → "--publish 127.0.0.1::10260"
g += e.containerPort;And the real bound port is already read back two lines after creation, then used for the connection string:
const B = await this.runtime.inspectContainer(Q),
V = B && getBoundHostPort(B) || k;So the proposed flow is:
- Attempt
createAndRunContainerwith the preferred port (W, elseQUICK_START_PORT) — keeps the stable, predictable 10260 whenever it is genuinely free. - On a port-bind failure from the daemon, retry once with
hostPortomitted. Docker allocates atomically at bind time. getBoundHostPortalready supplies the actual port downstream — no other changes needed.- Delete
findAvailablePort,QUICK_START_PORT_BAND_END, andQUICK_START_PORT_FALLBACK_ATTEMPTS.
Verified end to end with 10260 held by another process:
--publish 127.0.0.1:10260:10260 → bind failed as expected
--publish 127.0.0.1::10260 → Docker assigned host port 61146
getBoundHostPort() would read: 61146 → connection string uses it
This removes the TOCTOU (bind becomes atomic), removes the arbitrary 100-port ceiling in favour of the full ephemeral range, and removes both the random sampling and the false "all in use" message.
Alternative considered and rejected
Turning findAvailablePort into a sequential scan fixes the wrong message and the spurious failure, but leaves the TOCTOU in (1) completely untouched and keeps the 100-port cap. It addresses the lesser of the two defects.
Worth keeping
The isPortFree pre-check on a user-specified port still earns its place: it produces a fast, friendly error before a multi-minute pull rather than after. It just cannot be the only guard, since it is inherently advisory — the creation error needs graceful handling either way, which would also give (1) a proper message instead of raw daemon output.
Related
- Local Quick Start port error messages bypass vscode.l10n.t() and always render in English #852 — the "all in use" string is also not localized. Whatever wording replaces it should go through
vscode.l10n.t()and should not claim the whole range was checked. - Discussion: upstream our Docker failure-diagnosis enhancements to @microsoft/vscode-container-client #840 — Docker error text being discarded on non-zero exit, relevant to how the bind failure surfaces.
Activity
tnaum-ms commentedon Aug 5, 2026
Guanzhou Song (@guanzhousongmicrosoft) good catch - I put this one on hold, I have another review session in progress that looks at some low level issues, once I merge that, we'll revisit that issue whether it's still valid and then return to it.
Fix the Local Quick Start bug-bash issues (#851, #852, #854, #855, #856…
tnaum-ms commentedon Aug 6, 2026
Closing as won't-fix. Recording the reasoning so this does not get quietly re-fixed later.
Both halves of this report were correct.
findAvailablePortreally did sample a handful of ports out of the band and then claim the whole range was occupied, and the probe-release-pull-bind sequence really was a TOCTOU: nothing held the reservation across a 1.22 GB image pull, and by the time the bind failed the friendly "already in use" path had already been passed.What changed is the port model, not the diagnosis. The Local Quick Start code review (finding L3) removed the automatic port mechanism outright. The principle is "no magic after execute":
That closes the same window from the other side. There is no probe-then-release gap to lose, because the port is fixed and validated before anything is pulled. And
findAvailablePortis gone entirely, so the false "all in use" claim cannot be made: it was deleted rather than reworded.Why not also take the bind-time fallback. #866 contained an isolated commit implementing it (
docker rungets the preferred port; a bind conflict retries once with the host port omitted so Docker allocates atomically). It is a genuine robustness improvement for the remaining edge case. We are not taking it, because what it adds on top of the explicit model is an automatic relocation: a user who chose 10260 and ends up on 61146 has to discover that from the success screen. That is the specific behaviour the explicit model exists to remove. The narrower race it would close is not worth reintroducing it.boundPortstill reads the actual binding, so the success screen and the stored connection string cannot drift from reality.Thanks Guanzhou Song (@guanzhousongmicrosoft) for finding it, and for isolating the fix into its own commit so this could be decided cleanly.