From 64eba103e34b84d06902e0c8ffcf8bbe93f8d6a1 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Tue, 4 Mar 2025 08:51:38 -0400 Subject: [PATCH] MT#55283 protect timer_fd with mutex Solves a race condition when deleting a stream, as the fd might get closed just as another thread tries to read from it, leading to EBADF read error. Change-Id: I8ea3ff72c2788ce3051a86a0bbd1650b03965376 --- perf-tester/main.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/perf-tester/main.c b/perf-tester/main.c index 32b083dbe..d1b34cc08 100644 --- a/perf-tester/main.c +++ b/perf-tester/main.c @@ -339,7 +339,13 @@ static void readable(int fd, void *o) { while (true) { uint64_t exp; - ssize_t ret = read(fd, &exp, sizeof(exp)); + ssize_t ret; + { + LOCK(&s->lock); + if (s->timer_fd != fd) + break; + ret = read(fd, &exp, sizeof(exp)); + } if (ret != sizeof(exp)) { if (ret == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) break; @@ -584,8 +590,14 @@ static void del_stream(void) { if (!s) return; - poller_del_item(rtpe_poller, s->timer_fd); - s->timer_fd = -1; + int fd; + { + LOCK(&s->lock); + fd = s->timer_fd; + s->timer_fd = -1; + } + + poller_del_item(rtpe_poller, fd); obj_put(s); }