{"record":{"id":"e5272d824cff0627","repo":"microsoft/aspire","slug":"failed-to-set-nul-stdin-handle-inheritance","errorCode":null,"errorMessage":"Failed to set NUL stdin handle inheritance","messagePattern":"Failed to set NUL stdin handle inheritance","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"src/Aspire.Cli/Processes/IsolatedProcess.Windows.cs","lineNumber":61,"sourceCode":"            WindowsProcessInterop.FileShareRead,\n            nint.Zero,\n            WindowsProcessInterop.OpenExisting,\n            0,\n            nint.Zero);\n\n        if (nulStdinHandle.IsInvalid)\n        {\n            throw new Win32Exception(Marshal.GetLastWin32Error(), \"Failed to open NUL device for stdin\");\n        }\n\n        AnonymousPipeServerStream? stdoutPipe = null;\n        AnonymousPipeServerStream? stderrPipe = null;\n\n        try\n        {\n            if (!WindowsProcessInterop.SetHandleInformation(nulStdinHandle, WindowsProcessInterop.HandleFlagInherit, WindowsProcessInterop.HandleFlagInherit))\n            {\n                throw new Win32Exception(Marshal.GetLastWin32Error(), \"Failed to set NUL stdin handle inheritance\");\n            }\n\n            // PipeDirection.In = server reads, client writes. Inheritable is REQUIRED:\n            // PROC_THREAD_ATTRIBUTE_HANDLE_LIST restricts WHICH handles get inherited but does\n            // NOT promote non-inheritable handles to inheritable ones. Without this flag the\n            // child would see ERROR_INVALID_HANDLE on its stdout/stderr writes.\n            stdoutPipe = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable);\n            stderrPipe = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable);\n\n            var stdio = new WindowsProcessInterop.StdioHandles(\n                Stdin: nulStdinHandle.DangerousGetHandle(),\n                Stdout: stdoutPipe.ClientSafePipeHandle.DangerousGetHandle(),\n                Stderr: stderrPipe.ClientSafePipeHandle.DangerousGetHandle());\n\n            // Pass the caller's environment (if touched) verbatim to the spawn primitive;\n            // null = inherit parent env block.\n            var environment = startInfo.GetEnvironmentForSpawn();\n","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Cli/Processes/IsolatedProcess.Windows.cs#L43-L79","documentation":"After opening NUL for the child's stdin, the CLI must mark that handle inheritable (SetHandleInformation with HANDLE_FLAG_INHERIT) so the spawned child can actually use it. When SetHandleInformation returns FALSE, a Win32Exception with the underlying Win32 error code is thrown. This happens before the child is created, so no orphan process is left behind.","triggerScenarios":"StartWindows calls WindowsProcessInterop.SetHandleInformation(nulStdinHandle, HandleFlagInherit, HandleFlagInherit) and it returns false — Marshal.GetLastWin32Error() typically yields ERROR_INVALID_HANDLE (6) or ERROR_ACCESS_DENIED (5).","commonSituations":"The NUL handle was already disposed/closed due to a race or earlier failure, security software stripping handle flags, or a corrupted handle value from a failed CreateFileW path that slipped past the IsInvalid check.","solutions":["Read NativeErrorCode: ERROR_INVALID_HANDLE points at handle creation failing earlier — fix the upstream 'Failed to open NUL device for stdin' condition first.","Check whether security software or a restricted token/job is interfering with handle manipulation and run with a normal user token.","Reproduce with a minimal `cmd /c dir > NUL` style test to confirm handle inheritance works in the environment.","Update or reinstall the Aspire CLI; report persistent occurrences with the Win32 error code."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    var process = isolatedProcess.Start(startInfo);\n}\ncatch (Win32Exception ex) when (ex.Message == \"Failed to set NUL stdin handle inheritance\")\n{\n    // Check ex.NativeErrorCode: 6 => upstream open failed; 5 => security policy\n}","preventionTips":["Resolve any 'Failed to open NUL device for stdin' errors first — this error is usually downstream of a bad handle.","Run the CLI under a normal (non-restricted) user token.","Audit EDR/AV policies that hook SetHandleInformation.","Keep the Aspire CLI updated so handle-lifecycle bugs are patched."],"tags":["windows","process","win32","handle-inheritance"],"backgroundTag":"permission-denied","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}