unbit / uwsgi

uWSGI application server container
http://projects.unbit.it/uwsgi
Other
3.45k stars 688 forks source link

Enable debugging in vscode (uwsgi and flask) #2638

Open dev-samples opened 4 months ago

dev-samples commented 4 months ago

I am trying to enable debugging in vscode for an uwsgi application that is serving a Flask app.

I have found some resources here:

https://code.visualstudio.com/docs/python/tutorial-flask https://stackoverflow.com/questions/55605176/setup-uwsgi-debug-in-vs-code

The first link will work if removing uwsgi from the setup and the second link is more what I need, but its not working.

Before spending to much time on this is it a know limitation the debugging uwsgi in vscode is not supported?

app.py

import sys
from flask import Flask
app = Flask(__name__)
app.debug = True

@app.route("/")
def hello():
    return "Hello world from Flask"

if __name__ == "__main__":
    app.run(host='0.0.0.0')

wsgi.py

import sys
from app import app
app.debug = True

import debugpy
debugpy.configure(python="/usr/bin/python3")
debugpy.listen(('0.0.0.0', 4444))
debugpy.wait_for_client()
debugpy.breakpoint() 

if __name__ == "__main__":
    app.run()

myproject.ini

[uwsgi]
module = wsgi:app

master = true
processes = 1

socket = :5000
die-on-term = true

protocol = http
catch-exceptions = true

.vscode/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": [
    {
      "type": "debugpy",
      "request": "attach",
      "name": "Launch uWSGI",
      "connect": {
        "host": "localhost",
        "port": 4444
      },      
      "pathMappings": [
        {
          "localRoot": "${workspaceFolder}", 
          "remoteRoot": "${workspaceFolder}" 
        }
      ]
    },
  ]
}

And then start server with:

uwsgi --ini myproject.ini

When I run the above debug configuration from inside vscode I can see that the server is responding:

[pid: 36184|app: 0|req: 1/1] 127.0.0.1 () {10 vars in 113 bytes} [Fri May 10 22:30:51 2024] Content-Length: => generated 153 bytes in 14 msecs ( 405) 3 headers in 123 bytes (2 switches on core 0)

I just don't hit any of my breakpoints (that I have all over the place)

xrmx commented 4 months ago

I think that debugger is supposed to run itself a python module so it isn't going to work.

dev-samples commented 4 months ago

Actually it looks like its working, just had to put the debugpy configs in the route:

@app.route("/")
def hello():
    if(not debugpy.is_client_connected()):
        debugpy.listen(('0.0.0.0', 4444))

    print("Waiting for debugger attach")
    debugpy.wait_for_client()
    debugpy.breakpoint() #must have 

I can now call that route (/) multiple times and end up in the breakpoints I have set in there.,

dev-samples commented 4 months ago

Not really consistent though, I did find: https://github.com/microsoft/debugpy/issues/1536 https://stackoverflow.com/questions/78288758/debugpy-problem-vs-code-debugpy-not-responding-to-cmd-thread-suspend-single-not

but not sure its 100% related