Skip to content

A failed proxy server instance cannot be stopped, and re-applying the same mode never retries it #8630

Open

Description

@Giardi77

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:

  1. Removing the failed spec calls AsyncioServerInstance._stop(), which starts with assert self._servers (mitmproxy/proxy/mode_servers.py:268-269). _start failed before assigning _servers, so this raises AssertionError. Servers.update catches it and logs str(ret), which is empty, so the user sees a blank error line.
  2. Re-applying the same mode list is a no-op. Servers.update reuses the existing instance for an unchanged ProxyMode, schedules no start task and returns before dispatching changed (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 _instances but its instance has last_exception set 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions