denoland/deno · error

SetStdHandle failed (error {})

Error message

SetStdHandle failed (error {})

What it means

The final step of Deno's Windows stdio repair: after successfully opening \\?\NUL, it calls SetStdHandle to install that handle as the missing standard stream. If the assignment fails, the process would run with inconsistent stdio, so Deno panics with the last Win32 error. Like the other stdio-repair panics, it reflects a broken process environment rather than anything in user code.

Source

Thrown at cli/util/windows.rs:255

          lpSecurityDescriptor: std::ptr::null_mut(),
          bInheritHandle: TRUE,
        };
        let file_handle = CreateFileA(
          c"\\\\?\\NUL".as_ptr() as *const u8,
          desired_access,
          FILE_SHARE_READ | FILE_SHARE_WRITE,
          &security_attributes,
          OPEN_EXISTING,
          FILE_ATTRIBUTE_NORMAL,
          std::ptr::null_mut(),
        );
        if file_handle == INVALID_HANDLE_VALUE {
          panic!("Could not open NUL device (error {})", GetLastError());
        }

        // Assign the opened NUL handle to the missing stdio handle.
        if SetStdHandle(std_handle, file_handle) == FALSE {
          panic!("SetStdHandle failed (error {})", GetLastError());
        }
      }
    }
  }
}

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Redirect stdio explicitly in the launching command so the repair path is never attempted
  2. Reboot to reset kernel handle state
  3. Test outside the sandbox/EDR layer; add exclusions if that resolves it
  4. Report with the error code if reproducible on the latest Deno

Example fix

:: before — rely on deno repairing a closed stdio slot
deno run app.ts

:: after — explicit redirection avoids the repair entirely
deno run app.ts < NUL > out.log 2>&1
Defensive patterns

Strategy: fallback

Prevention

When it happens

Trigger: Same precondition as the NUL-open failure (a closed stdio slot) plus a SetStdHandle failure — handle table corruption, security software intercepting handle mutation, or unusual container/sandbox layers restricting handle operations on Windows.

Common situations: Deno spawned inside sandboxed CI runners or job objects that restrict handle mutation; rare enough that a reboot or a different launch method (explicit redirection) usually clears it.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/d2dcf7df207f250f. Report an issue: GitHub.