running-process 4.5.2

Subprocess and PTY runtime for the running-process project
Documentation
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! Continuation of integration tests — split for file-size budget.

use running_process::daemon::client::{DaemonClient, SpawnCommandRequest};
use running_process::proto::daemon::StatusCode;

use super::{make_register_request, scaled, start_server_with_tempdb};

// ---------------------------------------------------------------------------
// Test 9: status shows tracked_process_count
// ---------------------------------------------------------------------------

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_status_shows_tracked_count() {
    let scope = format!("integ2-{}", line!());
    let (server_handle, socket, _tmp_dir) = start_server_with_tempdb(&scope);

    tokio::time::sleep(std::time::Duration::from_millis(300)).await;

    let result = tokio::task::spawn_blocking(move || {
        let mut client = DaemonClient::connect_to(&socket).expect("failed to connect to daemon");

        // Status before any registrations -> tracked_process_count == 0.
        let status0 = client.status().expect("status failed");
        assert_eq!(status0.code, StatusCode::Ok as i32);
        let s0 = status0.status.expect("status payload missing");
        assert_eq!(
            s0.tracked_process_count, 0,
            "expected 0 tracked processes initially"
        );

        // Register 2 processes.
        let reg1 = make_register_request(
            20001,
            1000.0,
            "subprocess",
            "proc1",
            "/tmp",
            "TOOL:1",
            "contained",
        );
        let resp1 = client.send_request(reg1).expect("register 20001 failed");
        assert_eq!(resp1.code, StatusCode::Ok as i32);

        let reg2 =
            make_register_request(20002, 2000.0, "pty", "proc2", "/home", "TOOL:2", "detached");
        let resp2 = client.send_request(reg2).expect("register 20002 failed");
        assert_eq!(resp2.code, StatusCode::Ok as i32);

        // Status after registrations -> tracked_process_count == 2.
        let status2 = client.status().expect("status after register failed");
        assert_eq!(status2.code, StatusCode::Ok as i32);
        let s2 = status2
            .status
            .expect("status payload missing after register");
        assert_eq!(
            s2.tracked_process_count, 2,
            "expected 2 tracked processes after registering 2"
        );

        // Clean up.
        let _ = client.shutdown(true, 5.0);
    })
    .await;
    result.expect("client task panicked");

    let _ = tokio::time::timeout(std::time::Duration::from_secs(5), server_handle).await;
}

// ---------------------------------------------------------------------------
// Test 10: spawn_daemon runs a detached command under daemon control
// ---------------------------------------------------------------------------

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_spawn_daemon_tracks_spawned_process_and_context() {
    let scope = format!("integ-spawn-{}", line!());
    let (server_handle, socket, _tmp_dir) = start_server_with_tempdb(&scope);

    tokio::time::sleep(scaled(std::time::Duration::from_millis(300))).await;

    let workdir = tempfile::tempdir().expect("failed to create temp dir");
    let workdir_path = workdir.path().to_path_buf();
    let workdir_string = workdir_path.to_string_lossy().into_owned();
    let env_file = workdir_path.join("spawn-env.txt");
    let cwd_file = workdir_path.join("spawn-cwd.txt");

    let command = if cfg!(windows) {
        format!(
            "echo %RP_DAEMON_TEST_VAR%> \"{}\" & cd > \"{}\" & ping 127.0.0.1 -n 6 >NUL",
            env_file.display(),
            cwd_file.display()
        )
    } else {
        format!(
            "printf '%s' \"$RP_DAEMON_TEST_VAR\" > \"{}\"; pwd > \"{}\"; sleep 5",
            env_file.display(),
            cwd_file.display()
        )
    };

    let socket_for_client = socket.clone();
    let workdir_for_client = workdir_path.clone();
    let result = tokio::task::spawn_blocking(move || {
        let mut client =
            DaemonClient::connect_to(&socket_for_client).expect("failed to connect to daemon");

        let request = SpawnCommandRequest::shell(command)
            .with_cwd(workdir_for_client.clone())
            .with_env("RP_DAEMON_TEST_VAR", "daemon-spawn-ok")
            .with_originator("TEST:spawn");

        let spawned = client
            .spawn_command(&request)
            .expect("spawn_command should succeed");
        assert!(spawned.pid > 0, "spawned pid should be > 0");
        assert_eq!(spawned.originator.as_deref(), Some("TEST:spawn"));
        assert_eq!(
            spawned.cwd.as_deref(),
            Some(workdir_for_client.to_string_lossy().as_ref())
        );
        assert_eq!(spawned.containment, "detached");

        std::thread::sleep(scaled(std::time::Duration::from_millis(600)));

        let list_resp = client.list_active().expect("list_active failed");
        let processes = list_resp
            .list_active
            .expect("list_active payload missing")
            .processes;
        let tracked = processes
            .iter()
            .find(|process| process.pid == spawned.pid)
            .expect("spawned process should be tracked");
        assert_eq!(tracked.command, request.command);
        assert_eq!(tracked.originator, "TEST:spawn");
        assert_eq!(tracked.containment, "detached");

        let kill_resp = client
            .kill_tree(spawned.pid, 3.0)
            .expect("kill_tree should succeed");
        assert_eq!(kill_resp.code, StatusCode::Ok as i32);

        let _ = client.shutdown(true, 5.0);
    })
    .await;
    result.expect("client task panicked");

    let env_contents = std::fs::read_to_string(&env_file).expect("env file should exist");
    assert!(
        env_contents.contains("daemon-spawn-ok"),
        "expected daemon-spawn-ok in env file, got: {env_contents}"
    );

    let cwd_contents = std::fs::read_to_string(&cwd_file).expect("cwd file should exist");
    let observed_cwd = std::fs::canonicalize(cwd_contents.trim())
        .unwrap_or_else(|_| std::path::PathBuf::from(cwd_contents.trim()));
    let expected_cwd = std::fs::canonicalize(workdir.path())
        .unwrap_or_else(|_| std::path::PathBuf::from(&workdir_string));
    assert_eq!(observed_cwd, expected_cwd);

    let _ = tokio::time::timeout(std::time::Duration::from_secs(5), server_handle).await;
}

// ---------------------------------------------------------------------------
// Test 11: spawned commands survive daemon shutdown
// ---------------------------------------------------------------------------

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_spawned_process_survives_daemon_shutdown() {
    let scope = format!("integ-spawn-survive-{}", line!());
    let (server_handle, socket, _tmp_dir) = start_server_with_tempdb(&scope);

    tokio::time::sleep(scaled(std::time::Duration::from_millis(300))).await;

    let workdir = tempfile::tempdir().expect("failed to create temp dir");
    let marker = workdir.path().join("survived.txt");

    let command = if cfg!(windows) {
        format!(
            "ping 127.0.0.1 -n 3 >NUL & echo survived> \"{}\" & ping 127.0.0.1 -n 3 >NUL",
            marker.display()
        )
    } else {
        format!(
            "sleep 1; printf 'survived' > \"{}\"; sleep 2",
            marker.display()
        )
    };

    let socket_for_client = socket.clone();
    let marker_for_assert = marker.clone();
    let result = tokio::task::spawn_blocking(move || {
        let mut client =
            DaemonClient::connect_to(&socket_for_client).expect("failed to connect to daemon");

        let spawned = client
            .spawn_command(&SpawnCommandRequest::shell(command))
            .expect("spawn_command should succeed");
        assert!(spawned.pid > 0);
        assert!(
            spawned.originator.is_some(),
            "spawned process should record originator metadata"
        );

        let shutdown = client.shutdown(true, 5.0).expect("shutdown failed");
        assert_eq!(shutdown.code, StatusCode::Ok as i32);

        let _ = std::fs::metadata(&marker_for_assert);
    })
    .await;
    result.expect("client task panicked");

    tokio::time::timeout(scaled(std::time::Duration::from_secs(5)), server_handle)
        .await
        .expect("server did not stop in time")
        .expect("server task panicked");

    let deadline = std::time::Instant::now() + scaled(std::time::Duration::from_secs(6));
    while !marker.exists() && std::time::Instant::now() < deadline {
        std::thread::sleep(std::time::Duration::from_millis(100));
    }

    let contents = std::fs::read_to_string(&marker).expect("marker file should exist");
    assert_eq!(contents.trim(), "survived");
}

// ===========================================================================
// Phase 4: Reaper, KillTree, KillZombies, GetProcessTree integration tests
// ===========================================================================

// ---------------------------------------------------------------------------
// Test 10: kill_zombies dry_run with no zombies returns OK and empty list
// ---------------------------------------------------------------------------

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_kill_zombies_dry_run_with_no_zombies() {
    let scope = format!("integ4-{}", line!());
    let (server_handle, socket, _tmp_dir) = start_server_with_tempdb(&scope);

    tokio::time::sleep(std::time::Duration::from_millis(300)).await;

    let result = tokio::task::spawn_blocking(move || {
        let mut client = DaemonClient::connect_to(&socket).expect("failed to connect to daemon");

        let resp = client
            .kill_zombies(true)
            .expect("kill_zombies dry_run failed");
        assert_eq!(
            resp.code,
            StatusCode::Ok as i32,
            "kill_zombies should return OK"
        );
        let zombies = resp
            .kill_zombies
            .expect("kill_zombies payload missing")
            .zombies;
        // Filter out orphan conhost.exe entries — those are ambient system
        // state, not controlled by this test.
        let registry_zombies: Vec<_> = zombies
            .iter()
            .filter(|z| z.command != "conhost.exe")
            .collect();
        assert!(
            registry_zombies.is_empty(),
            "expected no registry zombies in empty registry, got {}",
            registry_zombies.len()
        );

        // Clean up.
        let _ = client.shutdown(true, 5.0);
    })
    .await;
    result.expect("client task panicked");

    let _ = tokio::time::timeout(std::time::Duration::from_secs(5), server_handle).await;
}

// ---------------------------------------------------------------------------
// Test 11: kill_zombies (non-dry-run) with no zombies returns OK and empty list
// ---------------------------------------------------------------------------

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_kill_zombies_with_no_zombies() {
    let scope = format!("integ4-{}", line!());
    let (server_handle, socket, _tmp_dir) = start_server_with_tempdb(&scope);

    tokio::time::sleep(std::time::Duration::from_millis(300)).await;

    let result = tokio::task::spawn_blocking(move || {
        let mut client = DaemonClient::connect_to(&socket).expect("failed to connect to daemon");

        let resp = client.kill_zombies(false).expect("kill_zombies failed");
        assert_eq!(
            resp.code,
            StatusCode::Ok as i32,
            "kill_zombies should return OK"
        );
        let zombies = resp
            .kill_zombies
            .expect("kill_zombies payload missing")
            .zombies;
        // Filter out orphan conhost.exe entries — ambient system state.
        let registry_zombies: Vec<_> = zombies
            .iter()
            .filter(|z| z.command != "conhost.exe")
            .collect();
        assert!(
            registry_zombies.is_empty(),
            "expected no registry zombies in empty registry, got {}",
            registry_zombies.len()
        );

        // Clean up.
        let _ = client.shutdown(true, 5.0);
    })
    .await;
    result.expect("client task panicked");

    let _ = tokio::time::timeout(std::time::Duration::from_secs(5), server_handle).await;
}

// ---------------------------------------------------------------------------
// Test 12: kill_tree for a nonexistent PID returns OK with 0 killed
// ---------------------------------------------------------------------------

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_kill_tree_nonexistent_pid() {
    let scope = format!("integ4-{}", line!());
    let (server_handle, socket, _tmp_dir) = start_server_with_tempdb(&scope);

    tokio::time::sleep(std::time::Duration::from_millis(300)).await;

    let result = tokio::task::spawn_blocking(move || {
        let mut client = DaemonClient::connect_to(&socket).expect("failed to connect to daemon");

        // Use a PID that almost certainly does not exist.
        let resp = client.kill_tree(4_000_099, 3.0).expect("kill_tree failed");
        assert_eq!(
            resp.code,
            StatusCode::Ok as i32,
            "kill_tree should return OK"
        );
        let count = resp
            .kill_tree
            .expect("kill_tree payload missing")
            .processes_killed;
        assert_eq!(count, 0, "expected 0 processes killed for nonexistent PID");

        // Clean up.
        let _ = client.shutdown(true, 5.0);
    })
    .await;
    result.expect("client task panicked");

    let _ = tokio::time::timeout(std::time::Duration::from_secs(5), server_handle).await;
}

// ---------------------------------------------------------------------------
// Test 13: get_process_tree for current process returns non-empty tree
// ---------------------------------------------------------------------------

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_get_process_tree_for_current_process() {
    let scope = format!("integ4-{}", line!());
    let (server_handle, socket, _tmp_dir) = start_server_with_tempdb(&scope);

    tokio::time::sleep(std::time::Duration::from_millis(300)).await;

    let current_pid = std::process::id();
    let result = tokio::task::spawn_blocking(move || {
        let mut client = DaemonClient::connect_to(&socket).expect("failed to connect to daemon");

        let resp = client
            .get_process_tree(current_pid)
            .expect("get_process_tree failed");
        assert_eq!(
            resp.code,
            StatusCode::Ok as i32,
            "get_process_tree should return OK"
        );
        let tree_display = resp
            .get_process_tree
            .expect("get_process_tree payload missing")
            .tree_display;
        assert!(
            !tree_display.is_empty(),
            "tree display should not be empty for current process"
        );
        assert!(
            tree_display.contains(&format!("pid={current_pid}")),
            "tree display should contain current PID, got: {tree_display}"
        );

        // Clean up.
        let _ = client.shutdown(true, 5.0);
    })
    .await;
    result.expect("client task panicked");

    let _ = tokio::time::timeout(std::time::Duration::from_secs(5), server_handle).await;
}

// ---------------------------------------------------------------------------
// Test 14: kill_zombies finds a registered dead process via dry-run
// ---------------------------------------------------------------------------

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_kill_zombies_finds_registered_dead_process() {
    let scope = format!("integ4-{}", line!());
    let (server_handle, socket, _tmp_dir) = start_server_with_tempdb(&scope);

    tokio::time::sleep(std::time::Duration::from_millis(300)).await;

    let result = tokio::task::spawn_blocking(move || {
        let mut client = DaemonClient::connect_to(&socket).expect("failed to connect to daemon");

        // Register a fake dead PID (4_000_050 is unlikely to be a real process).
        let reg_req = make_register_request(
            4_000_050,
            1000.0,
            "subprocess",
            "fake-dead-cmd",
            "/tmp",
            "TEST:zombie",
            "contained",
        );
        let reg_resp = client.send_request(reg_req).expect("register failed");
        assert_eq!(
            reg_resp.code,
            StatusCode::Ok as i32,
            "register should succeed"
        );

        // Dry-run: should detect the dead process as a zombie.
        let resp = client
            .kill_zombies(true)
            .expect("kill_zombies dry_run failed");
        assert_eq!(
            resp.code,
            StatusCode::Ok as i32,
            "kill_zombies should return OK"
        );
        let zombies = resp
            .kill_zombies
            .expect("kill_zombies payload missing")
            .zombies;
        // Filter out orphan conhost.exe entries — ambient system state.
        let registry_zombies: Vec<_> = zombies
            .iter()
            .filter(|z| z.command != "conhost.exe")
            .collect();
        assert_eq!(
            registry_zombies.len(),
            1,
            "expected 1 registry zombie for dead fake PID, got {}",
            registry_zombies.len()
        );
        assert_eq!(registry_zombies[0].pid, 4_000_050);
        assert_eq!(registry_zombies[0].command, "fake-dead-cmd");
        assert!(
            !registry_zombies[0].killed,
            "dry_run should not kill the process"
        );

        // The process should still be in the registry (dry-run does not remove).
        let list_resp = client.list_active().expect("list_active failed");
        let procs = list_resp
            .list_active
            .expect("list_active payload missing")
            .processes;
        assert_eq!(
            procs.len(),
            1,
            "process should still be tracked after dry-run"
        );

        // Clean up.
        let _ = client.shutdown(true, 5.0);
    })
    .await;
    result.expect("client task panicked");

    let _ = tokio::time::timeout(std::time::Duration::from_secs(5), server_handle).await;
}