spenceforce / pdb-attach

A python debugger that can attach to running processes.
BSD 3-Clause "New" or "Revised" License
30 stars 1 forks source link

Running inside wsgi flask #15

Closed mrForce closed 2 years ago

mrForce commented 2 years ago

I have a flask app running through Apache, and I'm trying to use pdb_attach with it. I inserted this into the main script:

import pdb_attach pdb_attach.listen(6222)

After the server starts up, I tried to attach to it via: sudo -u apache python3.6 -m pdb_attach 3782 6222, but that didn't work. So, I tried running "nc localhost 6222", to see if anything was even listening on the port, and it just immediately returned. Running sudo lsof -i -P -n | grep LISTEN doesn't turn up anything listening on port 6222 either.

Then, I created a sample flask app, and stuck pdb_attach.listen(6333) at the top. Ran flask run, and then attached the pdb_attach to it, and it worked fine. So, it seems to be the fact that it's running through apache that is creating the issue. Do you have any advice for remedying this?

spenceforce commented 2 years ago

Interesting. My first thought is maybe Apache hasn't started the python process yet? What happens if you create an endpoint that loops infinitely, go to the endpoint in the browser, then connect with pdb-attach? Can you send a minimal code example including files related to the Apache set up for your project?

On a side note, I'm currently in the middle of a move and may not be able to run any code examples until Monday or Tuesday.

mrForce commented 2 years ago

Thanks! Here are the files. When I do "wget localhost:8082/flask/ending", it hangs, as expected. flaskTest.zip

spenceforce commented 2 years ago

The problem you're having stems from the design of pdb-attach and Apache. It uses signals to listen for incoming client requests to attach to the process. Because of that I think you are going to run into a lot of problems using pdb-attach inside Apache.

Apparently Apache intercepts and ignores all signal handlers that are registered by the user program. There is a way around this documented here. When I applied the solution from the mod_wsgi docs I ran into another issue though. Not sure if this is a problem caused by Apache or by Flask in combination with Apache, but child threads are spawned (I assume by Apache/mod_wsgi) that the application code is run in. When pdb_attach.listen() tries to set a signal handler python throws an error: ValueError: signal only works in main thread, so pdb-attach isn't going to work for Flask + Apache/mod_wsgi in general.

I would hesitate to use pdb-attach from Apache anyways, especially if this is a production server. Anybody with login access to the machine can attach to the running process since it communicates via sockets and typically you want a web server to be pretty locked down on who can touch code running in production.

I have been working on another method for pdb-attach to work without signal handlers as signal handlers are unusable for this purpose on Windows, making pdb-attach unusable on Windows. I think it would work for your use case, but it's not finished.

mrForce commented 2 years ago

Thanks!