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

@ -556,19 +556,7 @@ CURLcode Curl_open(struct Curl_easy **curl)
return result;
}
static void conn_shutdown(struct Curl_easy *data)
{
DEBUGASSERT(data);
infof(data, "Closing connection");
/* possible left-overs from the async name resolvers */
Curl_resolver_cancel(data);
Curl_conn_close(data, SECONDARYSOCKET);
Curl_conn_close(data, FIRSTSOCKET);
}
static void conn_free(struct Curl_easy *data, struct connectdata *conn)
void Curl_conn_free(struct Curl_easy *data, struct connectdata *conn)
{
size_t i;
@ -620,11 +608,9 @@ static void conn_free(struct Curl_easy *data, struct connectdata *conn)
*
* This function MUST NOT reset state in the Curl_easy struct if that
* isn't strictly bound to the life-time of *this* particular connection.
*
*/
void Curl_disconnect(struct Curl_easy *data,
struct connectdata *conn, bool dead_connection)
struct connectdata *conn, bool aborted)
{
/* there must be a connection to close */
DEBUGASSERT(conn);
@ -639,13 +625,14 @@ void Curl_disconnect(struct Curl_easy *data,
DEBUGASSERT(!data->conn);
DEBUGF(infof(data, "Curl_disconnect(conn #%"
CURL_FORMAT_CURL_OFF_T ", dead=%d)",
conn->connection_id, dead_connection));
CURL_FORMAT_CURL_OFF_T ", aborted=%d)",
conn->connection_id, aborted));
/*
* If this connection isn't marked to force-close, leave it open if there
* are other users of it
*/
if(CONN_INUSE(conn) && !dead_connection) {
if(CONN_INUSE(conn) && !aborted) {
DEBUGF(infof(data, "Curl_disconnect when inuse: %zu", CONN_INUSE(conn)));
return;
}
@ -662,23 +649,10 @@ void Curl_disconnect(struct Curl_easy *data,
Curl_http_auth_cleanup_negotiate(conn);
if(conn->connect_only)
/* treat the connection as dead in CONNECT_ONLY situations */
dead_connection = TRUE;
/* treat the connection as aborted in CONNECT_ONLY situations */
aborted = TRUE;
/* temporarily attach the connection to this transfer handle for the
disconnect and shutdown */
Curl_attach_connection(data, conn);
if(conn->handler && conn->handler->disconnect)
/* This is set if protocol-specific cleanups should be made */
conn->handler->disconnect(data, conn, dead_connection);
conn_shutdown(data);
/* detach it again */
Curl_detach_connection(data);
conn_free(data, conn);
Curl_conncache_disconnect(data, conn, aborted);
}
/*
@ -824,6 +798,7 @@ static bool prune_if_dead(struct connectdata *conn,
* any time (HTTP/2 PING for example), the protocol handler needs
* to install its own `connection_check` callback.
*/
DEBUGF(infof(data, "connection has input pending, not reusable"));
dead = TRUE;
}
Curl_detach_connection(data);
@ -881,8 +856,8 @@ static void prune_dead_connections(struct Curl_easy *data)
/* connection previously removed from cache in prune_if_dead() */
/* disconnect it */
Curl_disconnect(data, pruned, TRUE);
/* disconnect it, do not treat as aborted */
Curl_disconnect(data, pruned, FALSE);
}
CONNCACHE_LOCK(data);
data->state.conn_cache->last_cleanup = now;
@ -1294,8 +1269,8 @@ ConnectionExists(struct Curl_easy *data,
infof(data, "Multiplexed connection found");
}
else if(prune_if_dead(check, data)) {
/* disconnect it */
Curl_disconnect(data, check, TRUE);
/* disconnect it, do not treat as aborted */
Curl_disconnect(data, check, FALSE);
continue;
}
@ -3333,7 +3308,7 @@ static void reuse_conn(struct Curl_easy *data,
/* reuse init */
existing->bits.reuse = TRUE; /* yes, we're reusing here */
conn_free(data, temp);
Curl_conn_free(data, temp);
}
/**
@ -3678,7 +3653,7 @@ static CURLcode create_conn(struct Curl_easy *data,
if(!connections_available) {
infof(data, "No connections available.");
conn_free(data, conn);
Curl_conn_free(data, conn);
*in_connect = NULL;
result = CURLE_NO_CONNECTION_AVAILABLE;