Reputation: 3991
So.... I got very confused as to why my epoll based tcp server was randomly hanging epoll_wait, and then sending an empty array to some of the connections when I terminated with SIGINT.
Turns out, I had a random bug in my software which resulted in me writing what amounts to the following:
write(server_socket_fd, payload, 0)
Whilst this was incorrect, I certainly wouldn't expect that calling write with a size of 0 would hang the whole thing.
Why is this behavior happening and how do I prevent it ? Is calling write with the length argument zero just UB and I should always make sure the size is > 0 ?
Upvotes: 0
Views: 117
Reputation: 180161
Taking your questions in reverse order,
Is calling write with the length argument zero just UB and I should always make sure the size is > 0 ?
If
nbyte
is zero and the file is not a regular file, the results are unspecified.
The Linux manual pages for write()
say the same thing. "Unspecified" is different from "undefined" in standardese, but if you actually care what happens then it is wise to avoid unspecified behaviors, too.
Why is this behavior happening and how do I prevent it ?
The most reliable way to prevent it would be to avoid zero-length writes to anything but regular files. It might be that there is something specific to your usage pattern that triggers the unwanted behavior you observed, but we cannot evaluate code that you do not present to us, and even if we could, the behavior is still unspecified. Anything we knew or determined might change without warning in some future version of the kernel or standard library.
Upvotes: 1