Skip to content

Don't cache CanReuse value #35575

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
Aug 24, 2021
Merged

Don't cache CanReuse value #35575

merged 2 commits into from
Aug 24, 2021

Conversation

sebastienros
Copy link
Member

@sebastienros sebastienros commented Aug 20, 2021

Fixes #34768

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Worst case is that Http2 streams are pooled less often.

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

public bool CanReuse { get; private set; }
// We only want to reuse a stream that was not aborted and has completely finished writing.
// This ensures Http2OutputProducer.ProcessDataWrites is in the correct state to be reused.
public bool CanReuse => !_connectionAborted && HasResponseCompleted;

protected override void OnReset()
Copy link
Member

Choose a reason for hiding this comment

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

Would resetting the value to false in OnReset accomplish the same goal?

Copy link
Member

Choose a reason for hiding this comment

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

The line on 44 was effectively doing that before. The issue was that !_connectionAborted && HasResponseCompleted was being evaluated too early on a background thread and a RST_STREAM frame could still be processed and abort the stream before pooling. Now CanReuse is evaluated once in the main frame-processing loop right before it's pooled or not.

Copy link
Member

@JamesNK JamesNK left a comment

Choose a reason for hiding this comment

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

Please add a unit test. This is subtle behavior and we want to ensure it isn't regressed in the future.

Where is _connectionAborted set to true? Will it always happen before the stream is pooled? I want to make sure there is no chance that a stream is pooled and then some code aborts it while it is in the pool.

@sebastienros
Copy link
Member Author

sebastienros commented Aug 20, 2021

Where is _connectionAborted set to true?

When the RST_STREAM is processed.

Edit:
https://github.com/dotnet/aspnetcore/blob/main/src/Servers/Kestrel/Core/src/Internal/Http2/Http2Connection.cs#L731

@halter73
Copy link
Member

Please add a unit test.

I would also love that if you find a good way to do it @JamesNK. The problem is the RST_STREAM frame needs to be processed between Http2Stream.CompleteStream(bool) executing on a background thread and Http2Connection.UpdateCompletedStreams() running again.

This would be easy enough to do by adding some sort of event/resettable-Task we could hook in the tests when Http2Connection.OnStreamCompleted() is called and only setting the RST_STREAM frame then, but the problem there is that UpdateCompletedStreams() runs immediately after await Input.ReadAsync() completes without processing any incoming frames. So we'd also need a way to force processing frames without calling UpdateCompletedStreams() to simulate Http2Connection frame-processing loop running concurrently with Http2Stream.CompleteStream(bool). I'm not sure it's worth it.

@JamesNK
Copy link
Member

JamesNK commented Aug 21, 2021

Ok. If we can't add a unit test then we should comment. Something like:

CanReuse must be evaluated on the main frame-processing looping after the stream is removed from the connection's active streams collection. This is required because a RST_STREAM frame could arrive after the END_STREAM flag is received. Only once the stream is removed from the connection's active stream collection can no longer be reset, and is safe to evaluate for pooling.

@davidfowl
Copy link
Member

Nice catch!

@davidfowl
Copy link
Member

Why is this is main and not rc1?

@sebastienros sebastienros requested a review from JamesNK August 24, 2021 21:28
@sebastienros sebastienros enabled auto-merge (squash) August 24, 2021 21:30
@sebastienros sebastienros disabled auto-merge August 24, 2021 21:33
@sebastienros
Copy link
Member Author

@davidfowl merge to main, then backport to rc1 (standard procedure I believe)

@sebastienros sebastienros enabled auto-merge (squash) August 24, 2021 22:06
@sebastienros
Copy link
Member Author

/backport to release/6.0-rc1

@github-actions
Copy link
Contributor

Started backporting to release/6.0-rc1: https://github.com/dotnet/aspnetcore/actions/runs/1164353234

@sebastienros sebastienros merged commit bcdb82d into main Aug 24, 2021
@sebastienros sebastienros deleted the sebros/http2streamreuse branch August 24, 2021 23:03
@ghost ghost added this to the 7.0-preview1 milestone Aug 24, 2021
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Kestrel reset error when no reset received
6 participants