Skip to content

Allow only one Dev Proxy instance to be the system proxy - #2204

Merged
waldekmastykarz merged 2 commits into
dotnet:mainfrom
waldekmastykarz:single-system-proxy-owner
Dec 3, 2026
Merged

waldekmastykarz merged 2 commits into
dotnet:mainfrom
waldekmastykarz:single-system-proxy-owner

Conversation

@waldekmastykarz

@waldekmastykarz waldekmastykarz commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 76872f93c554dd4eb086079a1dc93e3ba9a69ff8

Dimension Assessment Rationale
Change confidence 8/10 ↑ Build (warnings as errors) and all test suites pass locally.
↑ Tests cover the startup guard decision (other live owner blocks; reload, not-system-proxy, Linux, crashed owner do not) and the lookup ignoring the current process.
↓ No end-to-end run with two real system-proxy instances; that would change the shared OS proxy settings and state folder on the test machine.
↓ CI and review pending.
Diagnosis confidence 9.5/10 ↑ Only StartDetachedProcessAsync checked for an existing owner; InvokeAsync (foreground, daemon, and each hot-reload iteration) went straight to registering.
Evidence fit 9/10 ↑ Matches the repro in the issue: a second foreground system-proxy instance registered instead of failing.

Closes #2203

What

Only one Dev Proxy instance can be the system proxy at a time. Before, only detached starts enforced this. Now foreground starts and hot-reload re-registration fail too, with the same error:

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

How

  • DevProxyCommand.InvokeAsync checks for another live owner after orphan recovery and after options are applied, before binding. It runs for foreground starts, detached daemons, and every hot-reload iteration, so a reload can't take the system proxy back from an instance that registered while it was restarting. The error goes through the logger, so --output json produces the same JSON entry shape as the detached path.
  • DevProxyCommand.FindBlockingSystemProxyOwnerAsync decides whether startup is blocked. It uses the snapshot owner (foreground or detached), falling back to running instance records, and ignores the current process so a reload doesn't block on itself.
  • The detached launcher now uses the same snapshot-based lookup and shares the error message.
  • Crashed-instance leftovers still go through orphan recovery: a snapshot whose owner is dead doesn't block.

Notes

  • The check applies only on Windows and macOS, where Dev Proxy registers the system proxy. On Linux nothing is registered, so foreground starts there are unchanged. The detached launcher keeps its existing behavior on all platforms.
  • Takeover handling in SystemProxyManager.SaveSnapshot/Disable is kept: it's still reachable through start races and stale snapshots, and Keep system proxy registered across hot-reload restarts #2149 changes reload handling in the same area.

Foreground starts and hot-reload re-registration now fail with the same
error as detached starts when another live process owns the system proxy.
The owner is taken from the system proxy snapshot, so foreground owners
count too. Closes dotnet#2203

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@waldekmastykarz
waldekmastykarz requested a review from a team as a code owner December 2, 2026 15:09
Copilot AI balanced review requested due to automatic review settings December 2, 2026 15:09

Copilot AI left a comment

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.

🟡 Changes recommended

The tests do not cover the startup guard, leaving the live-owner regression unprotected; isolated startup tests must verify blocking and non-blocking cases without changing native proxy settings.

1 open issue
What changed in this PR

Extends single-system-proxy ownership checks to foreground starts and hot reloads, addressing #2203.

Changes:

  • Rejects startup when another live process owns the system proxy.
  • Shares snapshot-based owner lookup with detached startup, allowing self-ownership during reload.
  • Adds owner-lookup tests with and without snapshots.
File Description
DevProxy/​Proxy/​SystemProxyManager.cs Adds self-excluding owner lookup and shared error text.
DevProxy/​Program.cs Uses snapshot-based ownership checks for detached startup.
DevProxy/​Commands/​DevProxyCommand.cs Checks ownership before binding on Windows and macOS.
DevProxy.Tests/​SystemProxyManagerTests.cs Tests lookup behavior when ignoring a specified process.

Review effort: Balanced

Comment thread DevProxy/Commands/DevProxyCommand.cs Resolved
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings December 2, 2026 15:16

Copilot AI left a comment

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.

🟢 Approval recommended

No active review issues were identified for the system-proxy ownership checks added to startup and hot reload.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@garrytrinder garrytrinder left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Functional test results

  • PR: dotnet/dev-proxy#2204
  • Tested commit: 76872f93c554dd4eb086079a1dc93e3ba9a69ff8
  • Environment: macOS 27.2.1, arm64, private .NET SDK 11.0.100, application and plugins built from the PR checkout.

The tests used RetryAfterPlugin and a local HTTP server.
Successful proxy requests returned HTTP 200 with the body pr2204-local-response\n.
The tests also checked the authenticated application programming interface (API) at /proxy.
API tokens did not enter the report or console output.

Scenario or test command Expected Actual Result
Build application and plugins Both builds succeed Both builds returned 0, with zero warnings and errors PASS
Automated checks for the tested commit Successful build, smoke, and automated tests GitHub checks passed, including macOS, Windows, and Linux tests; not repeated locally PASS
Start two detached instances with system registration disabled Separate proxy and API ports; both instances serve requests PIDs 68013 and 68019; proxy ports 51904 and 51913; both APIs returned 200; both proxy responses matched PASS
stop --pid 68013 Stop only the selected instance PID 68013 stopped; its state file disappeared; PID 68019 continued to serve API and proxy requests PASS
Change the remaining instance's configuration Reload without losing service PID 68019 remained; proxy port changed from 51913 to 51938; API and proxy requests succeeded PASS
Simulate a live owner with a private snapshot; start detached in text and JSON modes Launcher rejects the start with exit code 1 and the owner's PID Both modes returned 1; stdout remained empty; error named PID 68019; JSON contained one error entry; no child started PASS
Start foreground without system registration while the synthetic snapshot exists Synthetic ownership does not block this foreground start PID 68055 started; API returned 200; proxy response matched PASS
Stop foreground and detached instances by PID Both processes stop; state files disappear Both stop commands returned 0; processes, state files, and listeners disappeared PASS
Start with no configured plugins Startup fails with exit code 1; no instance remains Exit 1; diagnostic included No plugins configured or enabled.; no state file remained PASS
Start again after the failed start Valid configuration starts normally PID 68118 started; API returned 200; proxy response matched; PID-specific stop succeeded PASS
Start a real native owner; reject another foreground or detached owner Existing native proxy settings remain unchanged; second owner fails No disposable macOS or Windows environment was available BLOCKED
Reload a native owner, including a competing owner during restart Correct owner retains registration; restart cannot take registration from another owner Requires the same unavailable disposable environment BLOCKED
Stop, crash, recover, and restart a native owner Restore native settings and recover stale ownership without blocking valid starts Requires the same unavailable disposable environment and a separate certificate trust store BLOCKED

The synthetic snapshot contained an owned live PID and its recorded process start time.
The tests did not register a native system proxy.
The detached simulation retained --as-system-proxy false --install-cert false.
Its rejection shows the launcher's existing unconditional ownership check, not successful native registration.
The foreground simulation checks the disabled-registration path only.
These simulations do not prove the changed foreground guard or native recovery.

Build commands:

dotnet build DevProxy/DevProxy.csproj --no-restore --nologo -v quiet
dotnet build DevProxy.Plugins/DevProxy.Plugins.csproj --no-restore --nologo -v quiet

The initial build failed with NETSDK1004 because package assets were absent.
Locked restores from Microsoft's public dotnet-public feed supplied the missing packages.
The final builds succeeded without source or lock-file changes.
The test runner initially made incorrect assertions about escaped JSON and the configuration diagnostic.
It also failed to reap its foreground child while the stop command waited.
After those runner corrections, the complete runtime matrix passed.
Each earlier attempt also completed process and listener cleanup.

Startup exceptions and cleanup

| Scenario | Changed default | Why the default prevents reproduction | Execution environment | Safety controls | Cleanup verification |
|---|---|---|---|---|---|---|
| Detached runtime cases | None | Not applicable | macOS; .devproxy/pr2204; private SDK and home directory | --detach --as-system-proxy false --install-cert false --port 0 --api-port 0; command deadlines; PID and start-time records | All owned processes and listeners stopped; private files and SDK removed |
| Foreground simulation | Omitted --detach | Detached mode cannot test the foreground path | Same private environment | Separate stdout and stderr; 20-second readiness limit; PID-specific stop | Foreground process exited 0; state and listeners disappeared |
| Native ownership cases | Required changes not executed | Disabled registration cannot prove native ownership or certificate trust | Disposable environment unavailable | The repository's Testing Code Changes instructions and the test-pr skill prohibit host-native tests without isolation | No native test executed; no native baseline restoration was necessary |

The environment set HOME, CFFIXED_USER_HOME, and DOTNET_CLI_HOME to an owned private home directory.
The runner created Library/Application Support before .NET started.
DEV_PROXY_CERT_PATH selected an absolute private certificate path.
Observed state, log, credential, and certificate files remained under the private test directory.
CI=true suppressed token output.
The runner removed each synthetic snapshot before shutdown.

The SDK first-run output reported an ASP.NET development certificate installation.
A subsequent read-only dotnet dev-certs https --check --verbose returned 6.
It reported A default keychain could not be found and No valid certificate found in the private environment.
No certificate trust command ran.
The tests did not change host proxy settings.
The application used --install-cert false throughout.

Cleanup checks found no remaining owned processes, listeners, or instance state files.
The private SDK's build servers also stopped.
The test directory, private certificates, credentials, logs, packages, and SDK were removed.
The tracked checkout remained unchanged.

Requested changes

None. The tests did not confirm a functional defect attributable to this PR.

Outcome

Comment only.
Safe runtime tests and synthetic lookup checks passed on the tested commit.
Essential native ownership and recovery tests remain BLOCKED.
These results do not justify approval.
Cleanup completed.

The ASD-STE100 dictionary was unavailable from the public reference page.
The STE check is incomplete; this report does not claim verified conformance.

@waldekmastykarz
waldekmastykarz merged commit 5975ef0 into dotnet:main Dec 3, 2026
8 checks passed
@waldekmastykarz
waldekmastykarz deleted the single-system-proxy-owner branch December 3, 2026 13:41
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.

[BUG]: Only one Dev Proxy instance should be the system proxy, but foreground starts and reloads can take it over

3 participants