Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:1jehuang Jcode Stale Socket Reaping

From Leeroopedia
Knowledge Sources
Domains Systems, Reliability
Last Updated 2026-10-18 00:00 GMT

Overview

Safe cleanup of dead daemon sockets by checking both listener liveness and daemon lock acquisition before unlinking, preventing stranded daemons.

Description

The Stale_Socket_Reaping heuristic ensures that when a jcode client encounters a socket with no live listener, it does not blindly unlink the socket path. Instead, reap_stale_socket_if_dead() checks both that the socket has no live listener and that the daemon lock can be acquired. A client-side cleanup that unlinks a refused socket can strand a live daemon behind an unlinked pathname, leaving the process running with the daemon lock held while new clients can no longer discover or connect to it. The reaper only removes the socket when it confirms no daemon is listening and no lock is held.

Usage

Use this heuristic on client startup when detecting a stale socket. Call reap_stale_socket_if_dead() before attempting to spawn a new daemon. Never unlink a socket on connection-refused alone — a client-side cleanup can strand a live daemon.

The Insight (Rule of Thumb)

  • Action: Call reap_stale_socket_if_dead(path) which checks listener liveness AND daemon lock acquisition before unlinking.
  • Value: Two-condition check: socket_has_live_listener() == false AND daemon_lock_acquired == true.
  • Trade-off: Slightly slower startup (two checks) but prevents stranding a live daemon.
  • Compatibility: Unix-only (#[cfg(unix)]); also detects EMFILE (too many open files) as a distinct error.

Reasoning

A daemon may briefly refuse connections while holding its lock (e.g., during startup or under load). Unlinking the socket in this state strands the daemon: it's still running and holding the lock, but new clients can't find the socket. The two-condition check (no listener AND lock acquirable) ensures the daemon is truly dead before removing its socket.

Code Evidence

Connection-refused handling from crates/jcode-app-core/src/server/socket.rs:107-121:

/// Do not unlink the path on connection-refused here. A client-side cleanup can
/// strand a live daemon behind an unlinked Unix socket pathname, leaving the
/// process running with the daemon lock held while new clients can no longer
/// discover or connect to it.

EMFILE detection from crates/jcode-app-core/src/server/socket.rs:122-125:

Err(err) if err.raw_os_error() == Some(libc::EMFILE) => Err(anyhow::anyhow!(
    "{} ({})",
    err,
    crate::util::process_fd_diagnostic_snapshot()

Reaper function from crates/jcode-app-core/src/server/socket.rs:162-165:

/// Returns true if a stale socket was reaped.
#[cfg(unix)]
pub async fn reap_stale_socket_if_dead(path: &std::path::Path) -> bool {
    // Nothing to reap if the path isn't even present.
    if !crate::transport::is_socket_path(path) {
        return false;

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment