anyevent: handle 'disconnected' flag in client_do_disconnect() itself

Commit f737984 ("fix #4816: do not disconnect twice if client sends no
data") introduced a 'disconnected' flag in the request state to avoid
duplicate calls to client_do_disconnect() for a given client. The flag
is only set and checked in the on_error callback of the handle
however. Do this more centrally at the beginning of the
client_do_disconnect() function itself to catch all callers and code
paths that could lead to a duplicate call. For example, while not
currently known to cause issues, the on_eof handler might re-enter the
function.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
Link: https://lore.proxmox.com/20250408142014.86344-4-f.ebner@proxmox.com
This commit is contained in:
Fiona Ebner 2025-04-08 16:20:14 +02:00 committed by Thomas Lamprecht
parent fa63916921
commit f82b416214

View File

@ -136,6 +136,13 @@ sub cleanup_reqstate {
sub client_do_disconnect {
my ($self, $reqstate) = @_;
# Avoid any re-entrant call. For example, the on_error callback can be called twice for the same
# connection/handle if the timeout is reached before any data has been received. The on_error
# callback might also get invoked as part of the stoptls() call during shutdown below, which is
# another situation where the function would be re-entered without this check.
return if $reqstate->{disconnected};
$reqstate->{disconnected} = 1;
cleanup_reqstate($reqstate, 1);
my $shutdown_hdl = sub {
@ -1911,13 +1918,7 @@ sub accept_connections {
my ($hdl, $fatal, $message) = @_;
eval {
$self->log_aborted_request($reqstate, $message);
# this error callback can be called twice for the same
# connection/handle if the timeout is reached before
# any data has been received, avoid misleading errors
if (!$reqstate->{disconnected}) {
$reqstate->{disconnected} = 1;
$self->client_do_disconnect($reqstate);
}
$self->client_do_disconnect($reqstate);
};
if (my $err = $@) { syslog('err', "$err"); }
},