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

Fix SslStreamCertificateContext OCSP test failures #100644

Conversation

rzikm
Copy link
Member

@rzikm rzikm commented Apr 4, 2024

Fixes #97836.
Fixes #97779.

This PR fixes two data races and adds some internal logging (leftover from debugging, but I figured they can become useful in case we need to debug issues in this area in the future).

First data race was because of following pattern

if (_pendingTask == null)
{
    _pendingTask = DoStuffAsync();
}

//...
Task DoStuffAsync()
{
    // ....
    await SomeOtherTaskThatNeverCompletesSynchronously()
    // ...
    // done, remove the pending task
    _pendingTask = null;
}

Both assignments to _pendingTask are racing and some of the test failures in the past were due to _pendingTask = null assignment finishing first.

Second data race was test-only. Consider the function

        internal byte[]? GetOcspResponseNoWaiting()
        {
            try
            {
                ValueTask<byte[]?> task = GetOcspResponseAsync();

                if (task.IsCompletedSuccessfully)
                {
                    if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"Got OCSP response.");
                    return task.Result;
                }
            }
            catch
            {
            }

            if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, "No OCSP response available.");
            return null;
        }

The test assumed that if GetOcspResponseAsync does not finish synchronously (e.g. no cached OCSP response and new one is being downloaded), the method will always return null. However, it is possible that the task finishes between returning from GetOcspResponseAsync and checking task.IsCompletedSuccessfully. The tests were adjusted to not depend on this.

@rzikm rzikm requested a review from bartonjs April 4, 2024 17:17
Comment on lines +255 to +259
TaskCompletionSource<byte[]?> completionSource = new TaskCompletionSource<byte[]?>();

ArraySegment<char> rentedChars = UrlBase64Encoding.RentEncode(encoded);
byte[]? ret = null;
_pendingDownload = completionSource.Task;
FetchOcspAsyncCore(completionSource);
return completionSource.Task;
Copy link
Member

Choose a reason for hiding this comment

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

Does anything bad happen if something starts waiting on _pendingDownload while the TCS's task is still PendingActivation?

Copy link
Member Author

Choose a reason for hiding this comment

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

I can't think of anything bad happening, if the TCS wasn't completed yet, then the behavior should be equivalent to waiting for a regular Task returned from an async method (rest of the calling async method being as continuation etc.)

@rzikm
Copy link
Member Author

rzikm commented Apr 5, 2024

/azp run runtime-libraries-coreclr outerloop

@rzikm
Copy link
Member Author

rzikm commented Apr 5, 2024

/azp list

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link

CI/CD Pipelines for this repository:

@rzikm
Copy link
Member Author

rzikm commented Apr 5, 2024

/azp run runtime-coreclr jitstress

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rzikm
Copy link
Member Author

rzikm commented Apr 5, 2024

/azp run runtime-coreclr jitstressregs

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rzikm
Copy link
Member Author

rzikm commented Apr 8, 2024

CI looks good the reenabled tests did not fail again and other failures are unrelated
@bartonjs PTAL.

@rzikm rzikm merged commit c1a4c9c into dotnet:main Apr 8, 2024
120 of 131 checks passed
matouskozak pushed a commit to matouskozak/runtime that referenced this pull request Apr 30, 2024
* Fix SslStreamCertificateContext OCSP test failures

* Fix code style

* Remove unwanted change

* fixup! Fix code style

* fixup! Remove unwanted change
@github-actions github-actions bot locked and limited conversation to collaborators May 9, 2024
@karelz karelz added this to the 9.0.0 milestone May 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.