{"record":{"id":"9d7699e2a404bae2","repo":"jlcodes99/cockpit-tools","slug":"ws-9d7699","errorCode":null,"errorMessage":"[WS] 保存服务状态失败: {}","messagePattern":"\\[WS\\] 保存服务状态失败: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/cockpit-core/src/modules/websocket.rs","lineNumber":501,"sourceCode":"                        \"[WS] 无法绑定端口 ({}-{})，最后错误: {}\",\n                        preferred_port,\n                        preferred_port + PORT_RANGE - 1,\n                        e\n                    ));\n                    return;\n                }\n            }\n        }\n    }\n\n    let listener = match listener {\n        Some(l) => l,\n        None => return,\n    };\n\n    // 保存服务状态到共享文件（供 VS Code 扩展读取）\n    if let Err(e) = init_server_status(port) {\n        crate::modules::logger::log_error(&format!(\"[WS] 保存服务状态失败: {}\", e));\n    }\n\n    crate::modules::logger::log_info(&format!(\n        \"[WS] WebSocket 服务已启动: ws://127.0.0.1:{}\",\n        port\n    ));\n\n    let server = get_server();\n\n    while let Ok((stream, addr)) = listener.accept().await {\n        if !is_allowed_remote_client(&addr) {\n            crate::modules::logger::log_warn(&format!(\"[WS] 鎷掔粷闈炵櫧鍚嶅崟鏉ユ簮: {}\", addr));\n            continue;\n        }\n        let server_clone = Arc::clone(server);\n        tokio::spawn(handle_connection(server_clone, stream, addr));\n    }\n}","sourceCodeStart":483,"sourceCodeEnd":519,"githubUrl":"https://github.com/jlcodes99/cockpit-tools/blob/1ed8b77992d62ca81fabf744deb0839ad361d5bf/crates/cockpit-core/src/modules/websocket.rs#L483-L519","documentation":"This is a log line emitted when init_server_status(port) fails while the WebSocket server starts up. init_server_status persists the port/status into a shared status file that the VS Code extension reads to discover the running service. The error value comes from the file I/O layer (create/write of the status file), and the module deliberately logs and continues instead of aborting the server.","triggerScenarios":"Calling start of the WebSocket server when the status file cannot be created or written: init_server_status(port) returns Err because the target directory is missing, the file is not writable, the path is misconfigured, or the disk is full.","commonSituations":"Running cockpit under a user without write permission to the shared config/data directory; read-only container filesystem or sandbox; the directory the status file lives in was removed or moved; antivirus/backup tools locking the file; disk quota exceeded.","solutions":["Check the error text after '[WS] 保存服务状态失败:' — it names the underlying IO error (e.g. Permission denied / No such file or directory) and fix that specific cause.","Verify the directory containing the status file exists and is writable by the process user (mkdir -p / chown, or run as a user with access).","If running in a container/sandbox, mount or enable a writable path for the shared status file location.","Free disk space / quota if the error indicates the write failed for capacity reasons, then restart the WebSocket server."],"exampleFix":"// before: status file written into a possibly-missing directory\nif let Err(e) = init_server_status(port) {\n    log_error(&format!(\"[WS] 保存服务状态失败: {}\", e));\n}\n// after: ensure the parent directory exists before writing\nif let Some(dir) = status_file_path().parent() {\n    let _ = std::fs::create_dir_all(dir);\n}\nif let Err(e) = init_server_status(port) {\n    log_error(&format!(\"[WS] 保存服务状态失败: {}\", e));\n}","handlingStrategy":"try-catch","validationCode":"let dir = std::path::Path::new(\"/path/to/status/dir\");\nif !dir.exists() {\n    std::fs::create_dir_all(dir).expect(\"cannot create status dir\");\n}\nlet probe = dir.join(\".write-test\");\nstd::fs::write(&probe, b\"ok\").expect(\"status dir not writable\");\nlet _ = std::fs::remove_file(&probe);","typeGuard":null,"tryCatchPattern":"match init_server_status(port) {\n    Ok(()) => { /* proceed */ }\n    Err(e) => {\n        log_error(&format!(\"[WS] 保存服务状态失败: {}\", e));\n        // degrade gracefully: server can still run; extension falls back to manual port entry\n    }\n}","preventionTips":["Create the status-file parent directory at install/startup time with create_dir_all.","Run the service under a user that owns the shared data directory.","In containers, mount a writable volume at the status-file location.","Monitor disk space/quota on the host running cockpit."],"tags":["websocket","file-io","rust"],"backgroundTag":"status-file-write-failed","analyzedSha":"1ed8b77992d62ca81fabf744deb0839ad361d5bf","analyzedAt":"2026-09-05T09:51:41.178Z","contentChangedAt":"2026-09-05T09:51:41.178Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}