-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Span-based HashAlgorithm fails seemingly randomly #27088
Comments
On macOS at least, it appears that If I do this: var bag = new ConcurrentBag<string>();
var plo = new ParallelOptions { MaxDegreeOfParallelism = 4 };
var builder = new SubjectIdentityBuilder();
Parallel.For(0, 1_000, plo, () => new List<string>(), (i, pls, list) => {
list.Add(builder.ComputeSubjectIdentityValue("kevin@vcsjones.com"));
return list;
}, list => {
list.ForEach(bag.Add);
});
var distinct = bag.Distinct().Count();
Console.WriteLine(distinct); Which uses the lock (this.sha)
{
if (!this.sha.TryComputeHash(hashInput, hash, out var _))
{
throw new CryptographicException($"Failed to compute hash for '{email}'");
}
} then I start getting a correct |
I don't think In .NET Framework it was just a nice way of combining |
Seems that way. These days there is @mrmartan your issue is likely because sharing a hashing instance is not thread safe. |
Thanks for the info. I half suspected the issue would be thread-safety. Can't tell what led me to believe HashAlgorithm was thread-safe. the documentation states non-static members are not guaranteed to be thread-safe. The HashProvider seems to be the way to go. All I was able to find were the sources for the @vcsjones Can you point me in the right direction? |
Sorry, I didn't mean to say that |
Oh, ok. Seems usable. It's a bummer though (if I understand you correctly) that there is no thread-safe implementation of SHA hashing algorithms in .NET available. |
I've recently noticed that .NET Core 2.1 span-based hash computation sometimes generates a result of
0000000000000000000000000000000000000000000000000000000000000000
. It does not depend on the input and repeated runs with the same input do produce correct results. I also noticed this is happening only on Linux (docker container based on microsoft/dotnet:2.1.2-aspnetcore-runtime and Microsoft.AspNetCore.App 2.1.2), or rather it did not happen on Windows for me.According to application logs this is happening in 1 out of 10k cases. Access to the instance of the class is multi-threaded.
The text was updated successfully, but these errors were encountered: