Implementation:1jehuang Jcode Server run
| Knowledge Sources | |
|---|---|
| Domains | Systems_Architecture, Concurrency |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
Concrete tool for running the jcode daemon server lifecycle, including socket binding, accept loops, idle timeout, and graceful shutdown, provided by the jcode server module.
Description
The Server::run method is the main entry point for the daemon process. It binds the main and debug listeners on the socket path, acquires the daemon lock, writes server metadata, and spawns the accept loops for client connections. It handles temporary server policy (idle timeout, owner-pid liveness monitoring), graceful shutdown (draining in-flight session saves), and disk cleanup.
The lifecycle module provides drain_session_saves_before_exit() which waits (bounded by 5 seconds) for any in-flight session checkpoint to complete before the process exits, preventing duplicate history on the next load. configure_temporary_server() sets environment variables marking the server as temporary with an owner PID and idle timeout.
Usage
- Start daemon: Called by
jcode serveor implicitly when a client spawns a daemon. - Temporary server: Configured via
JCODE_TEMP_SERVERenv var for per-run isolated servers. - Graceful shutdown: SIGTERM or idle timeout triggers
drain_session_saves_before_exit().
Code Reference
Source Location
- Repository: jcode
- File: crates/jcode-app-core/src/server.rs, crates/jcode-app-core/src/server/lifecycle.rs
Signature
// Server::run — main daemon entry point
impl Server {
pub async fn run(&self) -> Result<()>
}
// lifecycle.rs
pub(crate) async fn drain_session_saves_before_exit()
pub fn configure_temporary_server(
owner_pid: Option<u32>,
idle_timeout_secs: Option<u64>,
)
pub(crate) fn temporary_server_policy_from_env() -> Option<TemporaryServerPolicy>
pub(crate) fn spawn_temporary_lifecycle_monitor(
client_count: Arc<RwLock<usize>>,
sessions: super::SessionAgents,
swarm_state: super::SwarmState,
socket_path: PathBuf,
debug_socket_path: PathBuf,
server_name: String,
policy: TemporaryServerPolicy,
)
pub fn idle_monitor_should_start(
client_count: usize,
has_live_headless_worker: bool,
) -> bool
Import
// Internal server lifecycle, not a public import.
// Accessed via: jcode serve
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| socket_path | PathBuf | Yes /// Socket to bind | |
| idle_timeout_secs | u64 | No /// Default 1800s (30 min) for temp servers | |
| owner_pid | Option<u32> | No /// Owner process for temporary servers |
Outputs
| Name | Type | Description |
|---|---|---|
| listeners | bound | Main and debug socket listeners accepting connections |
| metadata | file /// servers.json with PID, socket path, started_at
| |
| exit | process /// Graceful drain on SIGTERM or idle timeout |
Usage Examples
# Explicitly start the shared daemon
jcode serve
# Or implicitly — any jcode command spawns it if not running
jcode run "hello"
Temporary Server with Idle Timeout
# Start a temporary server that exits after 300s idle
JCODE_TEMP_SERVER=1 JCODE_TEMP_SERVER_IDLE_SECS=300 jcode serve