- June 2026 (2)
- May 2026 (2)
- April 2026 (11)
- March 2026 (3)
- February 2026 (4)
- January 2026 (4)
- December 2025 (1)
- November 2025 (3)
- October 2025 (9)
- September 2025 (3)
- August 2025 (5)
- July 2025 (1)
- June 2025 (2)
- May 2025 (3)
- April 2025 (2)
- March 2025 (7)
- February 2025 (10)
- January 2025 (6)
- December 2024 (7)
- September 2024 (1)
- August 2024 (2)
- July 2024 (2)
- May 2024 (2)
- April 2024 (2)
- February 2024 (2)
- April 2023 (1)
- March 2023 (2)
- September 2022 (1)
- February 2022 (1)
- November 2021 (1)
- March 2021 (1)
- February 2021 (2)
- August 2019 (1)
- November 2018 (1)
- May 2017 (1)
- December 2016 (1)
- April 2016 (1)
- August 2015 (1)
- December 2014 (1)
- August 2014 (1)
- March 2014 (1)
- December 2013 (1)
- October 2013 (3)
- September 2013 (4)
- August 2013 (2)
- July 2013 (1)
- June 2013 (1)
- February 2013 (1)
- October 2012 (1)
- June 2012 (1)
- May 2012 (1)
- April 2012 (1)
- February 2012 (1)
- October 2011 (1)
- June 2011 (1)
- May 2011 (1)
- April 2011 (1)
- March 2011 (1)
- February 2011 (1)
- January 2011 (1)
- December 2010 (3)
- November 2010 (1)
- October 2010 (1)
- September 2010 (1)
- August 2010 (1)
- July 2010 (1)
- May 2010 (3)
- April 2010 (1)
- March 2010 (2)
- February 2010 (3)
- January 2010 (4)
- December 2009 (2)
- November 2009 (5)
- October 2009 (2)
- September 2009 (2)
- August 2009 (3)
- July 2009 (1)
- May 2009 (1)
- April 2009 (1)
- March 2009 (5)
- February 2009 (5)
- January 2009 (5)
- December 2008 (3)
- November 2008 (7)
- October 2008 (4)
- September 2008 (2)
- August 2008 (1)
- July 2008 (1)
- June 2008 (1)
- May 2008 (1)
- April 2008 (1)
- January 2008 (4)
- December 2007 (3)
- March 2007 (3)
- February 2007 (1)
- January 2007 (2)
- December 2006 (4)
- November 2006 (18)
- AI (83)
- TIL deep dives (75)
- Python (71)
- LLM from scratch (46)
- Resolver One (34)
- PyTorch (21)
- Blogkeeping (18)
- TIL (18)
- PythonAnywhere (17)
- Linux (16)
- Startups (15)
- Hugging Face (13)
- NSLU2 offsite backup project (13)
- Funny (11)
- Gadgets (11)
- Musings (11)
- Finance (10)
- Fine-tuning LLMs (10)
- C (9)
- Personal (8)
- Robotics (8)
- Website design (8)
- 3D (5)
- Rants (5)
- Cryptography (4)
- JavaScript (4)
- Music (4)
- Oddities (4)
- Quick links (4)
- Talks (4)
- Dirigible (3)
- Eee (3)
- JAX (3)
- Memes (3)
- Politics (3)
- Django (2)
- GPU Computing (2)
- LaTeX (2)
- MathML (2)
- OLPC XO (2)
- Retro Language Models (2)
- Space (2)
- VoIP (2)
- Copyright (1)
- Golang (1)
- Microprojects (1)
- Raspberry Pi (1)
- Software development tools (1)
- Agile Abstractions
- Astral Codex Ten
- :: (Bloggable a) => a -> IO ()
- David Friedman's Substack
- Econ & Energy
- Entrepreneurial Geekiness
- For some value of "Magic"
- Hackaday
- kaleidic.ai newsletter
- Knowing.NET
- Language Log
- Millennium Hand
- ntoll.org
- Obey the Testing Goat!
- PK
- PythonAnywhere News
- Simon Willison's Weblog
- Societive
- Software Deviser
- Some opinions, held with varying degrees of certainty
- tartley.com
Writing a reverse proxy/loadbalancer from the ground up in C, pause to regroup: non-blocking output
Before moving on to the next step in my from-scratch reverse proxy, I thought it would be nice to install it on the machine where this blog runs, and proxy all access to the blog through it. It would be useful dogfooding and might show any non-obvious errors in the code. And it did.
I found that while short pages were served up perfectly well, longer pages were corrupted and interrupted halfway through. Using curl gave various weird errors, eg.
curl: (56) Problem (3) in the Chunked-Encoded data
...which is a general error saying that it's receiving chunked data and the chunking is invalid.
Doubly strangely, these problems didn't happen when I ran the proxy on the machine where I'm developing it and got it to proxy the blog; only when I ran it on the same machine as the blog. They're different versions of Ubuntu, the blog server being slightly older, but not drastically so -- and none of the stuff I'm using is that new, so it seemed unlikely to be a bug in the blog server's OS. And anyway, select isn't broken.
After a ton of debugging with printfs here there and everywhere, I tracked it
down.
You'll remember that our code to transfer data from the backend to the client looks like this:
void handle_backend_socket_event(struct epoll_event_handler* self, uint32_t events)
{
struct backend_socket_event_data* closure = (struct backend_socket_event_data*) self->closure;
char buffer[BUFFER_SIZE];
int bytes_read;
if (events & EPOLLIN) {
bytes_read = read(self->fd, buffer, BUFFER_SIZE);
if (bytes_read == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
return;
}
if (bytes_read == 0 || bytes_read == -1) {
close_client_socket(closure->client_handler);
close_backend_socket(self);
return;
}
write(closure->client_handler->fd, buffer, bytes_read);
}
if ((events & EPOLLERR) | (events & EPOLLHUP) | (events & EPOLLRDHUP)) {
close_client_socket(closure->client_handler);
close_backend_socket(self);
return;
}
}
If you look closely, there's a system call there where I'm not checking the return value -- always risky. It's this:
write(closure->client_handler->fd, buffer, bytes_read);
The write function returns the number of bytes it managed to write, or an
error code. The debugging code revealed that sometimes it was returning -1, and
errno was set to EAGAIN, meaning that the operation would have blocked on a
non-blocking socket.
This makes a lot of sense. Sending stuff out over the network is a fairly complex process. There are kernel buffers of stuff to send, and as we're using TCP, which is connection-based, I imagine there's a possibility that the client is being slow, or the transmission of data over the Internet might be causing things to back up. Possibly sometimes it was returning a non-error code, too, but was still not able to write all of the bytes I asked it to write, so stuff was getting skipped.
[Update, later: and of course the different behaviour on the two machines would make sense. When it was running on the same machine as the blog's HTTP server, it would get data from it really quickly -- fast enough to overwhelm the "upstream" connection to the browser. By contrast, when it was running on a separate machine, the downstream and upstream bandwidth would be more similar -- data would come in from the blog at a speed much closer to the speed at whcih it could be sent to the client.]
So that means that even for this simple example of an epoll-based proxy to
work properly, we need to do some kind of buffering in the server to handle
cases where we're getting stuff from the backend faster than we can send it to
the client. And possibly vice versa. It's possible to get epoll events on an
FD when it's ready to accept output, so that's probably the way to go -- but it
will need a bit of restructuring. So the next step will be to implement that,
rather than the multiple-backend handling stuff I was planning.
This is excellent. Now I know a little more about why writing something like
nginx is hard, and have a vague idea of why I sometimes see stuff in its logs
along the lines of an upstream response is buffered to a temporary file.
Which is entirely why I started writing this stuff in the first place :-)
Here's a run-through of the code I had to write to fix the bug.