Skip to content

Conversation

@iSazonov
Copy link
Collaborator

@iSazonov iSazonov commented Jun 4, 2021

PR Summary

Fix #15513

Move HttpKnownHeaderNames initialization to static constructor.

PR Context

See #15513.

PR Checklist

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Jun 4, 2021
@iSazonov iSazonov self-assigned this Jun 4, 2021
@vexx32
Copy link
Collaborator

vexx32 commented Jun 4, 2021

Even with this, the hashset will still be accessed from multiple threads concurrently in the cases given in #15513, won't it? We should probably add a test using the code samples in the issue to verify that this is sufficient to resolve it. Since the user reporting it mentioned that it was a bit intermittent, we should repeat their test a few times in our own tests.

@iSazonov
Copy link
Collaborator Author

iSazonov commented Jun 4, 2021

Even with this, the hashset will still be accessed from multiple threads concurrently in the cases given in #15513, won't it?

A root of the issue was that second thread read the HashSet while first thread still initializes the HashSet.
The HashSet is used only in one place for reading so no need to concern more about race conditions.

We should probably add a test

It's not worth it. We will not be able to make such tests stable and defensible. Please use compiled artifact to check locally.

@dperez83
Copy link

dperez83 commented Jun 4, 2021

Even with this, the hashset will still be accessed from multiple threads concurrently in the cases given in #15513, won't it?

A root of the issue was that second thread read the HashSet while first thread still initializes the HashSet.
The HashSet is used only in one place for reading so no need to concern more about race conditions.

We should probably add a test

It's not worth it. We will not be able to make such tests stable and defensible. Please use compiled artifact to check locally.

If the only writes are the ones when the object is generated :
s_contentHeaderSet.Add(HttpKnownHeaderNames.Allow);
s_contentHeaderSet.Add(HttpKnownHeaderNames.LastModified);
...

I think the write race condition is eliminated with this change.

I am a C# dev, but I have zero knowledge on the Powershell source code. Hope I could help you somehow.

@iSazonov
Copy link
Collaborator Author

iSazonov commented Jun 4, 2021

I am a C# dev, but I have zero knowledge on the Powershell source code. Help I could help you somehow.

@dperez83 PowerShell is great project to improve skills. It's too big to learn it well in a short time but you can pick an area you like best and work there with some workgroup https://devblogs.microsoft.com/powershell/powershell-working-groups/
After monitoring issues and PRs for about a month you will be able to make good contributions.

PowerShell Team
Since we open sourced PowerShell in 2016, PowerShell has been an immensely popular project on GitHub. Every year, 700-1000 PRs and 1300-1500 issues are submitted to the PowerShell repo, with roughly half of the PRs and 90% of the issues filed from the community.

Copy link
Contributor

@PaulHigin PaulHigin left a comment

Choose a reason for hiding this comment

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

LGTM

@ghost ghost added the Review - Needed The PR is being reviewed label Jun 12, 2021
@ghost
Copy link

ghost commented Jun 12, 2021

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@iSazonov
Copy link
Collaborator Author

@daxian-dbw Could you please review?

@ghost ghost removed the Review - Needed The PR is being reviewed label Jun 12, 2021
@iSazonov
Copy link
Collaborator Author

@SteveL-MSFT Could you please approve? It is impossible to merge without green flag :-)

@ghost ghost added the Review - Needed The PR is being reviewed label Jun 25, 2021
@ghost
Copy link

ghost commented Jun 25, 2021

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@dperez83
Copy link

dperez83 commented Aug 4, 2021

I see that there's a pending review from @daxian-dbw
Is there anything else you need from me?

Thank you

@iSazonov iSazonov requested a review from TravisEz13 August 4, 2021 08:28
@iSazonov
Copy link
Collaborator Author

iSazonov commented Aug 4, 2021

@TravisEz13 Please review/approve.

@ghost ghost removed the Review - Needed The PR is being reviewed label Aug 4, 2021
if (s_contentHeaderSet == null)
{
s_contentHeaderSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
// Thread-safe initialization.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Thread-safe initialization.
// Thread-safe initialization because the constructor is only called once per AppDomain.

@TravisEz13 TravisEz13 assigned TravisEz13 and unassigned iSazonov Aug 4, 2021
@TravisEz13 TravisEz13 changed the title Thread-safe HttpKnownHeaderNames initialization Thread-safe HttpKnownHeaderNames initialization Aug 4, 2021
@TravisEz13 TravisEz13 merged commit aaaa0f2 into PowerShell:master Aug 4, 2021
@iSazonov iSazonov deleted the racecondition-in-httpknownheadernames branch August 5, 2021 02:52
@iSazonov iSazonov added this to the 7.2.0-preview.9 milestone Aug 5, 2021
@ghost
Copy link

ghost commented Aug 23, 2021

🎉v7.2.0-preview.9 has been released which incorporates this pull request.:tada:

Handy links:

@ghost ghost mentioned this pull request Aug 23, 2021
@ghost
Copy link

ghost commented Sep 28, 2021

🎉v7.2.0-preview.10 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invoke-RestMethod parallel problem

5 participants