lib: graceful connection shutdown

When libcurl discards a connection there are two phases this may go
through: "shutdown" and "closing". If a connection is aborted, the
shutdown phase is skipped and it is closed right away.

The connection filters attached to the connection implement the phases
in their `do_shutdown()` and `do_close()` callbacks. Filters carry now a
`shutdown` flags next to `connected` to keep track of the shutdown
operation.

Filters are shut down from top to bottom. If a filter is not connected,
its shutdown is skipped. Notable filters that *do* something during
shutdown are HTTP/2 and TLS. HTTP/2 sends the GOAWAY frame. TLS sends
its close notify and expects to receive a close notify from the server.

As sends and receives may EAGAIN on the network, a shutdown is often not
successful right away and needs to poll the connection's socket(s). To
facilitate this, such connections are placed on a new shutdown list
inside the connection cache.

Since managing this list requires the cooperation of a multi handle,
only the connection cache belonging to a multi handle is used. If a
connection was in another cache when being discarded, it is removed
there and added to the multi's cache. If no multi handle is available at
that time, the connection is shutdown and closed in a one-time,
best-effort attempt.

When a multi handle is destroyed, all connection still on the shutdown
list are discarded with a final shutdown attempt and close. In curl
debug builds, the environment variable `CURL_GRACEFUL_SHUTDOWN` can be
set to make this graceful with a timeout in milliseconds given by the
variable.

The shutdown list is limited to the max number of connections configured
for a multi cache. Set via CURLMOPT_MAX_TOTAL_CONNECTIONS. When the
limit is reached, the oldest connection on the shutdown list is
discarded.

- In multi_wait() and multi_waitfds(), collect all connection caches
  involved (each transfer might carry its own) into a temporary list.
  Let each connection cache on the list contribute sockets and
  POLLIN/OUT events it's connections are waiting for.

- in multi_perform() collect the connection caches the same way and let
  them peform their maintenance. This will make another non-blocking
  attempt to shutdown all connections on its shutdown list.

- for event based multis (multi->socket_cb set), add the sockets and
  their poll events via the callback. When `multi_socket()` is invoked
  for a socket not known by an active transfer, forward this to the
  multi's cache for processing. On closing a connection, remove its
  socket(s) via the callback.

TLS connection filters MUST NOT send close nofity messages in their
`do_close()` implementation. The reason is that a TLS close notify
signals a success. When a connection is aborted and skips its shutdown
phase, the server needs to see a missing close notify to detect
something has gone wrong.

A graceful shutdown of FTP's data connection is performed implicitly
before regarding the upload/download as complete and continuing on the
control connection. For FTP without TLS, there is just the socket close
happening. But with TLS, the sent/received close notify signals that the
transfer is complete and healthy. Servers like `vsftpd` verify that and
reject uploads without a TLS close notify.

- added test_19_* for shutdown related tests
- test_19_01 and test_19_02 test for TCP RST packets
  which happen without a graceful shutdown and should
  no longer appear otherwise.
- add test_19_03 for handling shutdowns by the server
- add test_19_04 for handling shutdowns by curl
- add test_19_05 for event based shutdowny by server
- add test_30_06/07 and test_31_06/07 for shutdown checks
  on FTP up- and downloads.

Closes #13976
This commit is contained in:
Stefan Eissing 2024-06-19 12:40:06 +02:00 committed by Daniel Stenberg
parent c1845dc0e2
commit c9b95c0bb3
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
33 changed files with 1313 additions and 276 deletions

View file

@ -410,7 +410,7 @@ struct Curl_multi *Curl_multi_handle(size_t hashsize, /* socket hash */
Curl_hash_init(&multi->proto_hash, 23,
Curl_hash_str, Curl_str_key_compare, ph_freeentry);
if(Curl_conncache_init(&multi->conn_cache, chashsize))
if(Curl_conncache_init(&multi->conn_cache, multi, chashsize))
goto error;
Curl_llist_init(&multi->msglist, NULL);
@ -1248,6 +1248,7 @@ CURLMcode curl_multi_waitfds(struct Curl_multi *multi,
struct Curl_easy *data;
struct curl_waitfds cwfds;
struct easy_pollset ps;
CURLMcode result = CURLM_OK;
if(!ufds)
return CURLM_BAD_FUNCTION_ARGUMENT;
@ -1262,13 +1263,21 @@ CURLMcode curl_multi_waitfds(struct Curl_multi *multi,
memset(&ps, 0, sizeof(ps));
for(data = multi->easyp; data; data = data->next) {
multi_getsock(data, &ps);
if(Curl_waitfds_add_ps(&cwfds, &ps))
return CURLM_OUT_OF_MEMORY;
if(Curl_waitfds_add_ps(&cwfds, &ps)) {
result = CURLM_OUT_OF_MEMORY;
goto out;
}
}
if(Curl_conncache_add_waitfds(&multi->conn_cache, &cwfds)) {
result = CURLM_OUT_OF_MEMORY;
goto out;
}
out:
if(fd_count)
*fd_count = cwfds.n;
return CURLM_OK;
return result;
}
#ifdef USE_WINSOCK
@ -1305,6 +1314,7 @@ static CURLMcode multi_wait(struct Curl_multi *multi,
struct pollfd a_few_on_stack[NUM_POLLS_ON_STACK];
struct curl_pollfds cpfds;
unsigned int curl_nfds = 0; /* how many pfds are for curl transfers */
CURLMcode result = CURLM_OK;
#ifdef USE_WINSOCK
WSANETWORKEVENTS wsa_events;
DEBUGASSERT(multi->wsa_event != WSA_INVALID_EVENT);
@ -1329,11 +1339,16 @@ static CURLMcode multi_wait(struct Curl_multi *multi,
for(data = multi->easyp; data; data = data->next) {
multi_getsock(data, &ps);
if(Curl_pollfds_add_ps(&cpfds, &ps)) {
Curl_pollfds_cleanup(&cpfds);
return CURLM_OUT_OF_MEMORY;
result = CURLM_OUT_OF_MEMORY;
goto out;
}
}
if(Curl_conncache_add_pollfds(&multi->conn_cache, &cpfds)) {
result = CURLM_OUT_OF_MEMORY;
goto out;
}
curl_nfds = cpfds.n; /* what curl internally uses in cpfds */
/* Add external file descriptions from poll-like struct curl_waitfd */
for(i = 0; i < extra_nfds; i++) {
@ -1345,8 +1360,8 @@ static CURLMcode multi_wait(struct Curl_multi *multi,
if(extra_fds[i].events & CURL_WAIT_POLLOUT)
events |= POLLOUT;
if(Curl_pollfds_add_sock(&cpfds, extra_fds[i].fd, events)) {
Curl_pollfds_cleanup(&cpfds);
return CURLM_OUT_OF_MEMORY;
result = CURLM_OUT_OF_MEMORY;
goto out;
}
}
@ -1364,8 +1379,8 @@ static CURLMcode multi_wait(struct Curl_multi *multi,
}
if(mask) {
if(WSAEventSelect(cpfds.pfds[i].fd, multi->wsa_event, mask) != 0) {
Curl_pollfds_cleanup(&cpfds);
return CURLM_INTERNAL_ERROR;
result = CURLM_OUT_OF_MEMORY;
goto out;
}
}
}
@ -1375,8 +1390,8 @@ static CURLMcode multi_wait(struct Curl_multi *multi,
#ifndef USE_WINSOCK
if(use_wakeup && multi->wakeup_pair[0] != CURL_SOCKET_BAD) {
if(Curl_pollfds_add_sock(&cpfds, multi->wakeup_pair[0], POLLIN)) {
Curl_pollfds_cleanup(&cpfds);
return CURLM_OUT_OF_MEMORY;
result = CURLM_OUT_OF_MEMORY;
goto out;
}
}
#endif
@ -1405,8 +1420,8 @@ static CURLMcode multi_wait(struct Curl_multi *multi,
pollrc = Curl_poll(cpfds.pfds, cpfds.n, timeout_ms); /* wait... */
#endif
if(pollrc < 0) {
Curl_pollfds_cleanup(&cpfds);
return CURLM_UNRECOVERABLE_POLL;
result = CURLM_UNRECOVERABLE_POLL;
goto out;
}
if(pollrc > 0) {
@ -1524,8 +1539,9 @@ static CURLMcode multi_wait(struct Curl_multi *multi,
}
}
out:
Curl_pollfds_cleanup(&cpfds);
return CURLM_OK;
return result;
}
CURLMcode curl_multi_wait(struct Curl_multi *multi,
@ -2695,6 +2711,7 @@ CURLMcode curl_multi_perform(struct Curl_multi *multi, int *running_handles)
/* the current node might be unlinked in multi_runsingle(), get the next
pointer now */
struct Curl_easy *datanext = data->next;
if(data->set.no_signal != nosig) {
sigpipe_restore(&pipe_st);
sigpipe_ignore(data, &pipe_st);
@ -2703,11 +2720,14 @@ CURLMcode curl_multi_perform(struct Curl_multi *multi, int *running_handles)
result = multi_runsingle(multi, &now, data);
if(result)
returncode = result;
data = datanext; /* operate on next handle */
} while(data);
sigpipe_restore(&pipe_st);
}
Curl_conncache_multi_perform(multi);
/*
* Simply remove all expired timers from the splay since handles are dealt
* with unconditionally by this function and curl_multi_timeout() requires
@ -2796,7 +2816,7 @@ CURLMcode curl_multi_cleanup(struct Curl_multi *multi)
}
/* Close all the connections in the connection cache */
Curl_conncache_close_all_connections(&multi->conn_cache);
Curl_conncache_multi_close_all(multi);
sockhash_destroy(&multi->sockhash);
Curl_hash_destroy(&multi->proto_hash);
@ -2877,7 +2897,6 @@ static CURLMcode singlesocket(struct Curl_multi *multi,
/* Fill in the 'current' struct with the state as it is now: what sockets to
supervise and for what actions */
multi_getsock(data, &cur_poll);
/* We have 0 .. N sockets already and we get to know about the 0 .. M
sockets we should have from now on. Detect the differences, remove no
longer supervised ones and add new ones */
@ -3155,13 +3174,16 @@ static CURLMcode multi_socket(struct Curl_multi *multi,
if(s != CURL_SOCKET_TIMEOUT) {
struct Curl_sh_entry *entry = sh_getentry(&multi->sockhash, s);
if(!entry)
if(!entry) {
/* Unmatched socket, we can't act on it but we ignore this fact. In
real-world tests it has been proved that libevent can in fact give
the application actions even though the socket was just previously
asked to get removed, so thus we better survive stray socket actions
and just move on. */
;
/* The socket might come from a connection that is being shut down
* by the multi's conncache. */
Curl_conncache_multi_socket(multi, s, ev_bitmask);
}
else {
struct Curl_hash_iterator iter;
struct Curl_hash_element *he;
@ -3294,6 +3316,9 @@ CURLMcode curl_multi_setopt(struct Curl_multi *multi,
break;
case CURLMOPT_MAX_TOTAL_CONNECTIONS:
multi->max_total_connections = va_arg(param, long);
/* for now, let this also decide the max number of connections
* in shutdown handling */
multi->max_shutdown_connections = va_arg(param, long);
break;
/* options formerly used for pipelining */
case CURLMOPT_MAX_PIPELINE_LENGTH: