denoland/deno · error

Failed to start winsocket

Error message

Failed to start winsocket

What it means

On Windows, os.hostname() resolves through GetHostNameW, which requires Winsock to be initialized; Deno calls WSAStartup(2.2) once per process and panics if it returns a nonzero error. A failing WSAStartup means the Winsock subsystem itself is unusable — catalog corruption or critical resource exhaustion — not a network outage or DNS problem.

Source

Thrown at ext/os/sys_info.rs:175

  {
    use std::ffi::OsString;
    use std::mem;
    use std::os::windows::ffi::OsStringExt;

    use windows_sys::Win32::Networking::WinSock::GetHostNameW;
    use windows_sys::Win32::Networking::WinSock::WSAStartup;

    let namelen = 256;
    let mut name: Vec<u16> = vec![0u16; namelen];
    // Start winsock to make `GetHostNameW` work correctly
    // https://github.com/retep998/winapi-rs/issues/296
    // SAFETY: Win32 call
    WINSOCKET_INIT.call_once(|| unsafe {
      let mut data = mem::zeroed();
      // MAKEWORD(2, 2)
      let wsa_startup_result = WSAStartup(0x0202, &mut data);
      if wsa_startup_result != 0 {
        panic!("Failed to start winsocket");
      }
    });
    let err =
      // SAFETY: length of wide string is 256 chars or less.
      // https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-gethostnamew
      unsafe { GetHostNameW(name.as_mut_ptr(), namelen as libc::c_int) };

    if err == 0 {
      // TODO(@littledivy): Probably not the most efficient way.
      let len = name.iter().take_while(|&&c| c != 0).count();
      OsString::from_wide(&name[..len])
        .to_string_lossy()
        .into_owned()
    } else {
      String::from("")
    }
  }
}

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Run `netsh winsock reset` as administrator and reboot — the canonical fix for catalog corruption
  2. Verify in a plain console first; if only a sandboxed environment fails, adjust that environment
  3. As a stopgap, read the hostname from the COMPUTERNAME environment variable instead of os.hostname()
  4. Update Deno and report if WSAStartup still fails on a healthy machine

Example fix

// before — panics when winsock is broken
const host = os.hostname();

// after — environment first, winsock-backed call only as fallback
const host = Deno.env.get("COMPUTERNAME") ?? os.hostname();
Defensive patterns

Strategy: fallback

Validate before calling

function hostnameSafe(): string {
  const env = Deno.env.get("COMPUTERNAME") ?? Deno.env.get("HOSTNAME");
  if (env) return env;
  return os.hostname(); // last resort: touches winsock
}

Prevention

When it happens

Trigger: Calling os.hostname() (node:os) on a Windows host where WSAStartup fails: winsock catalog corruption after installing VPN/LSP software, severely resource-exhausted machines, or locked-down minimal Windows environments.

Common situations: After installing/uninstalling VPNs, proxies, or firewall software that corrupts the winsock catalog; minimal Windows containers; endpoints with aggressive security tooling that blocks Winsock initialization.

Related errors


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