Skip to content

Allow multiple Dev Proxy instances when asSystemProxy is false#1589

Open
Copilot wants to merge 8 commits intomainfrom
copilot/fix-multiple-instances-issue
Open

Allow multiple Dev Proxy instances when asSystemProxy is false#1589
Copilot wants to merge 8 commits intomainfrom
copilot/fix-multiple-instances-issue

Conversation

Copy link
Contributor

Copilot AI commented Mar 5, 2026

The detached mode single-instance check unconditionally blocks new instances regardless of asSystemProxy. When asSystemProxy is false, instances don't conflict over system proxy registration and should be allowed to coexist.

Changes

  • ProxyInstanceState — Track asSystemProxy in the state file (defaults to true for backward compat with existing state files)
  • DevProxyConfigOptions — Register --as-system-proxy for early CLI arg parsing before full config load
  • StateManager — Switch from a single shared state.json to per-instance state files (state-{pid}.json) to avoid cross-instance interference. Each instance writes/deletes only its own file. LoadStateAsync() prefers the system-proxy instance, then falls back to the most recently started one. FindSystemProxyInstanceAsync() scans all state files to reliably detect a running system-proxy instance. Legacy state.json is still read for backward compatibility.
  • Program.cs — Use FindSystemProxyInstanceAsync() for the system-proxy conflict check; use LoadStateByPidAsync(process.Id) in the detached startup wait loop to find the specific child process; resolve asSystemProxy from CLI args → config file fallback and persist to state
  • StopCommand — Pass the specific state.Pid to DeleteStateAsync to avoid accidentally removing another instance's state file
// Before: single state.json, blocks unconditionally
if (await StateManager.IsInstanceRunningAsync()) { ... }

// After: per-instance state-{pid}.json, blocks only when a system-proxy instance exists
var systemProxyInstance = await StateManager.FindSystemProxyInstanceAsync();
if (systemProxyInstance is not null) { ... }
Original prompt

This section details on the original issue you should resolve

<issue_title>When asSystemProxy is false allow running multiple instances</issue_title>
<issue_description>When asSystemProxy is false allow running multiple instances of Dev Proxy. Right now, you get the following error:

Dev Proxy is already running (PID: 1234).
Use 'devproxy stop' to stop it first.

This error stands but only if Dev Proxy was started with asSystemProxy: true. If it's false, then it should be possible to start multiple instances of Dev Proxy.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

When asSystemProxy is false, the single-instance check is skipped
so multiple Dev Proxy instances can run simultaneously.

The AsSystemProxy value is now stored in the state file so the
detached mode launch check can determine if the existing instance
is acting as the system proxy.

Closes #1586

Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix issue with running multiple instances of Dev Proxy Allow multiple Dev Proxy instances when asSystemProxy is false Mar 5, 2026
@waldekmastykarz waldekmastykarz marked this pull request as ready for review March 5, 2026 14:40
@waldekmastykarz waldekmastykarz requested a review from a team as a code owner March 5, 2026 14:40
Copilot AI review requested due to automatic review settings March 5, 2026 14:40
Copy link
Contributor

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

Updates detached-mode instance detection so Dev Proxy only blocks starting a new detached instance when an existing detached instance owns the system proxy, enabling coexistence when asSystemProxy is false.

Changes:

  • Add asSystemProxy to the detached instance state file (defaulting to true for backward compatibility).
  • Parse --as-system-proxy early via DevProxyConfigOptions so it can be persisted into detached state.
  • Change detached startup logic to gate the “already running” check on existingState.AsSystemProxy.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
DevProxy/State/ProxyInstanceState.cs Adds AsSystemProxy to persisted detached state (back-compat default true).
DevProxy/Program.cs Gates detached single-instance check on existingState.AsSystemProxy and persists asSystemProxy into state.
DevProxy/Commands/DevProxyConfigOptions.cs Adds early parsing access for --as-system-proxy before full config load.

You can also share your feedback on Copilot code review. Take the survey.

@waldekmastykarz
Copy link
Collaborator

@copilot apply changes based on the comments in this thread

Copilot AI and others added 2 commits March 5, 2026 15:08
Each detached instance now stores its state in a separate file keyed
by PID (e.g. state-1234.json) instead of a single shared state.json.

This ensures that:
- Multiple non-system-proxy instances don't overwrite each other's state
- Deleting state on exit only removes the current instance's file
- devproxy stop/status/logs still work by preferring the system-proxy
  instance, then falling back to the most recently started one
- Legacy state.json files are still read for backward compatibility

StopCommand now passes the specific PID when deleting state to avoid
accidentally removing another instance's state file.

Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 5, 2026

@copilot apply changes based on the comments in this thread

Applied the review feedback in 0170b3d. StateManager now uses per-instance state files (state-{pid}.json) instead of a single shared state.json:

  • Each instance writes/deletes only its own state-{pid}.json — no cross-instance interference
  • LoadStateAsync() scans all state files and prefers the system-proxy instance
  • FindSystemProxyInstanceAsync() is used for the startup conflict check
  • LoadStateByPidAsync(pid) is used in the detached startup wait loop to find the specific child process
  • StopCommand passes state.Pid to DeleteStateAsync so it only removes the targeted instance's file
  • Legacy state.json is still read for backward compatibility with pre-upgrade instances

@waldekmastykarz waldekmastykarz marked this pull request as draft March 6, 2026 12:32
@waldekmastykarz waldekmastykarz marked this pull request as ready for review March 6, 2026 12:52
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.

When asSystemProxy is false allow running multiple instances

3 participants