mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] Deadlock in aio_cancel()
@ 2025-10-22 17:24 Markus Wichmann
  0 siblings, 0 replies; only message in thread
From: Markus Wichmann @ 2025-10-22 17:24 UTC (permalink / raw)
  To: musl

Good evening,

the AIO worker thread has to wait for preceding writes on the same FD to
finish before commencing its own work if the requested operation is a
sync or a write to an FD in append mode. It currently does so by calling
pthread_cond_wait().

pthread_cond_wait() is a cancel point, but has to re-acquire the mutex
even if cancelled. So all in all, the AIO worker thread will re-acquire
the queue lock if it is cancelled while waiting for preceding writes to
complete, and that happens before it can run cleanup().

aio_cancel() will take the queue lock and cancel each thread with a
nonzero running flag. But it waits for the thread to actually finish
with a futex, so it selfishly hogs the queue lock.

Since that means that the AIO worker thread tries to get the queue lock
while the its holder, the thread that called aio_cancel(), waits for the
AIO worker to finish, we have a deadlock situation.

The only solution I see is for aio_cancel() to give up the queue lock
while it waits for the thread to finish. However, that presents a new
problem: Now aio_cancel() has to restart the list iteration when it has
re-acquired the lock, since the list is not necessarily unchanged
afterward. And now I am wondering if this isn't possibly an infinite
loop under some circumstances.

BTW, a secondary issue with this is that even if pthread_cond_wait()
managed to re-acquire the mutex, cleanup() will then deadlock again when
it takes the queue lock that the thread is already holding. But that
could be solved by adding another cleanup handler that releases the
queue lock if cancellation happens while waiting for writers.

Ciao,
Markus

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2025-10-22 17:24 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-22 17:24 [musl] Deadlock in aio_cancel() Markus Wichmann

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).