microsoft/aspire · error · Win32Exception

Failed to open NUL device for stdin

Error message

Failed to open NUL device for stdin

What it means

When spawning a non-detached child on Windows, IsolatedProcess opens the NUL device (CreateFileW "NUL") to use as the child's stdin handle, since no input is provided. If CreateFileW returns an invalid handle, a Win32Exception carrying the raw Win32 error is thrown. Without a valid stdin handle the child's stdio wiring (STARTF_USESTDHANDLES) would be incomplete.

Solutions

  1. Check the inner Win32Exception NativeErrorCode: ERROR_FILE_NOT_FOUND/ERROR_PATH_NOT_FOUND usually means the NUL device driver is broken — verify with `echo test > NUL` in cmd.
  2. Restart the machine or repair Windows to restore the NUL device object if `> NUL` fails.
  3. Check antivirus / endpoint-protection or AppContainer policies that may block opening device objects, and add an exclusion for the CLI process.
  4. Try reproducing in a plain console (outside containers/CI sandboxes) to isolate sandbox interference.
  5. Update the Aspire CLI; if the environment is confirmed healthy but the error persists, file an issue.
Defensive patterns

Strategy: try-catch

Try / catch

try
{
    var process = isolatedProcess.Start(startInfo);
}
catch (Win32Exception ex) when (ex.Message == "Failed to open NUL device for stdin")
{
    // Surface ex.NativeErrorCode; treat as machine-level NUL device or policy problem
}

Prevention

When it happens

Trigger: StartWindows calls CreateFileW("NUL", GenericRead, FileShareRead, ..., OpenExisting, 0, null) and the returned SafeFileHandle.IsInvalid is true — the Win32 error code comes from Marshal.GetLastWin32Error() immediately after.

Common situations: Broken or removed NUL device driver on the machine, heavily restricted security software / device-object ACLs blocking \Device\Null, running in an unusual container or Job sandbox where the NUL device is unavailable, or a corrupted Windows installation.

Understand the failure class

Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/b1506cc49f9e4daa. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Cli/Processes/IsolatedProcess.Windows.cs:51

    private static StartedProcess StartWindows(IsolatedProcessStartInfo startInfo)
    {
        if (startInfo.Detached)
        {
            return StartWindowsSuppressed(startInfo);
        }

        var nulStdinHandle = WindowsProcessInterop.CreateFileW(
            "NUL",
            WindowsProcessInterop.GenericRead,
            WindowsProcessInterop.FileShareRead,
            nint.Zero,
            WindowsProcessInterop.OpenExisting,
            0,
            nint.Zero);

        if (nulStdinHandle.IsInvalid)
        {
            throw new Win32Exception(Marshal.GetLastWin32Error(), "Failed to open NUL device for stdin");
        }

        AnonymousPipeServerStream? stdoutPipe = null;
        AnonymousPipeServerStream? stderrPipe = null;

        try
        {
            if (!WindowsProcessInterop.SetHandleInformation(nulStdinHandle, WindowsProcessInterop.HandleFlagInherit, WindowsProcessInterop.HandleFlagInherit))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "Failed to set NUL stdin handle inheritance");
            }

            // PipeDirection.In = server reads, client writes. Inheritable is REQUIRED:
            // PROC_THREAD_ATTRIBUTE_HANDLE_LIST restricts WHICH handles get inherited but does
            // NOT promote non-inheritable handles to inheritable ones. Without this flag the
            // child would see ERROR_INVALID_HANDLE on its stdout/stderr writes.
            stdoutPipe = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable);
            stderrPipe = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable);

View on GitHub (pinned to 25830f84bd)