Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unix: fix signal handling data race #4287

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/unix/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "internal.h"
#include "strtok.h"

#include <stdatomic.h>
#include <stddef.h> /* NULL */
#include <stdio.h> /* printf */
#include <stdlib.h>
Expand Down Expand Up @@ -289,6 +290,7 @@ int uv__getiovmax(void) {


static void uv__finish_close(uv_handle_t* handle) {
atomic_uint caught_signals;
uv_signal_t* sh;

/* Note: while the handle is in the UV_HANDLE_CLOSING state now, it's still
Expand Down Expand Up @@ -322,7 +324,9 @@ static void uv__finish_close(uv_handle_t* handle) {
* okay because we only need to deliver the pending events.
*/
sh = (uv_signal_t*) handle;
if (sh->caught_signals > sh->dispatched_signals) {
caught_signals = atomic_load_explicit((atomic_uint*) &sh->caught_signals,
memory_order_seq_cst);
if (caught_signals > sh->dispatched_signals) {
handle->flags ^= UV_HANDLE_CLOSED;
uv__make_close_pending(handle); /* Back into the queue. */
return;
Expand Down
7 changes: 5 additions & 2 deletions src/unix/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <stdatomic.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
Expand Down Expand Up @@ -231,8 +232,10 @@ static void uv__signal_handler(int signum) {
assert(r == sizeof msg ||
(r == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)));

if (r != -1)
handle->caught_signals++;
if (r != -1) {
atomic_fetch_add_explicit((atomic_uint*) &handle->caught_signals, 1,
memory_order_seq_cst);
}
}
santigimeno marked this conversation as resolved.
Show resolved Hide resolved

uv__signal_unlock();
Expand Down