# Socket:send/4 timeout

**URL:** <https://erlangforums.com/t/socket-send-4-timeout/2974>\
**Category:** Questions / Help\
**Tags:** erlang, networking-tasks\
**Created:** [October 25, 2023, 8:37pm UTC](https://erlangforums.com/t/socket-send-4-timeout/2974 "2023-10-25T20:37:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![saleyn](https://erlangforums.com/user_avatar/erlangforums.com/saleyn/32/1188_2.png) [@saleyn](https://erlangforums.com/u/saleyn)\
**Post date:** [October 25, 2023, 8:37pm UTC](https://erlangforums.com/t/socket-send-4-timeout/2974/1 "2023-10-25T20:37:46Z")

</div>

For the `socket:sendmsg(Socket, Msg, Timeout)` or `socket:send(Socket, Msg, Timeout)` calls, is the call atomic in a sense that if there is a timeout, no part of the `Msg` will be written to the underlying socket buffer?

---

<div class="post-metadata">

**Author:** ![starbelly](https://erlangforums.com/user_avatar/erlangforums.com/starbelly/32/209_2.png) [@starbelly](https://erlangforums.com/u/starbelly)\
**Post date:** [October 26, 2023, 3:35am UTC](https://erlangforums.com/t/socket-send-4-timeout/2974/2 "2023-10-26T03:35:00Z")

</div>

I’m quite sure some others around here could comment in great detail, but that sounds right to me, in that you’re timing out on waiting for the select event (really [Erlang -- erl\_nif](https://www.erlang.org/doc/man/erl_nif#enif_select)) to say “Hey, you are good to go on writing to the socket now!”. An oversimplification, but still and in other words, no write will be performed until a control signal is received.

Also see [erl\_check\_io.c](https://github.com/erlang/otp/blob/master/erts/emulator/sys/common/erl_check_io.c)

Edit:

FWIW, I have read about cases where epoll can misfire FWIW, such that an FD isn’t really ready for writing or reading (iirc, this was around reads). I’d have to dig that back up and it’s not something I generally worry about myself, but I figured it’s so related to what you’re asking it’s worth mentioning.

---

<div class="post-metadata">

**Author:** ![saleyn](https://erlangforums.com/user_avatar/erlangforums.com/saleyn/32/1188_2.png) [@saleyn](https://erlangforums.com/u/saleyn)\
**Post date:** [October 26, 2023, 5:35am UTC](https://erlangforums.com/t/socket-send-4-timeout/2974/3 "2023-10-26T05:35:03Z")

</div>

For the TCP protocol the write to the socket buffer is atomic, but my question was more about whether the `Msg` passed to the `send/3` function is written in chunks (especially if it represents an iolist), with the opportunity to timeout while finishing to write all chunks, or the operation is always atomic. I suppose it’s always atomic, but it doesn’t hurt to ask.

As to the epoll misfiring, it wouldn’t be an issue as the subsequent `send(2)` call would return an EAGAIN error anyway if there’s not enough space in the socket buffer, and I assume, the NIF implementation would loop again for the next epoll check.

---

<div class="post-metadata">

**Author:** ![starbelly](https://erlangforums.com/user_avatar/erlangforums.com/starbelly/32/209_2.png) [@starbelly](https://erlangforums.com/u/starbelly)\
**Post date:** [October 26, 2023, 5:54am UTC](https://erlangforums.com/t/socket-send-4-timeout/2974/4 "2023-10-26T05:54:32Z")

</div>

Ahh yeah, well now that’s more interesting and would take digging into the code or getting an answer from an expert in this area of the code base. If the socket buffer is smaller than the size of your message, that can’t be atomic, right?

And yeah, I do believe the problematic misfire I’m thinking about is around reads (epoll says hey, there’s data for you, but not really 😛 )

---

<div class="post-metadata">

**Author:** ![saleyn](https://erlangforums.com/user_avatar/erlangforums.com/saleyn/32/1188_2.png) [@saleyn](https://erlangforums.com/u/saleyn)\
**Post date:** [October 26, 2023, 11:52pm UTC](https://erlangforums.com/t/socket-send-4-timeout/2974/5 "2023-10-26T23:52:44Z")

</div>

Yes, if the socket buffer can’t fit the data, you would get the EMSGSIZE error back from the kernel.

I checked the [source code](https://github.com/erlang/otp/blob/master/erts/emulator/nifs/unix/unix_socket_syncio.c#L2266) and the `send/sendmsg` calls are indeed atomic.

Though one might argue that it would make sense to loop there on return of EINT. Perhaps @raimo can comment?
