Skip to content

feat(install): add port conflict detection before starting services#15

Open
logicalangel wants to merge 1 commit intoPasarGuard:mainfrom
logicalangel:feat/port-conflict-detection
Open

feat(install): add port conflict detection before starting services#15
logicalangel wants to merge 1 commit intoPasarGuard:mainfrom
logicalangel:feat/port-conflict-detection

Conversation

@logicalangel
Copy link
Copy Markdown

@logicalangel logicalangel commented Apr 10, 2026

Summary

Adds automatic port conflict detection during installation to prevent the infinite Docker restart loop described in #10.

Problem

When another service already occupies port 8000 (the default UVICORN_PORT), docker compose up starts PasarGuard with network_mode: host, which causes:

[Errno 98] error while attempting to bind on address ('0.0.0.0', 8000): address already in use

This leads to an infinite restart loop since there is no pre-flight port check.

Solution

After the .env file is written and before up_pasarguard() starts the containers, the script now:

  1. Reads the configured UVICORN_PORT from .env (defaults to 8000)
  2. Checks if that port is already in use via the existing is_port_in_use() utility
  3. If a conflict is detected, warns the user and prompts for an alternative port
  4. Validates the new port (1-65535 range, not already in use)
  5. Updates both UVICORN_PORT and ALLOWED_ORIGINS in .env

Changes

  • pasarguard.sh: Added ~39 lines of port conflict detection logic in install_command(), placed between install_completion and up_pasarguard calls.

Testing

  • Verified the detection logic uses the existing is_port_in_use() function (ss → netstat → lsof fallback chain)
  • The prompt loop validates port range and re-checks availability before accepting

Closes #10

Summary by CodeRabbit

  • New Features
    • The installation process now intelligently detects if the configured port is already in use and interactively prompts you to select an available alternative. The system validates your port selection and automatically updates the configuration with the new port.

Check if UVICORN_PORT (default 8000) is already in use before starting
Docker containers. If a conflict is detected, prompt the user for an
alternative port and update .env accordingly (both UVICORN_PORT and
ALLOWED_ORIGINS).

This prevents the infinite restart loop that occurs when another service
occupies the configured port, since PasarGuard uses network_mode: host.

Closes PasarGuard#10
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 10, 2026

Walkthrough

The installation script now detects port conflicts before deployment. During setup, it checks if the configured UVICORN_PORT is in use, prompts the user to select an alternative if occupied, and updates the environment file with the new port selection.

Changes

Cohort / File(s) Summary
Port Conflict Detection
pasarguard.sh
Added port availability check during install_command() that validates the configured port, interactively prompts for an alternative if occupied, validates user input (integer 1-65535 range), and updates $ENV_FILE with the selected port, including replacing old port references in ALLOWED_ORIGINS.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A script so wise, now checks the door,
Before the app tries port once more,
If 8000's taken, fear not, my friend,
Pick another—our conflicts end! 🔌

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: adding port conflict detection before services start during installation.
Linked Issues check ✅ Passed All requirements from issue #10 are met: port availability checking, user prompting for alternatives, port validation (1-65535), .env file updates, and ALLOWED_ORIGINS updates.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing port conflict detection as specified in issue #10; no unrelated modifications detected.

✏️ 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.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
pasarguard.sh (1)

3746-3785: Port conflict detection logic looks solid and addresses the issue requirements.

The implementation correctly:

  • Reads UVICORN_PORT from .env with a sensible default of 8000
  • Uses the existing is_port_in_use() utility for detection
  • Validates port range and availability in a prompt loop
  • Updates both UVICORN_PORT and ALLOWED_ORIGINS in the .env file

One minor improvement per the static analysis hint:

🔧 Add `-r` flag to `read` command

The read command on line 3760 should include the -r flag to prevent backslash interpretation. While users are unlikely to enter backslashes in port numbers, it's a shell scripting best practice.

 while true; do
-    read -p "Enter a different port for PasarGuard (1-65535) or 'q' to abort: " new_port
+    read -rp "Enter a different port for PasarGuard (1-65535) or 'q' to abort: " new_port
     if [[ "$new_port" == "q" || "$new_port" == "Q" ]]; then
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pasarguard.sh` around lines 3746 - 3785, The read invocation inside the port
prompt loop should use the -r flag to prevent backslash interpretation; update
the read command that assigns to new_port (inside the while true loop that
validates ports) to use read -r so user input is read raw; no other logic
changes needed—this affects the read that prompts "Enter a different port for
PasarGuard..." in the block that checks is_port_in_use and later calls
set_or_uncomment_env_var and up_pasarguard.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@pasarguard.sh`:
- Around line 3746-3785: The read invocation inside the port prompt loop should
use the -r flag to prevent backslash interpretation; update the read command
that assigns to new_port (inside the while true loop that validates ports) to
use read -r so user input is read raw; no other logic changes needed—this
affects the read that prompts "Enter a different port for PasarGuard..." in the
block that checks is_port_in_use and later calls set_or_uncomment_env_var and
up_pasarguard.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a827e407-bb3c-4261-89d5-fc325c123701

📥 Commits

Reviewing files that changed from the base of the PR and between 8fb1929 and 86cbfe2.

📒 Files selected for processing (1)
  • pasarguard.sh

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.

Add port conflict detection during installation

1 participant