jlcodes99/cockpit-tools · error

[WS] 保存服务状态失败: {}

Error message

[WS] 保存服务状态失败: {}

What it means

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.

Source

Thrown at crates/cockpit-core/src/modules/websocket.rs:501

                        "[WS] 无法绑定端口 ({}-{}),最后错误: {}",
                        preferred_port,
                        preferred_port + PORT_RANGE - 1,
                        e
                    ));
                    return;
                }
            }
        }
    }

    let listener = match listener {
        Some(l) => l,
        None => return,
    };

    // 保存服务状态到共享文件(供 VS Code 扩展读取)
    if let Err(e) = init_server_status(port) {
        crate::modules::logger::log_error(&format!("[WS] 保存服务状态失败: {}", e));
    }

    crate::modules::logger::log_info(&format!(
        "[WS] WebSocket 服务已启动: ws://127.0.0.1:{}",
        port
    ));

    let server = get_server();

    while let Ok((stream, addr)) = listener.accept().await {
        if !is_allowed_remote_client(&addr) {
            crate::modules::logger::log_warn(&format!("[WS] 鎷掔粷闈炵櫧鍚嶅崟鏉ユ簮: {}", addr));
            continue;
        }
        let server_clone = Arc::clone(server);
        tokio::spawn(handle_connection(server_clone, stream, addr));
    }
}

View on GitHub (pinned to 1ed8b77992)

Solutions

  1. 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.
  2. 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).
  3. If running in a container/sandbox, mount or enable a writable path for the shared status file location.
  4. Free disk space / quota if the error indicates the write failed for capacity reasons, then restart the WebSocket server.

Example fix

// before: status file written into a possibly-missing directory
if let Err(e) = init_server_status(port) {
    log_error(&format!("[WS] 保存服务状态失败: {}", e));
}
// after: ensure the parent directory exists before writing
if let Some(dir) = status_file_path().parent() {
    let _ = std::fs::create_dir_all(dir);
}
if let Err(e) = init_server_status(port) {
    log_error(&format!("[WS] 保存服务状态失败: {}", e));
}
Defensive patterns

Strategy: try-catch

Validate before calling

let dir = std::path::Path::new("/path/to/status/dir");
if !dir.exists() {
    std::fs::create_dir_all(dir).expect("cannot create status dir");
}
let probe = dir.join(".write-test");
std::fs::write(&probe, b"ok").expect("status dir not writable");
let _ = std::fs::remove_file(&probe);

Try / catch

match init_server_status(port) {
    Ok(()) => { /* proceed */ }
    Err(e) => {
        log_error(&format!("[WS] 保存服务状态失败: {}", e));
        // degrade gracefully: server can still run; extension falls back to manual port entry
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of jlcodes99/cockpit-tools@1ed8b77992 (2026-09-05). Data as JSON: /api/errors/9d7699e2a404bae2. Report an issue: GitHub.