POSTS
Lets Build a Web Server by Ruslan Spivak
Over the Chinese New Year holiday, I was working through this series of web tutorials on Ruslan’s blog. Indeed, as the articles recommend, typing out and testing each bit of code (using curl) helped me to understand each point the author was trying to illustrate. It’s always nice to get to write something low-level, because the abstractions you work with (sockets, file handles) are all pretty mature so any problems you encounter are most likely to be found in your own code. And another bonus is having to learn about stuff that may have seemed “tangential” to writing a web server in the first place. For example, part 2 of the series has you exploring the WSGI interface, which allows you to host web apps written in major Python web frameworks (flask, django) on your own scrappy web server.
Perhaps the most relevant knowledge to my current job that I gained was in part 3, where you get to explore the problems of writing a concurrent web server. Although Python does have a threading module allowing you to write code that looks multithreaded, it’s not truly concurrent since the Python runtime is single-threaded and uses the global interpreter lock to cycle between “threads”. Of course, there is also the multiprocessing module, but for learning purposes the article delves into manually forking the process in order to handle new client requests. It was definitely interesting to get to see how zombie processes are created (due to not cleaning them up with the wait syscall), and even how a naive cleanup handler can get “overwhelmed” by having too many concurrent client requests which then result in some zombie processes escaping cleanup. (“zombie processes” are a really fun name, so evocative!)
What’s so bad about zombie processes anyway? As part of my duties involve being a sysadmin, I do see a lot of zombie processes appear within our kubernetes cluster, due to docker containers not cleaning up properly when they get killed. These zombie processes don’t really present an issue, because they do not use up system resources (they do consume file descriptors but the overall number of zombies is quite small). Still, I’d like to make sure they don’t happen at all, because cleaning them up involves restarting the server so it’s a painful process.
-
python
zombies
webserver