{"record":{"id":"426f9cad1a83a334","repo":"wezterm/wezterm","slug":"can-t-get-the-size-of-shared-memory-virtualquery","errorCode":null,"errorMessage":"Can't get the size of Shared Memory, VirtualQuery failed: {:#}","messagePattern":"Can't get the size of Shared Memory, VirtualQuery failed: (.+?)","errorType":"exception","errorClass":"std::io::Error","httpStatus":null,"severity":"warning","filePath":"wezterm-escape-parser/src/apc.rs","lineNumber":403,"sourceCode":"            ));\n        }\n\n        let shm = SharedMemObject {\n            _handle: handle_wrapper,\n            buf,\n        };\n\n        let mut memory_info = MEMORY_BASIC_INFORMATION::default();\n        let res = unsafe {\n            VirtualQuery(\n                shm.buf.Value,\n                &mut memory_info,\n                std::mem::size_of::<MEMORY_BASIC_INFORMATION>(),\n            )\n        };\n        if res == 0 {\n            let err = std::io::Error::last_os_error();\n            return Err(std::io::Error::new(\n                std::io::ErrorKind::Other,\n                format!(\n                    \"Can't get the size of Shared Memory, VirtualQuery failed: {:#}\",\n                    err\n                ),\n            ));\n        }\n        let mut size = memory_info.RegionSize;\n        let offset = data_offset.unwrap_or(0) as usize;\n        if offset >= size {\n            return Err(std::io::Error::new(\n                std::io::ErrorKind::Other,\n                format!(\n                    \"offset {} bigger than or equal to shm region size {}\",\n                    offset, size\n                ),\n            ));\n        }","sourceCodeStart":385,"sourceCodeEnd":421,"githubUrl":"https://github.com/wezterm/wezterm/blob/b99b1ca2cc0a77e97600410726cdf5e3750bd89b/wezterm-escape-parser/src/apc.rs#L385-L421","documentation":"Once the view is mapped, the Windows shm reader calls VirtualQuery on the base pointer to learn the region size (needed to bound the copy). A zero return means the query could not describe the region, reported as ErrorKind::Other 'Can't get the size of Shared Memory, VirtualQuery failed: {os error}'. This is a near-impossible path for a valid open mapping+view; it indicates the view became invalid between mapping and querying.","triggerScenarios":"Another thread unmapped or closed the view/handle concurrently, or the handle was invalid in a way MapViewOfFile masked; practically only under concurrent teardown of the transfer.","commonSituations":"Races where the producer closes the mapping while the terminal is mid-read; memory corruption in either process; antivirus/security software interfering with mapped sections.","solutions":["Serialize the transfer: producer keeps the mapping alive until the reader signals completion, so the view stays valid through VirtualQuery.","Treat the error as transient and retry the whole transfer once via a different transport (inline base64 or file).","Check for concurrent unmapping/closing code paths in the embedding application."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"match read_shared_memory_data_win(&name, off, size) {\n    Ok(data) => Ok(data),\n    Err(e) if e.to_string().contains(\"VirtualQuery\") => {\n        // transient teardown race: one retry, then fall back to another transport\n        Err(e)\n    }\n    Err(e) => Err(e),\n}","preventionTips":["Never close/unmap the section while the reader is mid-transfer.","Use an explicit ack from reader to writer before teardown.","Log mapping names with timings to correlate races in bug reports."],"tags":["windows","memory-map","shared-memory","race-condition"],"backgroundTag":"memory-map-failed","analyzedSha":"b99b1ca2cc0a77e97600410726cdf5e3750bd89b","analyzedAt":"2026-08-20T04:47:31.434Z","contentChangedAt":"2026-08-20T04:47:31.434Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}