From bcccc1bfd7e1586259ff1c516a458e04321d0e78 Mon Sep 17 00:00:00 2001 From: Christian Babeux Date: Wed, 5 Dec 2012 23:33:17 -0500 Subject: [PATCH] Cygwin: Fix racy read to detect applications sockets close The mechanism to detect the applications sockets close is racy and could potentially discard data. A read(3) is performed on the application socket and the returned len is compared to 0 to detect the case where an application closed the socket. If a read(3) occur in the thread_manage_apps *before* the consumption of the data in the consumer, one byte of data would be discarded. To fix this race, perform a recv(3) on the socket with the MSG_PEEK flag. This has no impact on the data delivery to the consumer and can successfully detect the applications sockets close. --- src/bin/lttng-sessiond/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bin/lttng-sessiond/main.c b/src/bin/lttng-sessiond/main.c index 722549554..236341d27 100644 --- a/src/bin/lttng-sessiond/main.c +++ b/src/bin/lttng-sessiond/main.c @@ -1293,9 +1293,10 @@ static void *thread_manage_apps(void *data) char dummy; do { - readlen = read(pollfd, &dummy, 1); + readlen = recv(pollfd, &dummy, 1, MSG_PEEK); } while (readlen == -1 && errno == EINTR); + /* Peer has performed an orderly shutdown */ if (readlen == 0) { /* Removing from the poll set */ ret = lttng_poll_del(&events, pollfd); -- 2.34.1