Skip to content

fix(start_proxy): fail fast on bind error instead of false "Started" - #24

Open
felipetruman wants to merge 1 commit into
snapspecter:mainfrom
felipetruman:fix/start-proxy-bind-precheck
Open

fix(start_proxy): fail fast on bind error instead of false "Started"#24
felipetruman wants to merge 1 commit into
snapspecter:mainfrom
felipetruman:fix/start-proxy-bind-precheck

Conversation

@felipetruman

Copy link
Copy Markdown

Problem

MitmController.start() schedules master.run() via asyncio.create_task and immediately returns "Started proxy on port N" while setting running = True — without waiting for the bind to succeed.

When the port is already in use, mitmproxy's master.run() dies asynchronously with SystemExit(1) (address already in use). The result is a false state:

  • start_proxy reports success to the caller
  • running == True but no proxy is actually listening
  • an unretrieved task exception pollutes the event loop (Task exception was never retrieved)

Fix

Add a synchronous probe bind() before launching the master. If the port is taken, return a clear error and leave running=False. Deterministic — avoids trying to catch mitmproxy's async SystemExit.

Verification

  • Free port → "Started proxy on port N", proxy listens, stop() works (unchanged).
  • Busy port → "Couldn't start the proxy on 127.0.0.1:8097: [Errno 98] Address already in use", running=False, no orphan task exception.

🤖 Generated with Claude Code

start() scheduled master.run() via create_task and immediately returned
"Started proxy on port N" + set running=True, without waiting for the bind.
When the port was already in use, mitmproxy's master.run() died
asynchronously with SystemExit(1) ("address already in use"), leaving the
controller in a false state: running=True but no proxy listening, plus an
unretrieved task exception polluting the event loop.

Add a synchronous probe bind before starting the master. If the port is
taken, return a clear error and leave running=False. Deterministic and
avoids chasing mitmproxy's async SystemExit.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.

2 participants