Repository navigation
Description
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 useI 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:
- Use versions mentioned in the environments section
- Run minimal Flask app (example here)
Activity
jaredannis commentedon Jun 28, 2023
i am having same issue, in my docker compose:
command: python -m debugpy --listen 0.0.0.0:5666 -m flask run --host=0.0.0.0
gets error:
RuntimeError: Can't listen for client connections: [Errno 98] Address already in use
setting flask to --no-reload lets the app load, but i am losing functionality
i am able to get it to work if explicitly set these versions:
werkzeug==2.2.2
flask==2.2.2
debugpy==1.6.3
srh-sloan commentedon Nov 16, 2023
We are also experiencing exactly this problem. Is there any update on when it will be fixed please?
sebastienpires commentedon Dec 4, 2023
Same issue here - we start losing the ability to debug if we upgrade any Python3.10 application to Werkzeug 2.3.0.
int19h commentedon Dec 4, 2023
Note that you can disable the Flask reloader by using
--no-reloadwithout disabling other Flask debugging features.yezhengli-Mr9 commentedon Jan 1, 2024
Are you following https://code.visualstudio.com/docs/containers/docker-compose for
pythonwithdocker-compose? Not able to debug in vscode same way as pycharm in j2eeexpert2015/flask_docker#1 although feel I need to debug in vscode j2eeexpert2015/springbootdebug#1 due to the fact I need to rundocker-compose upundersudo su.For https://code.visualstudio.com/docs/containers/docker-compose and utilizing sample code https://github.com/j2eeexpert2015/flask_docker, I finish following steps:
launch.json:{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Python: Remote Attach", "type": "python", "request": "attach", "connect": { "host": "localhost", "port": 5678 }, "pathMappings": [ { "localRoot": "${workspaceFolder}", "remoteRoot": "." } ], "justMyCode": true } ] }docker-compose.yamlhttps://github.com/j2eeexpert2015/flask_docker/blob/587dc48e1a563226e5fd1ac63e82243590649f5c/docker-compose.yml#L4-L8 asdocker-compose.debug.yamland add into ``docker-compose.debug.yaml` as followingversion: "2" services: app: build: ./app ++ command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 app.py runserver 0.0.0.0:5000 --nothreading --noreload"] links: - db ports: - "5000:5000" ++ - 5678:5678 db: image: mysql:5.7 ports: - "32000:3306" environment: MYSQL_ROOT_PASSWORD: root volumes: - ./db:/docker-entrypoint-initdb.d/:roRUN pip install debugpytoDockerFilejust in case https://github.com/j2eeexpert2015/flask_docker/blob/587dc48e1a563226e5fd1ac63e82243590649f5c/app/Dockerfile#L10-L11COPY app.py /app ++RUN pip install debugpy CMD python app.pyAdd breakpoint in same as video tutorial

Right-click on the
docker-compose.debug.ymlfile and choose Compose Up:or
docker-compose upin terminal (similar to Java Debugging in Docker: How to Use VS Code and Docker Compose with sample code https://github.com/j2eeexpert2015/springbootdebug)localhost:5000While
localhost:5000in the browser smoothly returns{"get_employees": [{"Employee1": "Developer"}, {"Employee2": "HR"}]}`the breakpoint turns from red to hollow circle and never get hit

yezhengli-Mr9 commentedon Jan 1, 2024
Submit issue #1491
stevefoga commentedon Feb 13, 2024
Did anybody ever find a workaround for this?
Keith-CPlus commentedon Feb 23, 2024
Any other workaround for this? Other than bumping down werkzeug and flask versions?
lukaspiatkowski commentedon Mar 21, 2024
I've got a workaround, but you have to use debugpy API instead of the CLI. Just find the place in your code before you call
run(... , use_reloader=True)on your flask app and add:This will start debugpy only for the first original invocation of your app and not for the subsequent reload invocations. The
WERKZEUG_RUN_MAINenv variable is set by Werkezeug itself and is used to identify if this is the reloadable subprocess.Keith-CPlus commentedon Mar 26, 2024
This workaround works, but I seemed to be getting

SystemExit: 3, although it doesn't affect functionality.davidlougheed commentedon Apr 15, 2024
still encountering this; I need to keep live reloading enabled in a dev image with debugpy installed, and for now I'm stuck on Flask 2.2 until this gets fixed.
MaximeOlivie commentedon Apr 29, 2024
Same here. Very annoying. No new workaround from someone?
madolmo commentedon May 5, 2024
Same problem. Just upgrading from Werkzeug 2.2.2 to 2.3.8 debugpy doesen't work anymore.
sevenearths commentedon May 14, 2024
Same here. I have a project waiting for this update :(
oelhammouchi commentedon Aug 26, 2024
I've tried to provide a fix in #1657 since I need this functionality for a dockerised Flask project. So far it seems to work just fine. The tests are also positive with the exception of
test_attach_pid_client. I'm using Debian and according to #311 it should be working on Linux, but it doesn't for me. I get the same test errors on the original code though so it's not due to the change.rchiodo commentedon Aug 27, 2024
This seems to work for me with the latest bits of debugpy and werkzeug. I do get an exception when reloading, but debugpy does attach to the new instance:
I have werkzeug 3.0.4 and debugpy 1.8.5
oelhammouchi commentedon Aug 27, 2024
On Debian, I get this fatal error:
Same version of werkzeug and debugpy.
rchiodo commentedon Aug 27, 2024
Okay must be a linux only issue. I'll try on WSL (don't have a linux machine handy).
rchiodo commentedon Aug 27, 2024
Hmm, works fine on WSL too. I don't even get the error I had on windows.
rchiodo commentedon Aug 27, 2024
I tried the fix in #1657 and although I can't seem to reproduce the original error, the fix doesn't seem to break anything, so I merged it. Hopefully this fixes the problem for everybody.
int19h commentedon Aug 28, 2024
There's a lot of platform-specific weirdness around sockets, so it's not surprising that behavior differs. Note though that even when it works, it means that you have two different debugpy adapter processes, one of which is trying to manage both the parent and the child, while the other one (the one that causes the error on Linux and quietly overwrites the socket on Windows) is only managing the child. I'm not quite sure what the overall effects of that would be, but it's likely to break eventually.
sebastienpires commentedon Aug 28, 2024
I can confirm that the #1657 fix has resolved the issue I was facing on multiple applications running Python 3.11.
I've pulled the commit into my requirements file and upgraded Flask to 3.0.3 and Werkzeug to 3.0.4.
The debugger now starts without any issue and I'm able to use hot-reloading.
Thanks Othman El Hammouchi (@oelhammouchi) and Rich Chiodo (@rchiodo)
Update version of debugpyy
Update version of debugpyy
Update version of debugpyy
Update version of debugpyy