Skip to content

Commit bb71676

Browse files
nan01abshargonJimmy
authored
fix: concurrency conflict in NEP6Wallet.ToJson (#3527)
* fix: concurrency conflict in NEP6Wallet.ToJson * Update also ChangePasssword * Reduce lock time --------- Co-authored-by: Shargon <[email protected]> Co-authored-by: Jimmy <[email protected]>
1 parent 3f2f78c commit bb71676

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/Neo/Wallets/NEP6/NEP6Wallet.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,18 @@ public override WalletAccount Import(string nep2, string passphrase, int N = 163
293293
/// </summary>
294294
public JObject ToJson()
295295
{
296+
NEP6Account[] accountValues;
297+
lock (accounts)
298+
{
299+
accountValues = accounts.Values.ToArray();
300+
}
301+
296302
return new()
297303
{
298304
["name"] = name,
299305
["version"] = version.ToString(),
300306
["scrypt"] = Scrypt.ToJson(),
301-
["accounts"] = accounts.Values.Select(p => p.ToJson()).ToArray(),
307+
["accounts"] = accountValues.Select(p => p.ToJson()).ToArray(),
302308
["extra"] = extra
303309
};
304310
}
@@ -345,26 +351,28 @@ private bool VerifyPasswordInternal(string password)
345351
public override bool ChangePassword(string oldPassword, string newPassword)
346352
{
347353
bool succeed = true;
354+
NEP6Account[] accountsValues;
348355
lock (accounts)
349356
{
350-
Parallel.ForEach(accounts.Values, (account, state) =>
351-
{
352-
if (!account.ChangePasswordPrepare(oldPassword, newPassword))
353-
{
354-
state.Stop();
355-
succeed = false;
356-
}
357-
});
357+
accountsValues = accounts.Values.ToArray();
358358
}
359+
Parallel.ForEach(accountsValues, (account, state) =>
360+
{
361+
if (!account.ChangePasswordPrepare(oldPassword, newPassword))
362+
{
363+
state.Stop();
364+
succeed = false;
365+
}
366+
});
359367
if (succeed)
360368
{
361-
foreach (NEP6Account account in accounts.Values)
369+
foreach (NEP6Account account in accountsValues)
362370
account.ChangePasswordCommit();
363371
password = newPassword.ToSecureString();
364372
}
365373
else
366374
{
367-
foreach (NEP6Account account in accounts.Values)
375+
foreach (NEP6Account account in accountsValues)
368376
account.ChangePasswordRollback();
369377
}
370378
return succeed;

0 commit comments

Comments
 (0)