]> git.ipfire.org Git - thirdparty/haproxy.git/commit
BUG/MEDIUM: queue: implement a flag to check for the dequeuing
authorWilly Tarreau <w@1wt.eu>
Wed, 11 Sep 2024 07:37:53 +0000 (09:37 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 13 Sep 2024 06:35:47 +0000 (08:35 +0200)
commitb11495652e724d71f1f4247332f060fe48577664
treeafb47cc08ed4b5986c478b7a5ae9d0c6c0c6ced1
parentadaba6f904d11424327f90c3ab4df085c26fc8c4
BUG/MEDIUM: queue: implement a flag to check for the dequeuing

As unveiled in GH issue #2711, commit 5541d4995d ("BUG/MEDIUM: queue:
deal with a rare TOCTOU in assign_server_and_queue()") does have some
side effects in that it can occasionally cause an endless loop.

As Christopher analysed it, the problem is that process_srv_queue(),
which uses a trylock in order to leave only one thread in charge of
the dequeueing process, can lose the lock race against pendconn_add().
If this happens on the last served request, then there's no more thread
to deal with the dequeuing, and assign_server_and_queue() will loop
forever on a condition that was initially exepected to be extremely
rare (and still is, except that now it can become sticky). Previously
what was happening is that such queued requests would just time out
and since that was very rare, nobody would notice.

The root of the problem really is that trylock. It was added so that
only one thread dequeues at a time but it doesn't offer only that
guarantee since it also prevents a thread from dequeuing if another
one is in the process of queuing. We need a different criterion.

What we're doing now is to set a flag "dequeuing" in the server, which
indicates that one thread is currently in the process of dequeuing
requests. This one is atomically tested, and only if no thread is in
this process, then the thread grabs the queue's lock and dequeues.
This way it will be serialized with pendconn_add() and no request
addition will be missed.

It is not certain whether the original race covered by the fix above
can still happen with this change, so better keep that fix for now.

Thanks to @Yenya (Jan Kasprzak) for the precise and complete report
allowing to spot the problem.

This patch should be backported wherever the patch above was backported.
include/haproxy/server-t.h
src/queue.c