{"record":{"id":"6ba56d3743d9b164","repo":"zed-industries/zed","slug":"failed-to-set-socket-receive-timeout-setsockopt-r","errorCode":null,"errorMessage":"Failed to set socket receive timeout: setsockopt returned {ret}","messagePattern":"Failed to set socket receive timeout: setsockopt returned (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/etw_tracing/etw_tracing.rs","lineNumber":554,"sourceCode":"}\n\nfn receive_command(stream: &mut net::UnixStream) -> Result<(Command, bool)> {\n    use std::os::windows::io::{AsRawSocket, AsSocket};\n    use windows::Win32::Networking::WinSock::{SO_RCVTIMEO, SOL_SOCKET, setsockopt};\n\n    // Set a receive timeout so read_line returns an error after `timeout`.\n    let millis = RECORDING_TIMEOUT.as_millis() as u32;\n    let socket = stream.as_socket();\n    let ret = unsafe {\n        setsockopt(\n            windows::Win32::Networking::WinSock::SOCKET(socket.as_raw_socket() as _),\n            SOL_SOCKET,\n            SO_RCVTIMEO,\n            Some(&millis.to_ne_bytes()),\n        )\n    };\n    if ret != 0 {\n        bail!(\"Failed to set socket receive timeout: setsockopt returned {ret}\");\n    }\n\n    let mut reader = BufReader::new(&mut *stream);\n    match recv_json::<Command>(&mut reader) {\n        Ok(command) => Ok((command, false)),\n        Err(error) => {\n            log::warn!(\"Failed to receive ETW command, treating as timed-out Save: {error:#}\");\n            Ok((Command::Save, true))\n        }\n    }\n}\n\npub struct EtwSession {\n    output_path: PathBuf,\n    stream: BufReader<net::UnixStream>,\n    listener: net::UnixListener,\n    socket_path: PathBuf,\n}","sourceCodeStart":536,"sourceCodeEnd":572,"githubUrl":"https://github.com/zed-industries/zed/blob/f4178619acd0d47ea1f76a2025c42962c6d6638c/crates/etw_tracing/etw_tracing.rs#L536-L572","documentation":"Bailed when the Windows setsockopt call setting SO_RCVTIMEO on the accepted ETW subprocess socket returns non-zero (SOCKET_ERROR). The receive timeout is what makes read_line give up after RECORDING_TIMEOUT; when the option cannot be applied, session setup aborts rather than risk a permanently blocking read.","triggerScenarios":"The accepted socket is already closed or reset by a subprocess that died immediately after connecting, the socket handle is invalid, or a Winsock/provider-level condition rejects SO_RCVTIMEO — setsockopt returns non-zero and this bail fires.","commonSituations":"ETW helper crashing on startup so the peer RSTs before options are set; antivirus/firewall interference on the loopback socket; unusual Windows socket provider configurations.","solutions":["Call WSAGetLastError right after the failing setsockopt to identify the concrete reason code","Verify the elevated ETW subprocess stays alive after connecting (check its logs) — an immediate exit resets the socket","Retry the accept + setsockopt sequence once to ride out transient resets","As a diagnostic, run the elevated helper manually to see whether it fails at startup"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"Treat setsockopt failure as per-connection: log the WSAGetLastError code captured right after the call, drop that connection, and wait for the next accept instead of tearing down the ETW session.","preventionTips":["Apply socket options immediately after accept, before any reads","Verify the subprocess is alive when it connects (an immediate exit resets the socket)","Capture WSAGetLastError alongside the return code for diagnosable logs"],"tags":["windows","winsock","socket","etw","unsafe","rust"],"backgroundTag":"socket-option-failed","analyzedSha":"f4178619acd0d47ea1f76a2025c42962c6d6638c","analyzedAt":"2026-08-20T19:29:52.058Z","contentChangedAt":"2026-08-20T19:29:52.058Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}