Repeated reference in select messages from the new socket module

Guilherme Andrade g@REDACTED
Sat Oct 3 04:00:10 CEST 2020

Hello list,

Today I found a peculiar situation when using the new socket[1] module.

Upon a `:send/3` call with `nowait` as the timeout returning `{select,
SelectInfo}`, the controlling process will sometimes receive a duplicate
reference within two asynchronous select messages; the second message comes
later /when/ the socket is closed by a separate process (not the
controlling one) just at the right time - the conditions are hard to

That is:
- the controlling process first gets a `{'$socket', socket(), select,
SelectRef}` message when the socket is available for writing
- the controlling process then gets a second `{'$socket', socket(), abort,
{SelectRef, closed}` message
...and `SelectRef` is the same for both.

I looked for the root cause within `prim_socket_nif.c` (OTP and,
if I'm interpreting it correctly, this may happen upon 1) the socket
becoming available for writing and dispatching the message earlier passed
onto `enif_select_write`[2] and 2) a secondary process closing the socket
and dispatching the abort message while the controlling process is still
registered as a writer.

However, the C NIF code responsible for handling `socket` stuff is quite a
lot to take in an afternoon, and I may have misunderstood it.

Is my theory correct? Can select messages with a duplicate reference be
dispatched to the same process? And is this expected, or possibly a bug?

I worked around it by flushing the controlling process message queue with
`receive` to avoid the unexpected, second message by generically consuming
any remaining `select` messages related to that socket.

I can distill the code that replicates it (on OTP, mac OS) but
I'm asking about it first, just in case this is known or expected somehow.
The duplicate reference did caught me offhand.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the erlang-questions mailing list