forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.rs
More file actions
379 lines (337 loc) · 12.4 KB
/
socket.rs
File metadata and controls
379 lines (337 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
use super::Client;
use crate::transport::Stream;
use anyhow::Result;
use std::path::PathBuf;
use std::time::{Duration, Instant};
pub fn socket_path() -> PathBuf {
if let Ok(custom) = std::env::var("JCODE_SOCKET") {
return PathBuf::from(custom);
}
crate::storage::runtime_dir().join("jcode.sock")
}
/// Debug socket path for testing/introspection
/// Derived from main socket path
pub fn debug_socket_path() -> PathBuf {
let main_path = socket_path();
let filename = main_path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("jcode.sock");
let debug_filename = filename.replace(".sock", "-debug.sock");
main_path.with_file_name(debug_filename)
}
pub(super) fn sibling_socket_path(path: &std::path::Path) -> Option<PathBuf> {
let filename = path.file_name()?.to_str()?;
if let Some(base) = filename.strip_suffix("-debug.sock") {
return Some(path.with_file_name(format!("{}.sock", base)));
}
if let Some(base) = filename.strip_suffix(".sock") {
return Some(path.with_file_name(format!("{}-debug.sock", base)));
}
None
}
/// Remove a socket file and its sibling (main/debug) if present.
pub fn cleanup_socket_pair(path: &std::path::Path) {
let _ = std::fs::remove_file(path);
if let Some(sibling) = sibling_socket_path(path) {
let _ = std::fs::remove_file(sibling);
}
}
/// Connect to a socket path.
///
/// 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.
pub async fn connect_socket(path: &std::path::Path) -> Result<Stream> {
match Stream::connect(path).await {
Ok(stream) => Ok(stream),
Err(err) if err.kind() == std::io::ErrorKind::ConnectionRefused && path.exists() => {
anyhow::bail!(
"Socket exists but refused the connection at {}. Retry, or remove it after confirming no jcode server is running.",
path.display()
)
}
Err(err) if err.raw_os_error() == Some(libc::EMFILE) => Err(anyhow::anyhow!(
"{} ({})",
err,
crate::util::process_fd_diagnostic_snapshot()
)),
Err(err) => Err(err.into()),
}
}
pub(super) async fn socket_has_live_listener(path: &std::path::Path) -> bool {
crate::transport::is_socket_path(path) && Stream::connect(path).await.is_ok()
}
/// Return true if a live server process is listening on the socket path.
///
/// This is intentionally weaker than [`is_server_ready`]: a live listener may
/// still be finishing startup or be temporarily too busy to answer a ping
/// within the short readiness timeout. Callers that must avoid spawning a
/// duplicate daemon should prefer this check over a ping-only probe.
pub async fn has_live_listener(path: &std::path::Path) -> bool {
socket_has_live_listener(path).await
}
#[cfg(unix)]
pub(super) fn daemon_lock_path() -> PathBuf {
crate::storage::runtime_dir().join("jcode-daemon.lock")
}
#[cfg(unix)]
pub(super) struct DaemonLockGuard {
_file: std::fs::File,
path: PathBuf,
}
#[cfg(unix)]
impl Drop for DaemonLockGuard {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.path);
}
}
#[cfg(unix)]
pub(super) fn try_acquire_daemon_lock(path: &std::path::Path) -> Result<Option<DaemonLockGuard>> {
use std::fs::OpenOptions;
use std::os::fd::AsRawFd;
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
let file = OpenOptions::new()
.create(true)
.write(true)
.truncate(false)
.open(path)?;
let fd = file.as_raw_fd();
let ret = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) };
if ret == 0 {
Ok(Some(DaemonLockGuard {
_file: file,
path: path.to_path_buf(),
}))
} else {
Ok(None)
}
}
#[cfg(unix)]
pub(super) fn acquire_daemon_lock() -> Result<DaemonLockGuard> {
let path = daemon_lock_path();
try_acquire_daemon_lock(&path)?.ok_or_else(|| {
anyhow::anyhow!(
"Another jcode server process is already running for runtime dir {}",
crate::storage::runtime_dir().display()
)
})
}
#[cfg(unix)]
pub(super) fn mark_close_on_exec<T: std::os::fd::AsRawFd>(io: &T) {
let fd = io.as_raw_fd();
let flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
if flags >= 0 {
let _ = unsafe { libc::fcntl(fd, libc::F_SETFD, flags | libc::FD_CLOEXEC) };
}
}
pub fn set_socket_path(path: &str) {
crate::env::set_var("JCODE_SOCKET", path);
}
/// Spawn a server child process and wait until it signals readiness.
///
/// Creates an anonymous pipe, passes the write-end fd to the child via
/// `JCODE_READY_FD`, and awaits a single byte on the read end. The server
/// calls `signal_ready_fd()` once its accept loops are spawned, so the future
/// resolves only after the daemon can start servicing client requests.
///
/// Falls back to a short poll loop if the pipe read times out (e.g. server
/// built without ready-fd support, or crash before bind).
#[cfg(unix)]
pub async fn spawn_server_notify(cmd: &mut std::process::Command) -> Result<std::process::Child> {
use std::os::unix::io::FromRawFd;
use std::os::unix::process::CommandExt;
// Create a pipe: fds[0] = read end, fds[1] = write end.
// Use pipe2 with O_CLOEXEC on the read end (parent keeps it).
// The write end needs CLOEXEC cleared so it survives exec in the child.
let mut fds = [0i32; 2];
if unsafe { libc::pipe(fds.as_mut_ptr()) } != 0 {
anyhow::bail!("pipe() failed: {}", std::io::Error::last_os_error());
}
let read_fd = fds[0];
let write_fd = fds[1];
// Set CLOEXEC on the read end (parent only)
unsafe {
let flags = libc::fcntl(read_fd, libc::F_GETFD);
if flags >= 0 {
libc::fcntl(read_fd, libc::F_SETFD, flags | libc::FD_CLOEXEC);
}
}
// Pass the write-end fd to the child and tell it the fd number.
unsafe {
cmd.pre_exec(move || {
// Clear CLOEXEC on the write end so it survives exec
let flags = libc::fcntl(write_fd, libc::F_GETFD);
if flags >= 0 {
libc::fcntl(write_fd, libc::F_SETFD, flags & !libc::FD_CLOEXEC);
}
libc::setsid();
Ok(())
});
}
cmd.env("JCODE_READY_FD", write_fd.to_string());
let mut child = cmd.spawn()?;
// Close our copy of the write end so we get EOF if the child dies.
unsafe { libc::close(write_fd) };
// Wait for the ready signal (or timeout / child death).
let read_file = unsafe { std::fs::File::from_raw_fd(read_fd) };
let mut async_file = tokio::fs::File::from_std(read_file);
let mut buf = [0u8; 1];
match tokio::time::timeout(
Duration::from_secs(10),
tokio::io::AsyncReadExt::read(&mut async_file, &mut buf),
)
.await
{
Ok(Ok(1)) => {
crate::logging::info("Server signalled ready via pipe");
}
Ok(Ok(_)) => {
if let Some(status) = child.try_wait()? {
handle_server_start_exit(&mut child, status).await?;
}
crate::logging::info(
"Server closed ready pipe without signalling; falling back to poll",
);
wait_for_server_ready(&socket_path(), Duration::from_secs(5)).await?;
}
Ok(Err(e)) => {
crate::logging::info(&format!(
"Ready pipe read error: {}; falling back to poll",
e
));
wait_for_server_ready(&socket_path(), Duration::from_secs(5)).await?;
}
Err(_) => {
crate::logging::info("Timed out waiting for server ready signal; falling back to poll");
wait_for_server_ready(&socket_path(), Duration::from_secs(5)).await?;
}
}
if let Some(mut stderr) = child.stderr.take() {
// The shared daemon outlives the spawning client. Keep draining the
// stderr pipe after startup so later reloads cannot die on SIGPIPE
// when they emit provider/model selection notices during boot.
std::thread::spawn(move || {
let mut sink = std::io::sink();
let _ = std::io::copy(&mut stderr, &mut sink);
});
}
Ok(child)
}
/// Wait until a server socket is connectable and responds to a ping.
pub async fn wait_for_server_ready(path: &std::path::Path, timeout: Duration) -> Result<()> {
let start = Instant::now();
while start.elapsed() < timeout {
if crate::transport::is_socket_path(path)
&& let Ok(mut client) = Client::connect_with_path(path.to_path_buf()).await
&& let Ok(Ok(true)) =
tokio::time::timeout(Duration::from_millis(250), client.ping()).await
{
return Ok(());
}
tokio::time::sleep(Duration::from_millis(50)).await;
}
anyhow::bail!(
"Timed out waiting for responsive server socket {}",
path.display()
);
}
async fn probe_server_ready(path: &std::path::Path, ping_timeout: Duration) -> bool {
if !crate::transport::is_socket_path(path) {
return false;
}
let Ok(mut client) = Client::connect_with_path(path.to_path_buf()).await else {
return false;
};
matches!(
tokio::time::timeout(ping_timeout, client.ping()).await,
Ok(Ok(true))
)
}
pub async fn is_server_ready(path: &std::path::Path) -> bool {
probe_server_ready(path, Duration::from_millis(50)).await
}
#[cfg(unix)]
pub(super) fn take_server_start_stderr(child: &mut std::process::Child) -> String {
use std::io::Read;
child
.stderr
.take()
.and_then(|mut stderr| {
let mut buf = String::new();
stderr.read_to_string(&mut buf).ok()?;
Some(buf)
})
.unwrap_or_default()
}
#[cfg(unix)]
pub(super) fn server_start_matches_existing_server(stderr_output: &str) -> bool {
stderr_output.contains("Another jcode server process is already running")
|| stderr_output.contains("Refusing to replace active server socket")
}
pub(super) async fn wait_for_existing_server(path: &std::path::Path, timeout: Duration) -> bool {
let start = Instant::now();
while start.elapsed() < timeout {
if is_server_ready(path).await || has_live_listener(path).await {
return true;
}
tokio::time::sleep(Duration::from_millis(50)).await;
}
false
}
#[cfg(unix)]
pub(super) fn format_server_start_error(
status: std::process::ExitStatus,
stderr_output: &str,
) -> String {
if stderr_output.trim().is_empty() {
format!(
"Server exited before signalling ready ({}). Check logs at ~/.jcode/logs/",
status
)
} else {
format!(
"Server exited before signalling ready ({}):\n{}",
status,
stderr_output.trim()
)
}
}
#[cfg(unix)]
pub(super) async fn handle_server_start_exit(
child: &mut std::process::Child,
status: std::process::ExitStatus,
) -> Result<()> {
let stderr_output = take_server_start_stderr(child);
if server_start_matches_existing_server(&stderr_output) {
let socket_path = socket_path();
if wait_for_existing_server(&socket_path, Duration::from_secs(5)).await {
crate::logging::info(
"Server spawn raced with an existing daemon; treating startup as successful",
);
return Ok(());
}
}
anyhow::bail!(format_server_start_error(status, &stderr_output));
}
/// Write a single byte to the fd in `JCODE_READY_FD` and close it.
/// Called after startup plumbing is ready so the parent process knows the
/// server can accept and service client requests. The env var is cleared so child
/// processes (e.g. tool subprocesses) don't inherit a stale fd.
pub(super) fn signal_ready_fd() {
#[cfg(unix)]
{
use std::os::unix::io::FromRawFd;
if let Ok(fd_str) = std::env::var("JCODE_READY_FD") {
crate::env::remove_var("JCODE_READY_FD");
if let Ok(fd) = fd_str.parse::<i32>() {
let mut file = unsafe { std::fs::File::from_raw_fd(fd) };
let _ = std::io::Write::write_all(&mut file, b"R");
// file is dropped here which closes the fd
}
}
}
}