Repository navigation
Conversation
There was a problem hiding this comment.
✅ Lambda Tooling PR Reviewer: No high-confidence issues found.
1 lower-confidence observation: these passed verification at a lower score, so check before acting
1. [CONCURRENCY] samcli/local/docker/container.py:464: Clearing rapid_port_host before reload() lets concurrent readers of a running container see None
_read_rapid_port_host sets self.rapid_port_host = None, then makes a Docker API round-trip, and only then writes the port back. DurableLambdaContainer.wait_for_result calls self.start() on every invocation, and warm runtimes share one already-running container across request threads. During that round-trip, another thread can read None in get_port() (giving the emulator http://localhost:None) or in _make_http_request. To check, run concurrent durable invokes against a warm container. A fix is to compute the port into a local variable and assign self.rapid_port_host once, after reload() returns.
🧪 Hands-on test plan (not run: external contributor)Commit Summary: Run the touched unit tests, then exercise sam local invoke and sam local start-lambda against real Docker: basic invoke, concurrent RequestResponse and Event invokes (the reported race), EAGER warm containers with a check of the published host port range, a 0.0.0.0 host interface, and a container that exits immediately. Comparing with the merge base shows the port range change and may show the race. For a maintainer to check:
Planned checks (6):
Commands🤖 Automated hands-on test — not a review approval · model claude-opus-5.5 · commit History (2)
|
bnusunny
left a comment
There was a problem hiding this comment.
Summary
The design is the right fix for the race. Probing for a free port in __init__ and binding it at start() is a time-of-check/time-of-use (TOCTOU) race, and two containers starting together can pick the same port. Publishing with host port None and reading the assigned port back after start() removes the race entirely. Removing find_free_port/NoFreePortsError is consistent with that. The tests are adequate: create publishes None, start reads the binding, a restart clears the stale port, and _can_connect_to_socket handles None.
Blocking:
- Merging onto current develop breaks the build.
git merge-treereports clean, but the merged tree hasF821 Undefined name ContainerNotStartableExceptionatsamcli/local/docker/container.py:487, andtests/unit/local/docker/test_container.pyfails to collect (ImportError: cannot import name 'ContainerNotStartableException' from 'samcli.local.docker.container'). Cause: #9294 (1eee8173, merged 2026-11-06) added aContainerNotStartableExceptionraise instart()for a missing host interface, and this PR removes that import. Please rebase onto develop and keep the import. - Sequencing. bnusunny asked to wait for #9290, which also edits
samcli/local/docker/utils.py/test_utils.py.git merge-tree pr-9290 pr-9388conflicts in both files. Since this PR deletesfind_free_portand #9290 rewrites it, whichever lands second must reconcile. If this one lands, most of #9290'sutils.pychanges become dead code.
Non-blocking, worth addressing in the same revision:
- Concurrency window (bot observation, legitimate).
_read_rapid_port_hostsetsrapid_port_host = Noneand then does a Docker round-trip.DurableLambdaContainer.wait_for_resultcallsself.start()on every invoke against a warm, already-running container, so a concurrent thread can readNoneand buildhttp://localhost:None. Fix: compute into a local and assign once afterreload(). - Behaviour change. The RAPID host port moves from 5000–9000 to Docker's ephemeral range (typically 32768+). That is invisible to most users, but call it out in the PR body and the release notes. It matters for anyone with firewall rules or Windows excluded-port ranges tuned to the old range.
- Engine compatibility. Please confirm that
container.portsis populated afterreload()on Finch and Podman, which SAM supports viaContainerClientFactory, not only on Docker. The hands-on plan in the bot comment was never run because the author is an external contributor. ContainerNotStartableException"no free ports" test fixtures intests/unit/commands/local/*/test_cli.pynow describe a condition that can't happen. That's fine to keep as generic exception tests, but the message strings are stale.
CI: only the bot reviewer (pass) and hands-on (skipped) ran. The main CI workflows have not run on this head.
Inline comments
samcli/local/docker/container.py:33: after rebasing onto develop, keepContainerNotStartableExceptionin this import. #9294 raises it instart()for a missing host interface (merged tree failsruffwith F821 at line 487).samcli/local/docker/container.py:460: assign once instead of clearing first. Sketch:This closes the window where a concurrent durable invoke on a warm container readsreal_container.reload() bindings = real_container.ports.get(f"{self.RAPID_PORT_CONTAINER}/tcp") self.rapid_port_host = int(bindings[0]["HostPort"]) if bindings else None
None.samcli/local/docker/container.py:464:bindings[0]: with--container-host-interface 0.0.0.0, Docker can return both an IPv4 and an IPv6 binding, and with the userland proxy they have historically been able to differ. Prefer the binding whoseHostIpis0.0.0.0/the requested interface, or add a comment explaining why the first one is always right.samcli/local/docker/container.py:245: please mention the port-range change in the PR description (Docker ephemeral range instead of 5000–9000).samcli/local/docker/container.py:649: good guard. Consider aLOG.debugwhen the port is unset because the container exited, so a user reading--debugsees why the socket wait times out.
find_free_port picked a random port in 5000-8999 with a bind-and-close probe and nothing held it until Docker bound it at start, so containers starting at the same time could pick the same port and fail with "port is already allocated". Publish the RAPID port with no host port so Docker allocates it at start, then read the assigned port back on every start. A container that exits before the read has no bindings; the port stays unset and the socket check reports it as unreachable. Remove find_free_port and NoFreePortsError, which have no other users. Fixes aws#9387 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
test_must_not_keep_previous_rapid_host_port_on_restart already covers a container with no port bindings after start. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Assign rapid_port_host once after reload() so concurrent invokes on a warm container never read None, prefer the binding on the requested interface, and log at debug when the container has no binding. Update the stale "no free ports" test strings. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
03b6453 to
403a17c
Compare
|
@bnusunny rebased onto develop after #9290 and #9294:
|
|
@bnusunny a gentle ping on this one. The points from your review of Fresh numbers, 20 rounds of 50 concurrent Docker Desktop 4.100.0 (engine 29.9.3):
Finch 1.19.0 (nerdctl 2.2.2, containerd 2.2.1), SAM confirmed to be
Podman is not tested. Could you approve the CI run on this head? Happy to change anything |
There was a problem hiding this comment.
Thanks for the rebase and the Finch numbers. All the blocking points from my 11-13 review are fixed. I merged this with current develop and checked it: ruff, mypy and unit tests pass, and start-lambda handled 75 concurrent invokes with no "port is already allocated" errors.
One non-blocking thing:
- The PR body says
ContainerNotStartableExceptionis no longer raised, but #9294 still raises it for a missing host interface. Please keep that in mind in the follow-up so those handlers stay.
Which issue(s) does this change fix?
Fixes #9387
Why is this change necessary?
find_free_portpicks the RAPID host port before Docker binds it, so concurrent containers can pick the same port and fail withport is already allocated. Forstart-lambdaEventinvokes, the function silently never runs.How does it address the issue?
Publish the RAPID port with no host port so Docker assigns it at start, then read it back after
start(). This removes the now-unusedfind_free_portandNoFreePortsError. With the #9387 repro (20 rounds of 50 concurrent invokes),developfailed in 7 rounds and this PR in 0.What side effects does this change have?
The host port comes from Docker's dynamic range instead of 5000–8999. Follow-up:
ContainerNotStartableExceptionis no longer raised, so its handlers are now dead code.Mandatory Checklist
PRs will only be reviewed after checklist is complete
make prpasses:schema,black-check,lintandtest-allpass (coverage 94.15%)make update-reproducible-reqsif dependencies were changed: no dependency changesBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
🤖 Generated with Claude Code