Skip to content

feat(attach): support multiple concurrent attach sessions - #5168

Open
ekalinin wants to merge 1 commit into
containerd:mainfrom
ekalinin:feat/attach-multiplexer
Open

ekalinin wants to merge 1 commit into
containerd:mainfrom
ekalinin:feat/attach-multiplexer

Conversation

@ekalinin

@ekalinin ekalinin commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

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 attach sessions 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 by nerdctl run -d cannot be attached to at all, because its output already went to the logging process.

dockerd does 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 -it and start -a become clients of it.

docs/dev/attach.md is the long version.

Commits

feat(attachmux) ×4, fix(attachmux) ×4 the new pkg/attachmux: frame protocol, broker, client, unix socket transport. Touches no existing file
feat(cioutil) stable stdin FIFO at <dataStore>/containers/<ns>/<id>/stdin.fifo
refactor(logging) tee the container's raw output before the log driver's line splitting
feat(logging) start the broker inside the logging process; new leaf package pkg/logging/loguri
feat(taskutil) declare the stable stdin FIFO on the task; --log-driver none goes through the internal logger
fix(logging) ×3 listener ordering, a Windows log URI path, and keeping the none driver off the buffered channels
feat(config) disable_attach_broker as a way back to the old stdio
feat(attach) nerdctl attach dials the socket; several sessions at once
feat(run) foreground run -it streams through the broker
docs(attach) command reference, command help, docs/dev/attach.md

Invariants, each with a test

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.Task call, 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. Elsewhere Probe fails and run -it picks the legacy FIFO path, which is exactly today's behaviour.

Known limitations, stated in docs/dev/attach.md

  • Multi-session stdin is limited to terminal containers. For a non-terminal one the shim's binary scheme sets pio.copy = false and binaryIO.Stdin() returns nil, so stdin is not wired at all.
  • After a restart-policy restart, sessions are output only. The restart monitor recreates a terminal task through cio.TerminalLogURI, whose config carries no Stdin, 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 in pkg/attachmux and the new cases in pkg/logging.

go vet and golangci-lint clean 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.

@AkihiroSuda

Copy link
Copy Markdown
Member

This PR adds the pkg/attachmux package and nothing else.

Thanks, but hard to review/test/merge until the actual nerdctl attach is implemented in the PR

@AkihiroSuda AkihiroSuda added this to the v2.4.1 milestone Sep 8, 2026
@ekalinin
ekalinin force-pushed the feat/attach-multiplexer branch from 310a28e to 50687c3 Compare September 12, 2026 09:35
@ekalinin ekalinin changed the title feat(attachmux): add the stdio multiplexing package for multi-session attach feat(attach): support multiple concurrent attach sessions Sep 12, 2026
@ekalinin

ekalinin commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Done - this PR is now the whole change, rebased on current main. nerdctl attach, nerdctl run -it and nerdctl start -a all go through it, so it can be tested end to end.

The five steps are kept as separate commits so the history is still reviewable in order: pkg/attachmux on its own first (8 commits, no existing file touched, no containerd needed to review it), then the logging process becoming the owner of the container's stdio, then the switch, then the two commands, then the docs.

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 <dataStore>/attach/<hash>.sock as the socket location. Everything else follows from those two.

Two things worth knowing before testing:

  • disable_attach_broker (--disable-attach-broker, NERDCTL_DISABLE_ATTACH_BROKER, nerdctl.toml) is the way back to the old stdio. Off by default, and it only affects containers created while it is set - a running container's stdio is already bound.
  • The transport is linux || freebsd. Elsewhere Probe fails and run -it takes the legacy FIFO path, which is today's behaviour.

There is one overlap I could not resolve on my own: #5151 touches the same task.Wait result in run. This PR only announces an exit to sessions when status.Error() == nil, which is the same distinction #5151 is making, so the two should compose - but whichever lands second will need a rebase, and I am happy to be the one to do it.

@ekalinin
ekalinin force-pushed the feat/attach-multiplexer branch from 50687c3 to 37dfaa4 Compare September 18, 2026 07:57
@ekalinin

ekalinin commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Rebased on current main, conflicts resolved - #5151 landed in the meantime, which is the overlap I mentioned above.

@AkihiroSuda
AkihiroSuda requested a review from fahedouch October 2, 2026 01:49
Comment thread docs/command-reference.md Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Medium severity · 2 Low severity

Open (6)
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, foreground run, and start -a through 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.

Comment thread cmd/nerdctl/container/container_run.go Resolved
Comment thread pkg/cmd/container/create.go Resolved
Comment thread pkg/cmd/container/create.go Resolved
Comment thread pkg/containerutil/containerutil.go Resolved
Comment thread docs/command-reference.md Resolved
@ekalinin
ekalinin force-pushed the feat/attach-multiplexer branch from 37dfaa4 to 46304f0 Compare October 3, 2026 18:29
@ekalinin

ekalinin commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Rebased on current main and addressed all seven comments, in four commits:

fix(logging) the opt-out now reaches the logging process
fix(run) the detach announcement no longer blocks
fix(taskutil) run -dt --log-driver none can create its task
docs(attach) multi-session support is not a caveat, and stdin is qualified

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.

@AkihiroSuda

Copy link
Copy Markdown
Member

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]>
@ekalinin
ekalinin force-pushed the feat/attach-multiplexer branch from 46304f0 to 8ac8994 Compare November 26, 2026 13:50
@ekalinin

Copy link
Copy Markdown
Contributor Author

Please tidy up commits (1 topic per 1 commit)

Done. I’ve consolidated the implementation into one commit & rebased it onto main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nerdctl attach freezes the terminal sessions randomly.

3 participants