[erlang-bugs] minor bug in erl_interface.

Alexandre Snarskii <>
Wed May 8 12:32:13 CEST 2013


During valgrinding freeswitch compiled with mod_erlang_event
valgrind output was flooded with messages like the following: 

==96247== Warning: invalid file descriptor -2 in syscall close()
==96247==    at 0x7CA9D3: __sys_close (in /usr/lib32/libc.so.7)
==96247==    by 0xBC66F3: ei_accept_tmo (in /usr/local/lib/freeswitch/mod/mod_er
==96247==    by 0xBBF34F: mod_erlang_event_runtime (mod_erlang_event.c:1957)
==96247==    by 0x145DF8: switch_loadable_module_exec (switch_loadable_module.c:
==96247==    by 0x1F17B3: dummy_worker (thread.c:138)
==96247==    by 0x367F19: ??? (in /usr/lib32/libthr.so.3)

According to sources (./lib/erl_interface/src/connect/ei_connect.c), -2 is 
a timeout indication from ei_accept_t:

    if ((fd = ei_accept_t(lfd, (struct sockaddr*) &cli_addr, 
        &cli_addr_len, ms )) < 0) {
        EI_TRACE_ERR0("ei_accept","<- ACCEPT socket accept failed");
        erl_errno = (fd == -2) ? ETIMEDOUT : EIO;
        goto error;
    EI_TRACE_ERR0("ei_accept","<- ACCEPT failed");
    return ERL_ERROR;
} /* ei_accept */

and closesocket on unix systems is defined as just close(2), so any 
timeout or error on accept causes closing invalid file descriptor.

Patch is obvious: 

    EI_TRACE_ERR0("ei_accept","<- ACCEPT failed");
-   closesocket(fd);
+   if (fd>=0) 
+      closesocket(fd);
    return ERL_ERROR;

In theory, there is no difference between theory and practice. 
But, in practice, there is. 

More information about the erlang-bugs mailing list