Skip to content

Commit

Permalink
Properly replace close() to shutdown_socket()
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakio815 committed Dec 21, 2024
1 parent e3b0ea1 commit 30a9446
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
14 changes: 6 additions & 8 deletions core/federated/RTI/rti_remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,11 @@ void* federate_info_thread_TCP(void* fed) {
// Socket is closed
lf_print_error("RTI: Socket to federate %d is closed. Exiting the thread.", my_fed->enclave.id);
my_fed->enclave.state = NOT_CONNECTED;
my_fed->socket = -1;
// Nothing more to do. Close the socket and exit.
// Prevent multiple threads from closing the same socket at the same time.
LF_MUTEX_LOCK(&rti_mutex);
shutdown_socket(my_fed->socket, false); // from unistd.h
LF_MUTEX_UNLOCK(&rti_mutex);
// FIXME: We need better error handling here, but do not stop execution here.
break;
}
Expand Down Expand Up @@ -989,12 +993,6 @@ void* federate_info_thread_TCP(void* fed) {
}
}
}

// Nothing more to do. Close the socket and exit.
// Prevent multiple threads from closing the same socket at the same time.
LF_MUTEX_LOCK(&rti_mutex);
close(my_fed->socket); // from unistd.h
LF_MUTEX_UNLOCK(&rti_mutex);
return NULL;
}

Expand Down Expand Up @@ -1445,7 +1443,7 @@ void* respond_to_erroneous_connections(void* nothing) {
while (true) {
// Wait for an incoming connection request.
// The following will block until either a federate attempts to connect
// or close(rti->socket_descriptor_TCP) is called.
// or shutdown_socket(rti->socket_descriptor_TCP) is called.
int socket_id = accept_socket(rti_remote->socket_descriptor_TCP, -1);
if (socket_id < 0) {
return NULL;
Expand Down
6 changes: 3 additions & 3 deletions core/federated/federate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1993,7 +1993,7 @@ void* lf_handle_p2p_connections_from_federates(void* env_arg) {
// Ignore errors on this response.
write_to_socket(socket_id, 2, response);
}
close(socket_id);
shutdown_socket(socket_id, false);
continue;
}

Expand All @@ -2013,7 +2013,7 @@ void* lf_handle_p2p_connections_from_federates(void* env_arg) {
// Ignore errors on this response.
write_to_socket(socket_id, 2, response);
}
close(socket_id);
shutdown_socket(socket_id, false);
continue;
}

Expand Down Expand Up @@ -2051,7 +2051,7 @@ void* lf_handle_p2p_connections_from_federates(void* env_arg) {
// Failed to create a listening thread.
LF_MUTEX_LOCK(&socket_mutex);
if (_fed.sockets_for_inbound_p2p_connections[remote_fed_id] != -1) {
close(socket_id);
shutdown_socket(socket_id, false);
_fed.sockets_for_inbound_p2p_connections[remote_fed_id] = -1;
}
LF_MUTEX_UNLOCK(&socket_mutex);
Expand Down

0 comments on commit 30a9446

Please sign in to comment.