Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write to TCP socket keeps returning EAGAIN

I'm seeing a situation where I have a TCP socket (non-blocking) that I'm writing to, but under load it gets into a situation where it keeps returning EAGAIN. I know this because it stops processing, and I can attach a debugger and step through it. Stepping through, the write call returns an error and errno is set to EAGAIN every time (it busy-waits on EAGAIN...ignore that this is a bad idea :)

My understanding was that EAGAIN should only be returned on a write if the buffer is full, but I don't understand what would prevent it from draining and the write call eventually succeeding.

This is Ubuntu, Linux kernel 3.19.0-47-generic.

Ideas?

like image 816
rivenmyst137 Avatar asked Oct 29 '25 17:10

rivenmyst137


1 Answers

What is preventing it from draining is that the peer isn't reading as fast as you're writing. The peer's receive buffer fills, your send buffer fills, you can't write.

it busy-waits on EAGAIN...ignore that this is a bad idea

I can't ignore that. It's a bad idea. You're supposed to use select() in this specific circumstance to tell you when the socket becomes writable, not just loop mindlessly.

like image 176
user207421 Avatar answered Oct 31 '25 06:10

user207421