Fix crash when receiving 2xx response before stream is complete. (#591)

Motivation

It's totally acceptable for a HTTP server to respond before a request
upload has completed. If the response is an error, we should abort the
upload (and we do), but if the response is a 2xx we should probably just
finish the upload.

In this case it turns out we'll actually hit a crash when we attempt to
deliver an empty body message. his is no good!

Once that bug was fixed it revealed another: while we'd attempted to
account for this case, we hadn't tested it, and so it turns out that
shutdown would hang. As a result, I've also cleaned that up.

Modifications

- Tolerate empty circular buffers of bytes when streaming an upload.
- Notify the connection that the task is complete when we're done.

Result

Fewer crashes and hangs.
This commit is contained in:
Cory Benfield
2022-06-07 12:31:57 +01:00
committed by GitHub
parent 3fcd67061f
commit 2483e08ffb
5 changed files with 98 additions and 4 deletions
@@ -267,6 +267,10 @@ final class HTTP1ClientChannelHandler: ChannelDuplexHandler {
writePromise.futureResult.whenComplete { result in
switch result {
case .success:
// If our final action was `sendRequestEnd`, that means we've already received
// the complete response. As a result, once we've uploaded all the body parts
// we need to tell the pool that the connection is idle.
self.connection.taskCompleted()
oldRequest.succeedRequest(buffer)
case .failure(let error):
oldRequest.fail(error)