Skip to content

Stdin handle required to avoid conpty race condition upon WCOW container startup with terminal #2831

Closed

Description

@mcayland-ntx

Setup

OS: Windows 11 with latest updates, and Windows containers installed with containerd as per https://learn.microsoft.com/en-us/virtualization/windowscontainers/quick-start/set-up-environment?tabs=containerd but with the following version updates:

  • containerd version v2.2.0
  • nerdctl version v2.3.4 (from git)
  • hcsshim version spec: v1.3.0 (from git)

Description

I've been investigating why running a WCOW container using nerdctl run -d -t mcr.microsoft.com/windows/server:ltsc2025 terminates almost immediately with exit code 3221225786 (0xc000013a) STATUS_CONTROL_C_EXIT as reported at containerd/nerdctl#4899, and the best conclusion I have reached so far is that the underlying HcsProcess requires a valid non-NULL Stdin handle to avoid this immediate termination.

Analysis

Looking at the original issue reported with nerdctl above, with the latest git versions of nerdctl and hcsshim I see the following behaviour:

C:\Users\mark>nerdctl run -d -t mcr.microsoft.com/windows/nanoserver:ltsc2025
369992f8d96f29ab0412dc800b3eff24c8a3a29596bbf5ba3629450f2dd709fc

C:\Users\mark>nerdctl ps -a
CONTAINER ID    IMAGE                                            COMMAND                     CREATED          STATUS                               PORTS    NAMES
369992f8d96f    mcr.microsoft.com/windows/nanoserver:ltsc2025    "c:\\windows\\system32…"    5 seconds ago    Exited (3221225786) 2 seconds ago             nanoserver-36999

For comparison we can see that starting the same container in interactive mode works fine:

C:\Users\mark>nerdctl run --rm -it mcr.microsoft.com/windows/nanoserver:ltsc2025

Microsoft Windows [Version 10.0.26100.32860]
(c) Microsoft Corporation. All rights reserved.

C:\Windows\System32>echo "Hello world"
"Hello world"

C:\Windows\System32>exit
time="2026-07-20T09:52:06+01:00" level=warning msg="failed to remove hosts file for container \"163f0d294c723af096f2965253e16e226a7be00a3a4d8d1cdc93bc152776f251\"" error="hosts-store error\nnot found\nGetFileAttributesEx C:\\ProgramData\\nerdctl\\052055e3\\etchosts\\default\\163f0d294c723af096f2965253e16e226a7be00a3a4d8d1cdc93bc152776f251: The system cannot find the file specified."

C:\Users\mark>

I added various debug statements around nerdctl/hcsshim/containerd and the only obvious difference I could see was that in the detached terminal case the HcsProcess Stdin was set to NULL compared with the interactive terminal.

To test this theory I made the following simple change to hcsshim to see what would happen to the working interactive terminal if I hardcoded the HcsProcess Stdin to NULL:

diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go
index 82f859d5..8b8aa20c 100644
--- a/internal/cmd/cmd.go
+++ b/internal/cmd/cmd.go
@@ -180,7 +180,7 @@ func (c *Cmd) Start() error {
                        User:             c.Spec.User.Username,
                        WorkingDirectory: c.Spec.Cwd,
                        EmulateConsole:   c.Spec.Terminal,
-                       CreateStdInPipe:  c.Stdin != nil,
+                       CreateStdInPipe:  false,
                        CreateStdOutPipe: c.Stdout != nil,
                        CreateStdErrPipe: c.Stderr != nil,
                }

With this change we can see that the container that was previously working in interactive mode now fails in exactly the same, even to the point where the Ctrl-C character is visible in the terminal itself:

C:\Users\mark>nerdctl run -it mcr.microsoft.com/windows/nanoserver:ltsc2025
^C
C:\Users\mark>nerdctl ps -a
CONTAINER ID    IMAGE                                            COMMAND                     CREATED               STATUS                                    PORTS    NAMES
a141fb818e6e    mcr.microsoft.com/windows/nanoserver:ltsc2025    "c:\\windows\\system32…"    About a minute ago    Exited (3221225786) About a minute ago             nanoserver-a141f

So why is it necessary to provide a non-NULL handle to the HcsProcess Stdin only in the detached terminal case? The answer to this is because when nerdctl creates a detached terminal, it uses a new upstream BinaryIO which only creates pipes for Stdout/Stderr and not Stdin as per https://github.com/microsoft/hcsshim/blob/main/internal/cmd/io_binary.go#L33.

At this point my thoughts were that any HcsProcess must have a non-zero Stdin handle, however with the above patch applied to hcsshim I see something interesting: if I run a command such as nerdctl run --rm -it mcr.microsoft.com/windows/nanoserver:ltsc2025 cmd.exe /? enough times in a loop from cmd.exe then eventually it will succeed after perhaps 20-30 tries.

This suggests that it may not necessarily be the NULL Stdin handle that is directly responsible for the failure, but somehow hcsshim accidentally depends upon a functional Stdin in order to detect a successful WCOW container startup and process HcsProcess launch.

Summary

There is a currently an issue when launching detached containers from nerdctl using nerdctl run -d -t mcr.microsoft.com/windows/nanoserver:ltsc2025 causing the process to fail immediately with exit code 3221225786 (0xc000013a) STATUS_CONTROL_C_EXIT. This is linked to the presence of a valid non-NULL Stdin handle for the container HcsProcess which is likely to have one of two causes:

  • Console applications launched as a HcsProcess MUST have a valid non-NULL Stdin handle

This would require someone with Windows internal knowledge to confirm the expectations and behaviour of console applications such as cmd.exe when presented with a NULL Stdin handle. If all console applications MUST have a valid non-NULL Stdin handle then hcsshim should always create the Stdin pipe for a HcsProcess (even if it is never connected) which will resolve the issue.

  • Hcsshim inadvertently depends upon a valid Stdin handle to detect a successful HcsProcess launch

There may be logic in hcsshim that assumes a valid Stdin handle is always present to detect a successful HcsProcess launch: if a valid Stdin handle is not present then a racy logic condition in hcsshim may attempt to detect this and cause the HcsProcess to terminate immediately. For a detached container launched with nerdctl with BinaryIO, a Stdin pipe is never created for the HcsProcess causing the failure.

Activity

  1. mcayland-ntx commented on Jul 20, 2026

    @mcayland-ntx
    ContributorAuthor
    • Console applications launched as a HcsProcess MUST have a valid non-NULL Stdin handle

    This would require someone with Windows internal knowledge to confirm the expectations and behaviour of console applications such as cmd.exe when presented with a NULL Stdin handle. If all console applications MUST have a valid non-NULL Stdin handle then hcsshim should always create the Stdin pipe for a HcsProcess (even if it is never connected) which will resolve the issue.

    I can confirm that the diff below fixes nerdctl run -d -t mcr.microsoft.com/windows/nanoserver:ltsc2025 although it feels a little indiscriminate, and doesn't answer the question as to why occasionally the process will start successfully approximately every 1 in every 20-30 tries:

    diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go
    index 82f859d5..5e2b2c74 100644
    --- a/internal/cmd/cmd.go
    +++ b/internal/cmd/cmd.go
    @@ -180,7 +180,7 @@ func (c *Cmd) Start() error {
                            User:             c.Spec.User.Username,
                            WorkingDirectory: c.Spec.Cwd,
                            EmulateConsole:   c.Spec.Terminal,
    -                       CreateStdInPipe:  c.Stdin != nil,
    +                       CreateStdInPipe:  true,
                            CreateStdOutPipe: c.Stdout != nil,
                            CreateStdErrPipe: c.Stderr != nil,
                    }
    
  2. mcayland-ntx commented on Jul 20, 2026

    @mcayland-ntx
    ContributorAuthor

    containerd/hcsshim logs for when the detached terminal container fails to start: containerd-debug-bad.txt

    containerd/hcsshim logs for when the detached terminal container starts approximately every 1 in 20-30 tries: containerd-debug-good.txt

  3. mcayland-ntx commented on Jul 27, 2026

    @mcayland-ntx
    ContributorAuthor

    (Renamed issue from "Non-NULL Stdin handle appears to be required for HcsProcess to launch console process in WCOW container" to "Stdin handle required to avoid conpty race condition upon WCOW container startup with terminal")

    After some further debugging and analysis, I'm quite confident that the problem I am seeing with containers launched with nerdctl run -d -t mcr.microsoft.com/windows/server:ltsc2025 is caused by a race condition in conpty when no stdin handle is present. When nerdctl launches a container with a detached terminal, it uses the BinaryIO facility to launch a logging binary to capture the terminal output in which only stdout and stderr handles are created for the process: local debugging shows that stdin is set to -1 (or INVALID_HANDLE_VALUE) upon return from HcsWaitForOperationResultAndProcessInfo.

    I compared the logged terminal output of nerdctl run -d -t mcr.microsoft.com/windows/server:ltsc2025 netsh help for both the good and bad and noticed a difference in the terminal sequences:

    GOOD:

    {"log":"\u001b[?9001h\u001b[?1004h\u001b[?25l\u001b[2J\u001b[m\u001b[H\u001b]0;C:\\Windows\\system32\\netsh.exe\u0007\u001b[?25h\r\n","stream":"stdout","time":"2026-07-24T10:34:20.1281687Z"}
    {"log":"The following commands are available:\r\n","stream":"stdout","time":"2026-07-24T10:34:20.1281687Z"}
    {"log":"\u001b[?25l\r\n","stream":"stdout","time":"2026-07-24T10:34:20.1379867Z"}
    {"log":"Commands in this context:\r\n","stream":"stdout","time":"2026-07-24T10:34:20.1379867Z"}
    {"log":"?              - Displays a list of commands.\r\n","stream":"stdout","time":"2026-07-24T10:34:20.1379867Z"}
    {"log":"add            - Adds a configuration entry to a list of entries.\r\n","stream":"stdout","time":"2026-07-24T10:34:20.1386669Z"}
    {"log":"delete         - Deletes a configuration entry from a list of entries.\r\n","stream":"stdout","time":"2026-07-24T10:34:20.1386669Z"}
    {"log":"dnsclient      - Changes to the `netsh dnsclient' context.\r\n","stream":"stdout","time":"2026-07-24T10:34:20.1386669Z"}
    {"log":"dump           - Displays a configuration script.\r\n","stream":"stdout","time":"2026-07-24T10:34:20.1386669Z"}
    {"log":"exec           - Runs a script file.\r\n","stream":"stdout","time":"2026-07-24T10:34:20.1386669Z"}
    {"log":"help           - Displays a list of commands.\r\n","stream":"stdout","time":"2026-07-24T10:34:20.1386669Z"}
    ...
    ...
    

    BAD:

    {"log":"\u001b[?9001h\u001b[?1004h\u001b[?25l\u001b[2J\u001b[m\u001b[H\u001b]0;C:\\Windows\\system32\\netsh.exe\u0007\u001b[?25h\u001b[?9001l\u001b[?1004l","stream":"stdout","time":"2026-07-24T10:38:45.5984115Z"}
    

    Here we see that not only is the help output not present, but in the bad case there are two additional escape sequences generated in the log file which are \u001b[?9001l\u001b[?1004l. With some trivial searching I was able to determine that this is the reset sequence generated by conpty when dropping the conpty console writer before the child exits. The same page also documents that in the case where this happens then the container terminates with a status code of STATUS_CONTROL_C_EXIT (0xc000013a) which exactly matches the issue I am seeing.

    I also found a separate reference to similar behaviour in the MiniPty documentation at https://github.com/guitarrapc/MiniPty/blob/main/.github/docs/specs/lifecycle.md#lessons-learned which documents that stdin must not be closed before process launch completes on Windows otherwise the process terminates with STATUS_CONTROL_C_EXIT (0xc000013a).

    In conclusion it appears that this is not directly an issue with either containerd or hcsshim, but in fact is due to a race condition when conpty is attached to a process with no stdin handle. Given that this behaviour is unlikely to change in the short term, the best solution would be to add a workaround in hcsshim similar to below:

    diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go
    index 82f859d5..2491c045 100644
    --- a/internal/cmd/cmd.go
    +++ b/internal/cmd/cmd.go
    @@ -180,7 +180,7 @@ func (c *Cmd) Start() error {
                            User:             c.Spec.User.Username,
                            WorkingDirectory: c.Spec.Cwd,
                            EmulateConsole:   c.Spec.Terminal,
    -                       CreateStdInPipe:  c.Stdin != nil,
    +                       CreateStdInPipe:  c.Stdin != nil || c.Spec.Terminal,
                            CreateStdOutPipe: c.Stdout != nil,
                            CreateStdErrPipe: c.Stderr != nil,
                    }
    

    This ensures that if terminal emulation is requested for a container, a stdin pipe is always created to provide a valid handle to the WCOW container process, avoiding the startup race continue encountered with conpty.

    I am currently in the process of testing the above patch which seems to resolve the issue in our real environment, and intend to raise a PR once everything is confirmed to be working as expected.

  4. changed the title [-]Non-NULL Stdin handle appears to be required for HcsProcess to launch console process in WCOW container[/-] [+]Stdin handle required to avoid conpty race condition upon WCOW container startup with terminal[/+] on Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions