Skip to content

Commit 16a45d2

Browse files
committed
Set accepted sockets nonblocking
Configure newly accepted generic_unix TCP sockets as nonblocking before publishing them to the socket driver machinery. If fcntl fails, close the accepted fd and reply with an error so callers never observe a partially initialized connection. Signed-off-by: Peter M <petermm@gmail.com>
1 parent bf13b72 commit 16a45d2

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/platforms/generic_unix/lib/socket_driver.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,21 @@ static EventListener *accept_callback(GlobalContext *glb, EventListener *base_li
10601060
TRACE("socket_driver|accept_callback: accepted connection. fd: %i\n", fd);
10611061

10621062
term pid = listener->pid;
1063+
if (UNLIKELY(fcntl(fd, F_SETFL, O_NONBLOCK) == -1)) {
1064+
int err = errno;
1065+
close(fd);
1066+
BEGIN_WITH_STACK_HEAP(12, heap);
1067+
term ref = term_from_ref_ticks(listener->ref_ticks, &heap);
1068+
term reply = port_heap_create_reply(&heap, ref, port_heap_create_sys_error_tuple(&heap, FCNTL_ATOM, err));
1069+
port_send_message_nolock(glb, pid, reply);
1070+
END_WITH_STACK_HEAP(heap, glb);
1071+
globalcontext_get_process_unlock(glb, ctx);
1072+
if (socket_data->passive_listener) {
1073+
socket_data->passive_listener = NULL;
1074+
free(listener);
1075+
}
1076+
return NULL;
1077+
}
10631078
SocketDriverData *new_socket_data = socket_driver_create_data();
10641079
new_socket_data->sockfd = fd;
10651080
new_socket_data->proto = socket_data->proto;

0 commit comments

Comments
 (0)