@@ -523,19 +523,31 @@ struct HTTPRequestStateMachine {
523
523
where head. status. code < 300 :
524
524
525
525
return self . avoidingStateMachineCoW { state -> Action in
526
- let ( remainingBuffer, _) = responseStreamState. end ( )
527
- state = . running(
528
- . streaming( expectedBodyLength: expectedBodyLength, sentBodyBytes: sentBodyBytes, producer: producerState) ,
529
- . endReceived
530
- )
531
- return . forwardResponseBodyParts( remainingBuffer)
526
+ let ( remainingBuffer, connectionAction) = responseStreamState. end ( )
527
+ switch connectionAction {
528
+ case . none:
529
+ state = . running(
530
+ . streaming( expectedBodyLength: expectedBodyLength, sentBodyBytes: sentBodyBytes, producer: producerState) ,
531
+ . endReceived
532
+ )
533
+ return . forwardResponseBodyParts( remainingBuffer)
534
+ case . close:
535
+ // If we receive a `.close` as a connectionAction from the responseStreamState
536
+ // this means, that the response end was signaled by a connection close. Since
537
+ // the request is still uploading, we will not be able to finish the upload. For
538
+ // this reason we can fail the request here.
539
+ state = . failed( HTTPClientError . remoteConnectionClosed)
540
+ return . failRequest( HTTPClientError . remoteConnectionClosed, . close)
541
+ }
532
542
}
533
543
534
544
case . running( . streaming( _, _, let producerState) , . receivingBody( let head, var responseStreamState) ) :
535
545
assert ( head. status. code >= 300 )
536
546
assert ( producerState == . paused, " Expected to have paused the request body stream, when the head was received. Invalid state: \( self . state) " )
537
547
538
548
return self . avoidingStateMachineCoW { state -> Action in
549
+ // We can ignore the connectionAction from the responseStreamState, since the
550
+ // connection should be closed anyway.
539
551
let ( remainingBuffer, _) = responseStreamState. end ( )
540
552
state = . finished
541
553
return . succeedRequest( . close, remainingBuffer)
0 commit comments