proc-cli 2.0.1

Semantic CLI tool for process management. Target by port, PID, name or path.
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
//! `proc free` - Free ports by killing their processes and verifying availability
//!
//! Examples:
//!   proc free :3000              # Kill process on port 3000, verify port freed
//!   proc free :3000 :8080        # Free several ports: separate arguments
//!   proc free :3000 --yes        # Skip confirmation
//!   proc free :3000 --wait 30    # Wait up to 30s for port to free
//!
//! Commas still work (`proc free :3000,:8080`) but are deprecated.

use crate::core::port::PortInfo;
use crate::core::{flatten_targets, parse_target, Process, TargetType};
use crate::error::{ExitCode, ProcError, Result};
use crate::ui::{must_confirm, plural, Printer};
use clap::Args;
use colored::*;
use dialoguer::Confirm;
use serde::Serialize;

/// Free port(s) by killing the process and verifying the port is available
#[derive(Args, Debug)]
pub struct FreeCommand {
    /// Port target(s): :port (separate arguments for several, e.g. :3000 :8080)
    #[arg(required = true, num_args = 1.., value_parser = crate::core::validate_target_arg)]
    pub target: Vec<String>,

    /// Skip confirmation prompt
    #[arg(long, short = 'y')]
    pub yes: bool,

    /// Show what would be freed without actually freeing
    #[arg(long)]
    pub dry_run: bool,

    /// Output as JSON
    #[arg(long, short = 'j')]
    pub json: bool,

    /// Show verbose output
    #[arg(long, short = 'v')]
    pub verbose: bool,

    /// Max seconds to wait for port to become free
    #[arg(long, default_value = "10")]
    pub wait: u64,
}

impl FreeCommand {
    /// Executes the free command, killing processes and verifying ports are freed.
    pub fn execute(&self) -> Result<()> {
        let printer = Printer::from_flags(self.json, self.verbose);

        let targets = flatten_targets(&self.target);

        // Validate all targets are ports
        let mut ports: Vec<(&String, u16)> = Vec::new();
        for target in &targets {
            match parse_target(target) {
                TargetType::Port(port) => ports.push((target, port)),
                _ => {
                    return Err(ProcError::InvalidInput(format!(
                        "proc free only works with port targets (e.g. :3000), got '{}'. Use proc kill for processes.",
                        target
                    )));
                }
            }
        }

        // Resolve ports to processes: every process holding a port
        let mut port_processes: Vec<(u16, Process)> = Vec::new();
        // the ports nobody holds, as typed (":3000")
        let mut not_found: Vec<String> = Vec::new();

        for (target, port) in &ports {
            // A port nobody listens on, or whose holders have all gone, is not found
            let mut held = false;
            for listener in PortInfo::holders_of(*port)? {
                if let Some(proc) = Process::find_by_pid(listener.pid)? {
                    port_processes.push((*port, proc));
                    held = true;
                }
            }
            if !held {
                printer.warning(&format!("No process listening on port {}", port));
                not_found.push((*target).clone());
            }
        }
        // the JSON results list the ports that matched nothing
        let printer = printer.with_not_found(&not_found);

        if port_processes.is_empty() {
            if not_found.is_empty() {
                return Err(ProcError::InvalidInput(
                    "No port targets specified".to_string(),
                ));
            }
            printer.print_empty_result("free", "All specified ports are already free");
            return Ok(());
        }

        // The ports to free, once each however many processes hold them
        let target_ports: Vec<u16> = {
            let mut seen = std::collections::HashSet::new();
            port_processes
                .iter()
                .map(|(port, _)| *port)
                .filter(|port| seen.insert(*port))
                .collect()
        };

        // Deduplicate processes (multiple ports might be held by same process)
        let mut processes: Vec<Process> = {
            let mut seen = std::collections::HashSet::new();
            port_processes
                .iter()
                .filter(|(_, p)| seen.insert(p.pid))
                .map(|(_, p)| p.clone())
                .collect()
        };

        // Real CPU readings whenever the list is shown or returned (dry run,
        // the prompt, or the error for a missing `--yes`); `--yes` acts at once
        if self.dry_run || !self.yes {
            Process::measure_cpu(&mut processes);
        }

        if self.dry_run {
            printer.print_dry_run_with(
                "free",
                &processes,
                &format!(
                    "Dry run: would kill {} process{} to free {} port{}",
                    processes.len(),
                    plural(processes.len()),
                    target_ports.len(),
                    if target_ports.len() == 1 { "" } else { "s" }
                ),
            );
            return Ok(());
        }

        // Same rule everywhere: --yes, or ask; --json never skips it
        if must_confirm("free", self.yes, self.json, &processes, Some(&not_found))? {
            printer.print_confirmation("free", &processes);

            let prompt = format!(
                "Kill {} process{} to free {} port{}?",
                processes.len(),
                plural(processes.len()),
                target_ports.len(),
                if target_ports.len() == 1 { "" } else { "s" }
            );

            if !Confirm::new()
                .with_prompt(prompt)
                .default(false)
                .interact()?
            {
                printer.warning("Aborted");
                return Ok(());
            }
        }

        // SIGTERM first, then SIGKILL for what is left
        let start = std::time::Instant::now();
        stop_all(
            &processes,
            std::time::Duration::from_secs(self.wait.min(5)),
            &printer,
            Process::terminate,
            Process::kill,
        );

        // Poll until ports are free
        let mut freed: Vec<u16> = Vec::new();
        let mut still_busy: Vec<u16> = Vec::new();

        // --wait covers the whole run, graceful exit included, so it is a real
        // maximum; after a force kill the port still gets a second to be released
        let poll_deadline = std::cmp::max(
            start + std::time::Duration::from_secs(self.wait),
            std::time::Instant::now() + std::time::Duration::from_secs(1),
        );

        // A port counts as free once it reads free in two looks in a row: a
        // copy of the holder (a supervisor, a forked worker) can take it back
        // within milliseconds of the holder's death, and a single look between
        // the two says "free" about a port that is in use again
        let mut was_free = false;
        loop {
            let mut all_free = true;
            freed.clear();
            still_busy.clear();

            for port in &target_ports {
                // free once nobody is left listening on it, whoever that was
                match PortInfo::holders_of(*port) {
                    Ok(holders) if holders.is_empty() => freed.push(*port),
                    _ => {
                        still_busy.push(*port);
                        all_free = false;
                    }
                }
            }

            let settled = all_free && was_free;
            was_free = all_free;
            if settled || std::time::Instant::now() >= poll_deadline {
                break;
            }

            std::thread::sleep(std::time::Duration::from_millis(250));
        }

        // Report results
        if self.json {
            let results: Vec<FreeResult> = freed
                .iter()
                .map(|p| FreeResult {
                    port: *p,
                    freed: true,
                })
                .chain(still_busy.iter().map(|p| FreeResult {
                    port: *p,
                    freed: false,
                }))
                .collect();

            printer.print_json(&FreeOutput {
                action: "free",
                success: still_busy.is_empty(),
                results,
                not_found: &not_found,
            });
        } else {
            for port in &freed {
                println!(
                    "{} Freed port {}",
                    "✓".green().bold(),
                    port.to_string().cyan().bold()
                );
            }
            for port in &still_busy {
                println!(
                    "{} Port {} still in use (may be in TIME_WAIT)",
                    "✗".red().bold(),
                    port.to_string().cyan()
                );
            }
        }

        if still_busy.is_empty() {
            Ok(())
        } else {
            Err(ProcError::reported(
                ExitCode::GeneralError,
                format!(
                    "{} port{} still in use",
                    still_busy.len(),
                    if still_busy.len() == 1 { "" } else { "s" }
                ),
            ))
        }
    }
}

#[derive(Serialize)]
struct FreeOutput<'a> {
    action: &'static str,
    success: bool,
    results: Vec<FreeResult>,
    /// The ports nobody was listening on, as typed
    not_found: &'a [String],
}

#[derive(Serialize)]
struct FreeResult {
    port: u16,
    freed: bool,
}

/// Ask each process to end (SIGTERM; on Windows a refused request for a process
/// that is still running is not a failure), give them up to `grace` to do it, then
/// force kill whatever is still running. A signal that fails is a warning, not
/// an error: the port check afterwards says whether it mattered. Returns the
/// PIDs a signal failed for. The signals are parameters so a test can make them
/// fail; the command passes `Process::terminate` and `Process::kill`.
fn stop_all(
    processes: &[Process],
    grace: std::time::Duration,
    printer: &Printer,
    terminate: impl Fn(&Process) -> Result<()>,
    kill: impl Fn(&Process) -> Result<()>,
) -> Vec<u32> {
    let mut failed = Vec::new();
    for proc in processes {
        match terminate(proc) {
            // Windows can't ask a console program to close (taskkill without
            // /F needs a window); the force kill below still stops it
            Err(_) if cfg!(not(unix)) && proc.is_running() => {}
            Err(e) => {
                printer.warning(&format!(
                    "Failed to ask {} [PID {}] to stop: {}",
                    proc.name, proc.pid, e
                ));
                failed.push(proc.pid);
            }
            Ok(()) => {}
        }
    }

    // Wait for graceful exit
    let start = std::time::Instant::now();
    while start.elapsed() < grace {
        if processes.iter().all(|p| !p.is_running()) {
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(100));
    }

    // Force kill any remaining
    for proc in processes {
        if proc.is_running() {
            if let Err(e) = kill(proc) {
                printer.warning(&format!(
                    "Failed to kill {} [PID {}]: {}",
                    proc.name, proc.pid, e
                ));
                failed.push(proc.pid);
            }
        }
    }
    failed
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::ProcError;

    /// This test's own process: it exists and is the process it says it is, so
    /// `stop_all` sees it as running. The signals below are stand-ins that
    /// send nothing.
    fn this_process() -> Process {
        Process::find_by_pid(std::process::id())
            .unwrap()
            .expect("this process exists")
    }

    fn refuse(_: &Process) -> Result<()> {
        Err(ProcError::SignalError("not allowed".to_string()))
    }

    #[test]
    fn signals_that_fail_are_reported_not_fatal() {
        let me = this_process();
        let printer = Printer::from_flags(true, false);
        let failed = stop_all(
            std::slice::from_ref(&me),
            std::time::Duration::ZERO,
            &printer,
            refuse,
            refuse,
        );
        // It still counts as running, so the SIGKILL is tried after the polite
        // request; on Windows that request being refused is expected
        let refusals = if cfg!(unix) { 2 } else { 1 };
        assert_eq!(failed, vec![me.pid; refusals]);
    }

    #[test]
    fn a_refusal_for_a_process_that_is_gone_is_a_warning_and_nothing_is_waited_for() {
        let mut child = crate::test_support::idle_child();
        let gone = crate::test_support::gone_record(&mut child);
        let printer = Printer::from_flags(true, false);
        let start = std::time::Instant::now();
        // Not running, so the refusal is not the one Windows gives a console
        // program that is still there: it is reported, on every OS. The kill
        // is never tried, and the grace period ends at once.
        let failed = stop_all(
            std::slice::from_ref(&gone),
            std::time::Duration::from_secs(30),
            &printer,
            refuse,
            // were the kill tried, it would fail too and be listed twice
            refuse,
        );
        assert_eq!(failed, vec![gone.pid]);
        assert!(start.elapsed() < std::time::Duration::from_secs(10));
    }

    #[test]
    fn signals_that_work_report_nothing() {
        let me = this_process();
        let printer = Printer::from_flags(true, false);
        let failed = stop_all(
            std::slice::from_ref(&me),
            std::time::Duration::ZERO,
            &printer,
            |_| Ok(()),
            |_| Ok(()),
        );
        assert!(failed.is_empty());
    }
}