Skip to content
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

Allow connection close for custom streams #1603

Merged
merged 4 commits into from
Sep 2, 2023

Conversation

becheran
Copy link
Contributor

We do use fasthttp for our project as a proxy between an uplink connection and a target. We discovered the following scenario where we run into an issue that we can't solve without a minor change to the fasthttp client.

The scenario that we have is the following:

From the upsteam connection we get a request which is replied with a steam of data from the target. All works fine using fasthttp and the customStream body. We simply copy from the target reader to the upstream writer. Now the upstream suddenly closes the TCP connection and we get an error while trying to copy our data from the target.

Now we definitelly want to close the still open connection to the target. The only thing we can do from within the handler is set fastRequest.SetConnectionClose() and fastResponse.SetConnectionClose(). Though, the TCP connection will not be closed since it is too late when we set those values. The TCP connection is released and eventually re-used by the next upstream connection which would then result into an unexpected error.

If the closeConn check is done again in the newCloseReader wrapper function we could set the ConnectionClose() property from the outside and the connection will be closed once we release our request handler.

Copy link
Collaborator

@erikdubbelboer erikdubbelboer left a comment

Choose a reason for hiding this comment

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

Can you have a look why this makes TestAllocationClient fail? It seems like it causes an allocation that wasn't there before.

@becheran
Copy link
Contributor Author

Interesting. Don't have access to a computer right now. Will have a deeper look next week.

if err != nil {
hc.releaseReader(br)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@erikdubbelboer this is another change that would be required to avoid data races if the connection can be closed from outside. The reader shall only be released directly if it is not a stream connection. Otherwise do it in the cleanup method called via the closer interface.

The reason is that if the reader is realeased earlier, it would then be available for the next request and might cause a data race.

@becheran
Copy link
Contributor Author

@erikdubbelboer I did fix the TestAllocationClient but for the next run the TestStreamBodyRequestContentLength test failed. Though, I can't reproduce this locally and am also not sure how I could have possibly broken this? It is in the server side which I did not touch with the PR. Is this some test that is known to be flaky?

@erikdubbelboer erikdubbelboer merged commit 34e7da1 into valyala:master Sep 2, 2023
9 of 14 checks passed
@erikdubbelboer
Copy link
Collaborator

Thanks! Some tests can be flaky, especially on windows. It all looks good now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants