Repository navigation
Conversation
Thanks, but hard to review/test/merge until the actual |
310a28e to
50687c3
Compare
|
Done - this PR is now the whole change, rebased on current The five steps are kept as separate commits so the history is still reviewable in order: The part I would most like a second opinion on is still the one from the issue: the internal logging process as the home for the broker, and Two things worth knowing before testing:
There is one overlap I could not resolve on my own: #5151 touches the same |
50687c3 to
37dfaa4
Compare
|
Rebased on current |
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The opt-out is ineffective for detached containers, one advertised option combination fails, and detach/exit races can produce false drain errors.
Review effort: Balanced
Findings: 4
Open (6)
Avoid blocking detach notification send during concurrent status handling · New Persist disableAttachBroker to prevent unintended broker startup · New Handle detached TTY with --log-driver none without terminalBrokerIO · New Make detach notification non-blocking during concurrent status handling · New Only enable stdin fan-in for tasks that have stdin · New Only enable stdin fan-in for tasks that have stdin · New
What changed in this PR
Adds a logging-process-backed attach multiplexer so multiple sessions can safely share container stdio.
Changes:
- Adds the attach protocol, broker, client, Unix socket transport, and stable stdin FIFO.
- Routes
attach, foregroundrun, andstart -athrough the broker where supported. - Adds compatibility controls, documentation, and extensive unit/integration coverage.
| File | Description |
|---|---|
pkg/taskutil/taskutil.go |
Selects broker-backed task IO. |
pkg/logging/none_logger.go |
Makes the none logger synchronous. |
pkg/logging/loguri/loguri.go |
Classifies internal log URIs. |
pkg/logging/loguri/loguri_test.go |
Tests log URI classification. |
pkg/logging/logging.go |
Tees raw output into the broker. |
pkg/logging/logging_test.go |
Tests logging and broker integration. |
pkg/logging/broker_unix.go |
Starts the Unix attach broker. |
pkg/logging/broker_other.go |
Provides unsupported-platform fallback. |
pkg/containerutil/containerutil.go |
Adds broker streaming to start. |
pkg/config/config.go |
Adds the broker opt-out setting. |
pkg/cmd/container/remove.go |
Removes stale attach sockets. |
pkg/cmd/container/create.go |
Configures broker-aware logging. |
pkg/cmd/container/attach.go |
Selects socket or legacy FIFO attach. |
pkg/cioutil/stdin_fifo.go |
Defines stable stdin FIFO paths. |
pkg/cioutil/stdin_fifo_windows.go |
Adds Windows fallback. |
pkg/cioutil/stdin_fifo_unix.go |
Creates stable Unix FIFOs. |
pkg/cioutil/stdin_fifo_test.go |
Tests stable FIFO handling. |
pkg/cioutil/container_io.go |
Pins task stdin to stable FIFOs. |
pkg/attachmux/socket_unsupported.go |
Stubs unsupported transports. |
pkg/attachmux/socket_unsupported_test.go |
Tests unsupported transport behavior. |
pkg/attachmux/socket_test.go |
Tests socket lifecycle and dialing. |
pkg/attachmux/socket_supported.go |
Implements Unix socket transport. |
pkg/attachmux/proto.go |
Defines attach framing and controls. |
pkg/attachmux/proto_test.go |
Tests protocol framing. |
pkg/attachmux/client.go |
Implements attach sessions. |
pkg/attachmux/client_test.go |
Tests client streaming behavior. |
pkg/attachmux/broker.go |
Implements stdio fan-out and fan-in. |
pkg/attachmux/broker_test.go |
Tests broker concurrency and limits. |
pkg/attachmux/attachmux.go |
Defines package paths and errors. |
pkg/attachmux/attachmux_test.go |
Tests socket path derivation. |
docs/dev/attach.md |
Documents attach architecture. |
docs/config.md |
Documents the opt-out setting. |
docs/command-reference.md |
Updates attach behavior documentation. |
cmd/nerdctl/main.go |
Registers the global opt-out flag. |
cmd/nerdctl/image/image_convert_test.go |
Updates test root flags. |
cmd/nerdctl/helpers/flagutil.go |
Propagates the opt-out option. |
cmd/nerdctl/container/container_run.go |
Streams foreground TTY runs via broker. |
cmd/nerdctl/container/container_attach.go |
Updates attach command help. |
cmd/nerdctl/container/container_attach_linux_test.go |
Adds multi-session integration tests. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
37dfaa4 to
46304f0
Compare
|
Rebased on current
Two were real defects worth the catch. The opt-out did not back the change out for detached containers, which are the ones it matters most for, because the flag never reached the process that owns the stdio. And the detach announcement could deadlock against a simultaneous exit, reporting a drain timeout and a failed session for a container that ran fine. |
|
Please tidy up commits (1 topic per 1 commit) |
Use a per-container stdio broker to share container output across attach sessions and foreground terminal runs. Add a stable stdin FIFO and document the disable_attach_broker opt-out. Signed-off-by: Eugene Kalinin <[email protected]>
46304f0 to
8ac8994
Compare
Done. I’ve consolidated the implementation into one commit & rebased it onto main. |


Fixes #4374. Design discussion: #3570 (comment) and the follow-up on the switch.
This was five stacked pull requests; per review feedback on the first one it is now a single PR with the history kept separate. Rebased on current
main.The problem
A container's stdio FIFOs cannot be shared. A FIFO has one queue, so two
attachsessions on the same container each receive a random subset of its output: one terminal appears to freeze while the other prints characters it never asked for. And a container started bynerdctl run -dcannot be attached to at all, because its output already went to the logging process.dockerddoes not have this problem because it owns the container's stdio and fans it out. nerdctl is daemonless, so some other process has to own it.The approach
The owner is the internal logging process, the one nerdctl already runs as
_NERDCTL_INTERNAL_LOGGING. containerd spawns it from the container's log URI, so it exists for the container's whole lifetime and comes back on its own when the restart monitor recreates a task. It gains a unix socket, a fan-out of the container's raw output tapped before the log driver's line splitting, and the write end of a stable stdin FIFO that every session's input is merged into.nerdctl attach,run -itandstart -abecome clients of it.docs/dev/attach.mdis the long version.Commits
feat(attachmux)×4,fix(attachmux)×4pkg/attachmux: frame protocol, broker, client, unix socket transport. Touches no existing filefeat(cioutil)<dataStore>/containers/<ns>/<id>/stdin.fiforefactor(logging)feat(logging)pkg/logging/logurifeat(taskutil)--log-driver nonegoes through the internal loggerfix(logging)×3nonedriver off the buffered channelsfeat(config)disable_attach_brokeras a way back to the old stdiofeat(attach)nerdctl attachdials the socket; several sessions at oncefeat(run)run -itstreams through the brokerdocs(attach)docs/dev/attach.mdInvariants, each with a test
nerdctl runwith attached stdio stops draining container stdout at ~72 KiB, deadlocking the container (nerdctl 2.3.5, containerd 2.3.3, Kata runtime-rs) #5137 / Fix foreground stdio deadlock when the internal logging process stops consuming #5151.<-exitChis not on its own evidence: containerd delivers Wait RPC failures on the same channel as a syntheticExitStatus, which is whatnerdctl runwith attached stdio stops draining container stdout at ~72 KiB, deadlocking the container (nerdctl 2.3.5, containerd 2.3.3, Kata runtime-rs) #5137 and Fix foreground stdio deadlock when the internal logging process stops consuming #5151 are about. Rebased on Fix foreground stdio deadlock when the internal logging process stops consuming #5151,containerExitedis closed after its retry loop, which is reached only by thebreakon a clean status - the two returns inside the loop leave the goroutine without going past it.attachasks containerd, through thecio.Attachcallback thatcontainer.Taskhands the task's recordedStdin/Stdout/Stderr. It cannot be cached: the restart monitor recreates a task from the stored log URI without nerdctl running at all, so a container that had FIFO stdio can come back with URI stdio between two invocations.Compatibility
A container created by an older nerdctl, or on a platform where the socket is not implemented, keeps its FIFO stdio and attaches the way it always did, one session at a time. The classification and the IO construction happen in the same
container.Taskcall, so the restart monitor cannot swap one for the other in between.disable_attach_broker(--disable-attach-broker,NERDCTL_DISABLE_ATTACH_BROKER,nerdctl.toml) is the way back. It leaves the data store empty at task creation, which is what a container with a foreign log driver already looks like, so every branch takes the legacy path with no new code. Off by default. It only affects containers created while it is set: a running container's stdio is already bound.The transport is built for
linux || freebsd, matching where the logging process is built. ElsewhereProbefails andrun -itpicks the legacy FIFO path, which is exactly today's behaviour.Known limitations, stated in
docs/dev/attach.mdbinaryscheme setspio.copy = falseandbinaryIO.Stdin()returns nil, so stdin is not wired at all.cio.TerminalLogURI, whose config carries noStdin, so the shim never opens the read end of the stable FIFO. Not a regression: such a container has no stdin today either. Closing it needs the restart monitor to preserve the full IO config, which is an upstream containerd change.Testing
go test -race ./pkg/...: green, including 53 tests inpkg/attachmuxand the new cases inpkg/logging.go vetandgolangci-lintclean for linux, windows, freebsd and darwin.New integration tests in
cmd/nerdctl/container/container_attach_linux_test.go:TestAttachMultipleSessions,TestAttachToDetachedContainer,TestAttachToDetachedContainerWithoutTTY,TestAttachAfterDetachKeepsContainerResponsive.The switch was exercised on a built binary: default off, env on, toml on, env beating toml, flag beating both.