multi: xfers_really_alive

Yes, we were counting the "live" transfers before, but were they
*really* alive?

When determining to add the wakeup socket to fdset/waitfds etc, we
should only do that when the multi handle is actually processing
transfers. Other wise, the application could wait on the wakeup socket
forever.

For this, we counted `multi->xfers_alive` (e.g. the "running" number
returned by `curl_multi_perform()`). This was almost correct.

The problem is that added easy handles are counted as "alive" right away
on the addition. But the processing has not started yet. They did not
trigger any DNS resolves or opened any sockets yet.

Add two fields in multi and easy handle:

* `multi->xfers_really_alive`: counts the "alive" transfers that have
  passed `MSTATE_INIT` (at least once)
* `data->state.really_alive`: to track if the transfer has been counted

Add test 2412 to check that adding transfers without perform will not
trigger the wakeup socket to be added.

Fixes #22050
Reported-by: Bryan Henderson
Closes #22066
This commit is contained in:
Stefan Eissing 2026-06-17 14:20:02 +02:00 committed by Daniel Stenberg
parent abad1c9e48
commit f0be417635
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
8 changed files with 182 additions and 13 deletions

View file

@ -531,6 +531,8 @@ CURLMcode curl_multi_add_handle(CURLM *m, CURL *curl)
/* set the easy handle */
multistate(data, MSTATE_INIT);
/* not yet passed INIT state */
data->state.really_alive = FALSE;
#ifdef USE_LIBPSL
/* Do the same for PSL. */
@ -570,12 +572,6 @@ CURLMcode curl_multi_add_handle(CURLM *m, CURL *curl)
data->set.server_response_timeout;
multi->admin->set.no_signal = data->set.no_signal;
mresult = multi_assess_wakeup(multi);
if(mresult) {
failf(data, "error enabling wakeup listening: %d", mresult);
return mresult;
}
CURL_TRC_M(data, "added to multi, mid=%u, running=%u, total=%u",
data->mid, Curl_multi_xfers_running(multi),
Curl_uint32_tbl_count(&multi->xfers));
@ -851,6 +847,12 @@ CURLMcode curl_multi_remove_handle(CURLM *m, CURL *curl)
/* If in `msgsent`, it was deducted from `multi->xfers_alive` already. */
if(!Curl_uint32_bset_contains(&multi->msgsent, data->mid))
--multi->xfers_alive;
if(data->state.really_alive) {
data->state.really_alive = FALSE;
--multi->xfers_really_alive;
if(!multi->xfers_really_alive)
(void)multi_assess_wakeup(multi);
}
Curl_wildcard_dtor(&data->wildcard);
@ -1151,7 +1153,9 @@ CURLMcode Curl_multi_pollset(struct Curl_easy *data,
/* The admin handle always listens on the wakeup socket when there
* are transfers alive. */
if(data->multi && (data == data->multi->admin) &&
data->multi->xfers_alive) {
data->multi->xfers_really_alive) {
CURL_TRC_M(data, "adding wakeup, %u xfers really alive",
data->multi->xfers_really_alive);
result = Curl_pollset_add_in(data, ps, data->multi->wakeup_pair[0]);
}
#endif
@ -2459,6 +2463,12 @@ static void handle_completed(struct Curl_multi *multi,
Curl_uint32_bset_remove(&multi->dirty, data->mid);
Curl_uint32_bset_remove(&multi->pending, data->mid);
Curl_uint32_bset_add(&multi->msgsent, data->mid);
if(data->state.really_alive) {
data->state.really_alive = FALSE;
--multi->xfers_really_alive;
if(!multi->xfers_really_alive)
(void)multi_assess_wakeup(multi);
}
--multi->xfers_alive;
if(!multi->xfers_alive)
multi_assess_wakeup(multi);
@ -2466,6 +2476,18 @@ static void handle_completed(struct Curl_multi *multi,
static CURLMcode multistate_init(struct Curl_easy *data, CURLcode *result)
{
if(!data->state.really_alive) {
data->state.really_alive = TRUE;
++data->multi->xfers_really_alive;
if(data->multi->xfers_really_alive == 1) {
CURLMcode mresult = multi_assess_wakeup(data->multi);
if(mresult) {
failf(data, "error enabling wakeup listening: %d", mresult);
return mresult;
}
}
}
*result = Curl_pretransfer(data);
if(*result)
return CURLM_OK;