Repository navigation
Description
Problem Description
When a proxy server instance fails to bind at runtime (not at startup), it stays in Servers with last_exception set. Two lifecycle defects follow in v12.3.1:
- Removing the failed spec calls
AsyncioServerInstance._stop(), which starts withassert self._servers(mitmproxy/proxy/mode_servers.py:268-269)._startfailed before assigning_servers, so this raisesAssertionError.Servers.updatecatches it and logsstr(ret), which is empty, so the user sees a blank error line. - Re-applying the same mode list is a no-op.
Servers.updatereuses the existing instance for an unchangedProxyMode, schedules no start task and returns before dispatchingchanged(mitmproxy/addons/proxyserver.py:66-78). The instance stays failed forever and the same spec is never retried.
A workaround exists: update the mode list without the failed spec, then update again with it. It relies on the broken stop path above and is not discoverable.
Steps to reproduce
Occupy a port, then drive mode at runtime:
python3 -c 'import socket,time; s=socket.socket(); s.bind(("127.0.0.1",18080)); s.listen(); time.sleep(120)' &
mitmdump --set server=false -s retry.py
import asyncio
from mitmproxy import ctx
def running():
loop = asyncio.get_running_loop()
def apply():
ctx.options.update(server=True, mode=["[email protected]:18080"])
def readd():
ctx.options.update(mode=["[email protected]:18080"])
loop.call_later(0, apply)
loop.call_later(1, readd)
loop.call_later(2, lambda: print([
(s.mode.full_spec, s.is_running, s.last_exception)
for s in ctx.master.addons.get("proxyserver").servers
]))Observed: the first apply records the [Errno 48] ... address already in use exception; the second ctx.options.update with the identical spec produces no start attempt and no changed, and the instance stays is_running=False. Removing a failed spec first prints an empty error line (the AssertionError).
Proposal
- Make
_stop()tolerate an instance that never started. - When a spec in the new mode list is present in
_instancesbut its instance haslast_exceptionset or is not running, treat it as needing a start attempt (or expose a documented retry operation). - Log the assertion/stop failure with a message rather than an empty line.
- Document the supported way to change and retry modes at runtime.
Additional context
Startup bind failures are fatal via ErrorCheck (see #5376), so a runtime apply is the way to keep a long-running process alive while the user fixes an address. A desktop UI built on mitmdump relies on this to show a failed row and retry it.
Activity