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

System.InvalidOperationException: 'ValueFactory attempted to access the Value property of this instance.' #33

Open
aaelick opened this issue Mar 27, 2023 · 4 comments

Comments

@aaelick
Copy link

aaelick commented Mar 27, 2023

secp256k1.PublicKeyCreate(publicKey, privateKey);
Error if using this on multiple threads.

@zone117x
Copy link
Owner

Can you share some sample code to reproduce this?

@zone117x
Copy link
Owner

Seems like a potential problem with this line

}, isThreadSafe: false);

@aaelick
Copy link
Author

aaelick commented Mar 27, 2023

Seems like a potential problem with this line

}, isThreadSafe: false);

Looks like it.

@noname22
Copy link

noname22 commented Sep 20, 2023

Any updates on this?

It works fine in 1.0.0 and below. 1.0.1 and above, however, generates the InvalidOperationException.

Here's an example to reproduce the issue, if that helps (.NET 7, Console Application).

using Secp256k1Net;

int nJobs = 2;
bool done = false;
int nPublicKeysCreated = 0;

var tasks = Enumerable.Range(0, nJobs)
    .Select(x => Task.Run(Worker))
    .ToArray();

Console.WriteLine("Creating public keys, press enter to stop");
Console.ReadLine();

done = true;

Console.WriteLine($"{nPublicKeysCreated:N00} public keys created");

try
{
    Task.WaitAll(tasks);
}
catch(AggregateException ex)
{
    Console.WriteLine(ex.ToString());
}

void Worker()
{
    var secp256k1 = new Secp256k1();
    var random = new Random();

    while (!done)
    {
        var privateKey = new byte[32];
        var publicKey = new byte[64];

        random.NextBytes(privateKey);

        if (secp256k1.PublicKeyCreate(publicKey, privateKey))
        {
            Interlocked.Increment(ref nPublicKeysCreated);
        }
    }
}

Output with Secp256k1.Net 1.0.0 / Secp256k1.Native 0.1.23:

Creating public keys, press enter to stop

137,593 public keys created

Output with Secp256k1.Net 1.2.0 / Secp256k1.Native 0.1.23:

Creating public keys, press enter to stop

46,547 public keys created
System.AggregateException: One or more errors occurred. (ValueFactory attempted to access the Value property of this instance.)
 ---> System.InvalidOperationException: ValueFactory attempted to access the Value property of this instance.
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.CreateValue()
   at Secp256k1Net.Secp256k1..ctor(ErrorCallbackDelegate errorCallback)
   at Program.<>c__DisplayClass0_0.<<Main>$>g__Worker|1() in E:\code\Secp256k1-ThreadingError\Program.cs:line 29
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.WaitAllCore(Task[] tasks, Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.WaitAll(Task[] tasks)
   at Program.<Main>$(String[] args) in E:\code\Secp256k1-ThreadingError\Program.cs:line 20

@zone117x zone117x mentioned this issue Dec 8, 2023
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

No branches or pull requests

3 participants