Skip to content

Debugpy unable to start Flask server #1296

@TopeltR

Description

@TopeltR

Environment data

  • debugpy version: 1.6.7
  • OS and version: macOS Ventura 13.2.1
  • Python version (& distribution if applicable, e.g. Anaconda): 3.10.11
  • Using VS Code or Visual Studio: VS Code
  • Flask version: 2.3.2

Actual behaviour

When using a startup command to run a Flask server with debugpy and code reloading like this:
FLASK_DEBUG=true FLASK_ENV=development python3 -m debugpy --listen 0.0.0.0:5678 -m flask run

debugpy raises a RuntimeException:

Details
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
* Restarting with watchdog (fsevents)
Traceback (most recent call last):
File "/home/topeltr/Documents/Github/example-application/venv/lib/python3.10/site-packages/debugpy/_vendored/pydevd/pydevd.py", line 3489, in <module>
  main()
File "/home/topeltr/Documents/Github/example-application/venv/lib/python3.10/site-packages/debugpy/_vendored/pydevd/pydevd.py", line 3482, in main
  globals = debugger.run(setup['file'], None, None, is_module)
File "/home/topeltr/Documents/Github/example-application/venv/lib/python3.10/site-packages/debugpy/_vendored/pydevd/pydevd.py", line 2510, in run
  return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "/home/topeltr/Documents/Github/example-application/venv/lib/python3.10/site-packages/debugpy/_vendored/pydevd/pydevd.py", line 2528, in _exec
  globals = pydevd_runpy._run_module_as_main(module_name, alter_argv=False)
File "/home/topeltr/Documents/Github/example-application/venv/lib/python3.10/site-packages/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 238, in _run_module_as_main
  return _run_code(code, main_globals, None,
File "/home/topeltr/Documents/Github/example-application/venv/lib/python3.10/site-packages/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code
  exec(code, run_globals)
File "/home/topeltr/Documents/Github/example-application/venv/lib/python3.10/site-packages/debugpy/__main__.py", line 39, in <module>
  cli.main()
File "/home/topeltr/Documents/Github/example-application/venv/lib/python3.10/site-packages/debugpy/server/cli.py", line 430, in main
  run()
File "/home/topeltr/Documents/Github/example-application/venv/lib/python3.10/site-packages/debugpy/server/cli.py", line 302, in run_module
  start_debugging(argv_0)
File "/home/topeltr/Documents/Github/example-application/venv/lib/python3.10/site-packages/debugpy/server/cli.py", line 256, in start_debugging
  debugpy.listen(options.address)
File "/home/topeltr/Documents/Github/example-application/venv/lib/python3.10/site-packages/debugpy/public_api.py", line 31, in wrapper
  return wrapped(*args, **kwargs)
File "/home/topeltr/Documents/Github/example-application/venv/lib/python3.10/site-packages/debugpy/server/api.py", line 143, in debug
  log.reraise_exception("{0}() failed:", func.__name__, level="info")
File "/home/topeltr/Documents/Github/example-application/venv/lib/python3.10/site-packages/debugpy/server/api.py", line 141, in debug
  return func(address, settrace_kwargs, **kwargs)
File "/home/topeltr/Documents/Github/example-application/venv/lib/python3.10/site-packages/debugpy/server/api.py", line 262, in listen
  raise RuntimeError(str(endpoints["error"]))
RuntimeError: Can't listen for client connections: [Errno 48] Address already in use

I looked a bit more into the problem and it seems that a change in Werkzeug causes this. sys.orig_argv passes the entire command to the reloader i.e. python3 -m debugpy --listen 0.0.0.0:5678 -m flask run - which leads to a second instance of debugpy being started without shutting down the previous one causing the traceback from debugpy side. I already raised an issue on the Werkzeug side and they signalled that it should be handled on the debugpy side.

Expected behaviour

No RuntimeError emitted and Debugpy can start a Flask server with code reloading.

Steps to reproduce:

  1. Use versions mentioned in the environments section
  2. Run minimal Flask app (example here)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions