Skip to content

feat: auto-increment port when requested port is in use#64

Merged
jfernandez merged 2 commits intojfernandez:mainfrom
jzhuge:auto-increment-port
Mar 2, 2026
Merged

feat: auto-increment port when requested port is in use#64
jfernandez merged 2 commits intojfernandez:mainfrom
jzhuge:auto-increment-port

Conversation

@jzhuge
Copy link
Copy Markdown
Contributor

@jzhuge jzhuge commented Feb 12, 2026

Summary

  • Enables running multiple mdserve instances concurrently
  • Automatically tries the next available port (up to 10 retries) when requested port is in use
  • Prints warning notice when falling back to different port
  • Adds test coverage for port retry logic

Test plan

  • All existing tests pass
  • New test test_bind_retries_on_addr_in_use validates retry behavior
  • Manual testing: Start two instances, second auto-binds to next port

Closes #63

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds retry logic when binding the TCP listener so multiple mdserve instances can run concurrently by auto-incrementing the port when the requested port is already in use.

Changes:

  • Introduces bind_with_retry() to attempt binding across a port range when AddrInUse occurs.
  • Updates serve_markdown() to use the actual bound port for display and --open behavior, and prints a warning when falling back.
  • Adds a Tokio test validating the retry behavior when the initial port is occupied.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/app.rs Outdated
@jfernandez
Copy link
Copy Markdown
Owner

@jzhuge, thanks for the feature! would you mind taking a look at CI and fix the failures?

@jzhuge
Copy link
Copy Markdown
Contributor Author

jzhuge commented Feb 17, 2026

@jzhuge, thanks for the feature! would you mind taking a look at CI and fix the failures?

Fixed

@jfernandez
Copy link
Copy Markdown
Owner

@jzhuge a few more asks:

  1. Please squash the commits
  2. I'd prefer we remove the Claude Code attribution in the commit message
  3. See Copilots suggestion on the variable naming or changing the loop to be inclusive.

@jzhuge jzhuge force-pushed the auto-increment-port branch 6 times, most recently from 62222c7 to ed07952 Compare February 20, 2026 06:42
Enables running multiple mdserve instances concurrently by automatically
trying the next available port when the requested port is already bound.

- Add MAX_PORT_ATTEMPTS constant (default 10)
- Add bind_with_retry helper that attempts ports sequentially
- Update serve_markdown to use bind_with_retry and print notice
- Add test_bind_retries_on_addr_in_use test

Closes jfernandez#63
@jzhuge jzhuge force-pushed the auto-increment-port branch from ed07952 to 3e04831 Compare February 20, 2026 06:44
@jfernandez jfernandez merged commit c39fc20 into jfernandez:main Mar 2, 2026
7 checks passed
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.

Auto-increment port number if port is in use

3 participants