async-http-client icon indicating copy to clipboard operation
async-http-client copied to clipboard

AHC causes uncleanShutdown error on server side.

Open toffaletti opened this issue 10 months ago • 3 comments

I have a server that is functionally similar in setup to hummingbird-examples/http2, when I use async-http-client to make a request with a command line program that exits after the request, humming bird reports this error:

[HummingbirdHTTP2] Error handling inbound connection for HTTP2 handler: uncleanShutdown

I do not see the same error when using curl:

curl --cacert certs/ca.crt --http2 -v https://localhost:8043/healthcheck
    var tlsConfiguration: TLSConfiguration {
        get throws {
            let certificateChain = try NIOSSLCertificate.fromPEMFile(self.certificateChain)
            var tlsConfiguration = TLSConfiguration.makeClientConfiguration()
            tlsConfiguration.trustRoots = .certificates(certificateChain)
            #if os(macOS)
            tlsConfiguration.certificateVerification = .none
            #else
            // not supported with Network.framework
            tlsConfiguration.certificateVerification = .noHostnameVerification
            #endif
            return tlsConfiguration
        }
    }

relevant code in the run() async throws method of an AsyncParsableCommand:

        var clientConfig = HTTPClient.Configuration()
        clientConfig.tlsConfiguration = try self.tlsConfiguration
        try await HTTPClient.withHTTPClient(configuration: clientConfig) { client in
            var request = HTTPClientRequest(url: url.absoluteString)
            request.method = .POST
            request.headers.add(name: "content-type", value: "application/json")
            request.headers.add(name: "content-length", value: "\(payloadData.count)")
            request.body = .bytes(payloadData)
            let response = try await client.execute(request, timeout: .seconds(30))
            let body = try await response.body.collect(upTo: 10 * 1024 * 1024)
        }

I've tried many variations that all execute await client.shutdown() before exiting and they all result in this error. From what Cory says here: https://forums.swift.org/t/niossl-spurious-uncleanshutdown-error/53031/3 it seems like AHC isn't sending CLOSE_NOTIFY on shutdown?

toffaletti avatar Feb 07 '25 18:02 toffaletti

That seems possible, if unfortunate. Generally it won't matter for HTTP (as discussed in the linked thread), but it's definitely rude.

I suspect this only happens using the swift-nio-transport-services backend. Have you tried this with the client on Linux?

Lukasa avatar Feb 10 '25 08:02 Lukasa

You're right, I can't reproduce it using Linux as the client.

toffaletti avatar Feb 12 '25 08:02 toffaletti

Great, ok. So I think this is a problem with shutdown in the NIOTS backend, which does not auto-promote the shutdown to half-closure. I think if we explicitly performed half-closure everywhere, this would resolve matters.

Lukasa avatar Feb 12 '25 09:02 Lukasa