Ensure S3Service is STARTED when creating client (#128026)

It's possible for another component to request a S3 client after the
node has started to shut down, and today the `S3Service` will dutifully
attempt to create a fresh client instance even if it is closed. Such
clients will then leak, resulting in test failures.

With this commit we refuse to create new S3 clients once the service has
started to shut down.
This commit is contained in:
David Turner 2025-05-12 11:20:32 +01:00 committed by GitHub
parent 41613e6810
commit 0f9c1ead9b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -37,6 +37,7 @@ import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.DnsResolver;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.store.AlreadyClosedException;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.cluster.coordination.stateless.StoreHeartbeatService;
import org.elasticsearch.cluster.metadata.RepositoryMetadata;
@ -176,6 +177,13 @@ class S3Service extends AbstractLifecycleComponent {
if (existing != null && existing.tryIncRef()) {
return existing;
}
if (lifecycle.started() == false) {
// doClose() calls releaseCachedClients() which is also synchronized (this) so if we're STARTED here then the client we
// create will definitely not leak on close.
throw new AlreadyClosedException("S3Service is in state [" + lifecycle + "]");
}
final SdkHttpClient httpClient = buildHttpClient(clientSettings, getCustomDnsResolver());
Releasable toRelease = httpClient::close;
try {