Skip to content

perf(desktop): increase port scan interval and skip scan when no sessions - #3238

Closed
felixbarny wants to merge 1 commit into
ParadevAI:mainfrom
felixbarny:fix/desktop-port-scan-interval
Closed

felixbarny wants to merge 1 commit into
ParadevAI:mainfrom
felixbarny:fix/desktop-port-scan-interval

Conversation

@felixbarny

@felixbarny felixbarny commented Apr 7, 2026 •

Copy link
Copy Markdown

Summary

  • Increase the periodic port scan interval from 2,500ms to 10,000ms. The hint-based scan already fires within 500ms of detecting "listening on port X" in terminal output, so the periodic sweep is just a safety net and a longer interval is fine.
  • Skip the scan entirely when no sessions are registered, avoiding unnecessary ps/lsof spawns on startup or after all terminals are closed.

Each scan spawns ps (via pidtree) per session plus one lsof call. These process spawns are intercepted by kernel-level security agents (e.g. Elastic Endpoint), which inspect every file those processes open. Reducing scan frequency directly reduces EDR CPU load.

Part of #3235


Summary by cubic

Increase the desktop port scan interval to 10s and skip scans when no sessions exist. The hint-based scan still triggers within 500ms; this reduces background ps/lsof spawns and CPU load with security agents, addressing Linear #3235.

Written for commit a4431b2. Summary will update on new commits.

Summary by CodeRabbit

  • Chores
    • Optimized port scanning behavior to improve efficiency and reduce system resource usage.

…ions

Increase the periodic port scan interval from 2,500ms to 10,000ms. The
hint-based scan already fires within 500ms of detecting "listening on port X"
in terminal output, so the periodic sweep is just a safety net and a longer
interval is fine.

Also skip the scan entirely when no sessions are registered to avoid
spawning ps/lsof unnecessarily on startup or after all terminals are closed.

Closes ParadevAI#3235
@coderabbitai

coderabbitai Bot commented Apr 7, 2026 •

Copy link
Copy Markdown
Contributor

Caution

Review failed

Pull request was closed or merged during review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 00356f86-fb9c-4116-b10b-68f77591ff9f

📥 Commits

Reviewing files that changed from the base of the PR and between 97031ad and a4431b2.

📒 Files selected for processing (1)
  • apps/desktop/src/main/lib/terminal/port-manager.ts

📝 Walkthrough

Walkthrough

Updated port scanning in PortManager by increasing the scan interval from 2.5 to 10 seconds and adding an early-return guard to skip scanning when no sessions are registered, reducing unnecessary scanning activity and process spawning.

Changes

Cohort / File(s) Summary
Port Manager Optimization
apps/desktop/src/main/lib/terminal/port-manager.ts
Increased SCAN_INTERVAL_MS to reduce scan frequency and added early-return guard in scanAllSessions() to skip processing when session tracking sets are empty.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A port scan slows its eager pace,
From swift to steady, time and space,
When sessions rest, no needless quest,
Returns early puts performance to the test!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the two main changes: increasing the port scan interval and skipping scans when no sessions exist.
Description check ✅ Passed The description covers the key changes, rationale, and related issue, though it lacks formal sections matching the template structure (no explicit 'Type of Change' or 'Testing' sections).
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps

greptile-apps Bot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR optimizes the desktop port scanner with two targeted performance improvements that reduce EDR (Elastic Endpoint) CPU load by decreasing the frequency of ps (via pidtree) and lsof process spawns.

  • Increased SCAN_INTERVAL_MS from 2_500 to 10_000 — the hint-based scan already fires within 500ms of detecting port patterns in terminal output (scheduleHintScan), so the periodic sweep is a safety net where a 10s interval is acceptable
  • Added an early-return guard in scanAllSessions() when both sessions and daemonSessions maps are empty, skipping all process spawning at startup or after all terminals are closed
  • The empty-sessions guard is correctly positioned after the isScanning check and before the lock is acquired; unregisterSession/unregisterDaemonSession already call removePortsForPane synchronously, so no orphan ports can result from the skipped scan path

Confidence Score: 5/5

Safe to merge — focused performance optimization with no logic changes to core scanning behavior

Both changes are minimal and correct. The interval increase is a straightforward constant change. The empty-sessions guard is placed at the right point in the function: after the isScanning dedup check, before acquiring the lock, and unregistration already calls removePortsForPane synchronously so no orphan ports can result. No bugs found.

No files require special attention

Important Files Changed

Filename Overview
apps/desktop/src/main/lib/terminal/port-manager.ts Increased scan interval to 10s and added early-return guard when no sessions exist to reduce unnecessary process spawning — both changes are correct and have no side effects

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[setInterval fires every 10s] --> B{isScanning?}
    B -- Yes --> Z[Skip]
    B -- No --> C{sessions.size == 0\nAND daemonSessions.size == 0?}
    C -- Yes --> Z2[Skip - no ps/lsof spawned]
    C -- No --> D[isScanning = true]
    D --> E[collectRegularSessionPids]
    E --> F[collectDaemonSessionPids]
    F --> G[buildPortsByPane - lsof call]
    G --> H[updatePortsFromScan]
    H --> I[clearEmptyTreePanes]
    I --> J[cleanupUnregisteredPorts]
    J --> K[isScanning = false]

    L[Terminal output detected] --> M{containsPortHint?}
    M -- No --> N[Ignore]
    M -- Yes --> O[scheduleHintScan 500ms delay]
    O --> P[scanPane - targeted ps/lsof]
Loading

Reviews (1): Last reviewed commit: "perf(desktop): increase port scan interv..." | Re-trigger Greptile

@felixbarny

Copy link
Copy Markdown
Author

Superseded by #3236, which covers the same fix with a better implementation. Closing in favour of that PR.

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