Friday, July 10, 2015

Some socket/application debugging tips (Linux, Python)

This is more of a note than a blog post. Recently, I was engaged in debugging a web application that used Flask (a python web framework) and Moses, a statistical machine translation program from academia. At one point the front end, a GUI built on Flask, stopped responding and I didn't want to stop the entire process.

To see what sockets were active,
sudo netstat -ltnp
From there, with the pid in hand,
sudo strace -s 3000 -f -p <PID>
Searching for socket related system calls I was able to get a sense of what was going on. If just wanted to look at network traffic then -e trace=network would have been appropriate.

In this case my application was stuck on recv(6, , ,, where a socket was established but not receiving any information. Since I was using the basic Flask webserver things were single threaded and the app could not return to serving static pages again. The solution here is to shutdown the hanging socket and let the app serve pages as normal...

The socket was shut down with gdb:
#sudo apt-get install python3-dbg gdb
sudo gdb python3 <PID>
With the symbol table loaded we can use the shutdown command with,
call shutdown(6, 2) # safely shutsdown socket, even for multithreaded applications