Overview
Concrete tool for resolving, detecting, and connecting to the jcode daemon's Unix socket, provided by the jcode server module.
Description
The socket module provides the socket path resolution and connection primitives for the single-server, multi-client architecture. The socket_path() function resolves from JCODE_SOCKET env var or defaults to runtime_dir/jcode.sock. is_shared_socket() compares resolved socket identities (canonicalizing parent directories) to determine if a path is the shared daemon socket. connect_socket() connects with careful error handling (never unlinking a refused socket, detecting EMFILE). reap_stale_socket_if_dead() safely removes a socket left behind by a dead daemon by checking both listener liveness and daemon lock acquisition.
Usage
- Client startup: Call
socket_path() to get the socket, then socket_has_live_listener() to detect an existing daemon.
- Custom socket: Set
JCODE_SOCKET or use --socket for an isolated server.
- Stale cleanup: Call
reap_stale_socket_if_dead() on startup to clean up crashed daemon sockets.
Code Reference
Source Location
- Repository: jcode
- File: crates/jcode-app-core/src/server/socket.rs
Signature
pub fn socket_path() -> PathBuf
pub fn default_socket_path() -> PathBuf
pub fn is_shared_socket(path: &std::path::Path) -> bool
pub fn socket_path_is_custom() -> bool
pub fn debug_socket_path() -> PathBuf
pub fn cleanup_socket_pair(path: &std::path::Path)
pub async fn connect_socket(path: &std::path::Path) -> Result<Stream>
pub async fn socket_has_live_listener(path: &std::path::Path) -> bool
pub async fn reap_stale_socket_if_dead(path: &std::path::Path) -> bool
pub async fn has_live_listener(path: &std::path::Path) -> bool
pub async fn is_server_ready(path: &std::path::Path) -> bool
pub async fn wait_for_server_ready(path: &std::path::Path, timeout: Duration) -> Result<()>
pub async fn spawn_server_notify(cmd: &mut std::process::Command) -> Result<std::process::Child>
pub fn set_socket_path(path: &str)
Import
use crate::server::socket::{socket_path, connect_socket, has_live_listener};
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| JCODE_SOCKET |
env var |
No /// Custom socket path override
|
| path |
&Path |
Yes (connect/reap) /// Socket path to operate on
|
| timeout |
Duration |
Yes (wait) /// How long to wait for server readiness
|
Outputs
| Name |
Type |
Description
|
| socket_path |
PathBuf /// Resolved socket path
|
| connect_socket |
Result<Stream> /// Connected stream or error
|
| has_live_listener |
bool /// True if a daemon is listening
|
| reap_stale_socket_if_dead |
bool /// True if a stale socket was reaped
|
Usage Examples
Detect and Connect
use crate::server::socket::{socket_path, has_live_listener, connect_socket};
let path = socket_path();
if has_live_listener(&path).await {
let stream = connect_socket(&path).await?;
// Send requests to existing daemon
} else {
// Spawn a new daemon
}
Custom Isolated Socket
# Start an isolated server on a custom socket (does not disturb shared daemon)
jcode run --socket /tmp/jcode-isolated.sock "do a task"
Related Pages
Implements Principle
Requires Environment
Uses Heuristics
Page Connections
Double-click a node to navigate. Hold to expand connections.