Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 7, 2022

Conversation

Lukasa
Copy link
Collaborator

@Lukasa Lukasa commented Jun 6, 2022

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. This 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.
Resolves #562

@Lukasa Lukasa added the 🔨 semver/patch No public API change. label Jun 6, 2022
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. This 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.
@Lukasa Lukasa force-pushed the cb-crash-receiving-body-parts branch from eb0d918 to 6752103 Compare June 6, 2022 16:10
Copy link
Member

@fabianfett fabianfett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great patch and great fixes. One ask for a small comment.

@@ -267,6 +267,7 @@ final class HTTP1ClientChannelHandler: ChannelDuplexHandler {
writePromise.futureResult.whenComplete { result in
switch result {
case .success:
self.connection.taskCompleted()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding a comment here? Something like:

If our final action is sendRequestEnd this means, that we have already received the complete response. For this reason we must mark the connection as idle to the pool, once we have uploaded all body parts.

Copy link
Collaborator

@dnadoba dnadoba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Lukasa, nice patch, just one super tiny nit.

@@ -1253,6 +1253,37 @@ class HTTPEchoHandler: ChannelInboundHandler {
}
}

class HTTP200DelayedHandler: ChannelInboundHandler {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super tiny nit

Suggested change
class HTTP200DelayedHandler: ChannelInboundHandler {
final class HTTP200DelayedHandler: ChannelInboundHandler {

@Lukasa Lukasa merged commit 2483e08 into swift-server:main Jun 7, 2022
@Lukasa Lukasa deleted the cb-crash-receiving-body-parts branch June 7, 2022 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash while receiving response
3 participants