Skip to content

feat: add --strict-ports option for predictable port assignment#21320

Merged
mickqian merged 3 commits intosgl-project:mainfrom
yang1002378395-cmyk:feat-strict-ports
Mar 27, 2026
Merged

feat: add --strict-ports option for predictable port assignment#21320
mickqian merged 3 commits intosgl-project:mainfrom
yang1002378395-cmyk:feat-strict-ports

Conversation

@yang1002378395-cmyk
Copy link
Copy Markdown
Contributor

Summary

  • Add --strict-ports option to disable automatic port selection
  • When enabled, server fails immediately if requested ports are unavailable
  • Ensures predictable port assignment for users who need to know exactly which ports will be used

Problem

Currently, when a requested port is occupied, settle_port automatically selects an available port. This causes issues for users who:

  1. Don't know which port the server ended up using
  2. Cannot reliably create clients with the same configuration
  3. Have port conflicts across multiple server instances

Solution

Add --strict-ports option. When enabled:

  • Check if requested ports are available (HTTP, scheduler, master)
  • Fail with clear error message if any port is unavailable
  • Do NOT auto-select alternative ports

Usage

# Normal mode (default): auto-select ports if unavailable
python -m sglang.launch_server --model <model> --port 8080

# Strict mode: fail if port 8080 is unavailable
python -m sglang.launch_server --model <model> --port 8080 --strict-ports

Related to #21284

Test Plan

  • Start server with occupied port + --strict-ports → should fail with clear error
  • Start server with available port + --strict-ports → should succeed
  • Default behavior (no --strict-ports) → should auto-select ports as before

…ound

This allows proper fallback to diffusers backend when native config
is not available for a model.

Fixes sgl-project#21311
When --strict-ports is enabled, the server will fail immediately if
the requested ports are unavailable instead of automatically selecting
alternative ports. This ensures predictable port assignment for
users who need to know exactly which ports will be used.

Related to sgl-project#21284
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant enhancement to the server's port management by adding a --strict-ports option. This feature addresses the problem of unpredictable port assignments that occurred when requested ports were already in use, which previously led to automatic port selection. With this change, users can now enforce exact port configurations, as the server will explicitly check for port availability and terminate with an error if any specified port is occupied, thereby ensuring reliable and consistent server deployments.

Highlights

  • New --strict-ports option: Added a new command-line option --strict-ports to control server port assignment behavior.
  • Predictable Port Assignment: When --strict-ports is enabled, the server will fail immediately if any requested port (HTTP, scheduler, master) is unavailable, ensuring predictable port assignments and preventing automatic port selection.
  • Improved Error Handling: Introduced clear error messages when ports are unavailable in strict mode, guiding users on how to resolve port conflicts.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new strict_ports option, which, when enabled, causes the server to fail if requested ports are unavailable instead of auto-selecting them. It also modifies the model registry to log a debug message and return None instead of raising a RuntimeError when model information is not found. The review suggests that changing the RuntimeError to a debug log in the registry might hide critical configuration issues and recommends reverting to an exception. Additionally, it recommends making the error messages for scheduler_port and master_port more consistent and helpful when strict_ports is enabled.

Comment on lines +324 to +325
logger.debug(f"No model info found for model path: {model_path}")
return None
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Changing RuntimeError to logger.debug and returning None can hide potential configuration issues. If model info is not found for a given model_path, it's likely an error that should be surfaced to the user, rather than being silently ignored at the debug log level. This change could lead to hard-to-debug NoneType errors later in the execution flow. It's better to fail fast. Please consider reverting to raising an exception.

Suggested change
logger.debug(f"No model info found for model path: {model_path}")
return None
raise RuntimeError(f"No model info found for model path: {model_path}")

Comment on lines +388 to +395
if not is_port_available(self.scheduler_port):
raise RuntimeError(
f"Scheduler port {self.scheduler_port} is unavailable and --strict-ports is enabled."
)
if self.master_port is not None and not is_port_available(self.master_port):
raise RuntimeError(
f"Master port {self.master_port} is unavailable and --strict-ports is enabled."
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The error messages for unavailable scheduler_port and master_port are not as helpful as the one for the main port. For consistency and better user experience, consider including the suggestion to 'Either use a different port or remove --strict-ports to allow auto-selection' in all error messages when strict_ports is enabled.

Suggested change
if not is_port_available(self.scheduler_port):
raise RuntimeError(
f"Scheduler port {self.scheduler_port} is unavailable and --strict-ports is enabled."
)
if self.master_port is not None and not is_port_available(self.master_port):
raise RuntimeError(
f"Master port {self.master_port} is unavailable and --strict-ports is enabled."
)
if not is_port_available(self.scheduler_port):
raise RuntimeError(
f"Scheduler port {self.scheduler_port} is unavailable and --strict-ports is enabled. "
f"Either use a different port or remove --strict-ports to allow auto-selection."
)
if self.master_port is not None and not is_port_available(self.master_port):
raise RuntimeError(
f"Master port {self.master_port} is unavailable and --strict-ports is enabled. "
f"Either use a different port or remove --strict-ports to allow auto-selection."
)

- Reverted registry.py to raise RuntimeError instead of returning None
  to avoid hiding critical configuration issues
- Made strict_ports error messages consistent across all ports
- Added helpful hints for resolving port conflicts
@mickqian
Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@yhyang201
Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@mickqian mickqian merged commit f83b1b7 into sgl-project:main Mar 27, 2026
104 of 118 checks passed
satyamk7054 pushed a commit to satyamk7054/sglang that referenced this pull request Apr 3, 2026
…gnment (sgl-project#21320)

Co-authored-by: 阳虎 <yanghu@yanghudeMacBook-Pro.local>
JustinTong0323 pushed a commit to JustinTong0323/sglang that referenced this pull request Apr 7, 2026
…gnment (sgl-project#21320)

Co-authored-by: 阳虎 <yanghu@yanghudeMacBook-Pro.local>
yhyang201 pushed a commit to yhyang201/sglang that referenced this pull request Apr 22, 2026
…gnment (sgl-project#21320)

Co-authored-by: 阳虎 <yanghu@yanghudeMacBook-Pro.local>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

diffusion SGLang Diffusion run-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants