Skip to content

Add port-to-PID fallback chain with ss and lsof support#90

Open
EhabY wants to merge 2 commits intoyibn2008:masterfrom
EhabY:feat/port-to-pid-fallback
Open

Add port-to-PID fallback chain with ss and lsof support#90
EhabY wants to merge 2 commits intoyibn2008:masterfrom
EhabY:feat/port-to-pid-fallback

Conversation

@EhabY
Copy link

@EhabY EhabY commented Mar 11, 2026

Summary

  • Add ss and lsof as fallback tools for port-to-PID resolution
  • Linux: ss -tunlpnetstat -tunlplsof -nP -i :<port>
  • macOS/FreeBSD/SunOS: netstat -anvlsof -nP -i :<port>
  • Windows and Android: unchanged
  • Fix existing bug where netstat on Linux resolves with NaN when PID field is - (non-root)
  • Fix existing bug where netstat stderr on macOS rejects instead of allowing fallback
  • Extract execCmd and matchPort helpers to reduce boilerplate across all platform finders

The first commit introduces the fallback logic + tests while the second commit is just refactoring some code

Motivation

netstat is deprecated on many Linux distros in favor of ss, and running without root often produces empty PID fields. This change adds a try-and-fallback chain so port lookup works in more environments without requiring any specific tool to be installed.

if (err) {
reject(err)
} else {
resolve({ stdout: stdout.toString(), stderr: stderr.toString().trim() })
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only trimming stderr because lines are sometimes skipped for stdout

@EhabY EhabY force-pushed the feat/port-to-pid-fallback branch from 3ac9143 to 7efcc41 Compare March 11, 2026 15:42
EhabY added 2 commits March 12, 2026 13:32
Linux now tries ss → netstat → lsof; macOS/FreeBSD/SunOS tries
netstat → lsof. Fixes lookup on distros without netstat and on
non-root users where PID fields are empty.
Extract shared execCmd (promise wrapper for utils.exec) and matchPort
(port-matching predicate) helpers to reduce boilerplate across all
platform finders.
@EhabY EhabY force-pushed the feat/port-to-pid-fallback branch from 7efcc41 to a261c30 Compare March 12, 2026 10:33
@EhabY
Copy link
Author

EhabY commented Mar 12, 2026

@yibn2008 Hey! I've created this PR with some mock testing for the fallback logic. Would you mind taking a look? The first commit introduces the fallout logic but the second commit does some refactoring (we repeat a lot of the logic so I just created two functions to make it easier to read). Let me know if I should change anything here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant