avoid race condition

This commit is contained in:
sneurlax 2024-02-14 11:51:59 -06:00
parent 98c095b568
commit e979a352fb

View file

@ -848,6 +848,9 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
ElectrumxChainHeightService.timeStarted[cryptoCurrency.coin] = ElectrumxChainHeightService.timeStarted[cryptoCurrency.coin] =
subscriptionCreationTime; subscriptionCreationTime;
// Doublecheck to avoid race condition.
if (ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] ==
null) {
// Set stream subscription. // Set stream subscription.
ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] = ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] =
subscription.responseStream.asBroadcastStream().listen((event) { subscription.responseStream.asBroadcastStream().listen((event) {
@ -876,15 +879,16 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
level: LogLevel.Error); level: LogLevel.Error);
} }
}); });
} else { }
}
// A subscription already exists. // A subscription already exists.
// //
// Resume the stream subscription if it's paused. // Resume the stream subscription if it's paused.
if (ElectrumxChainHeightService if (ElectrumxChainHeightService
.subscriptions[cryptoCurrency.coin]!.isPaused) { .subscriptions[cryptoCurrency.coin]!.isPaused) {
// If it's paused, resume it. // If it's paused, resume it.
ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin]! ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin]!.resume();
.resume();
} }
// Causes synchronization to stall. // Causes synchronization to stall.
@ -913,7 +917,6 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
// Retry/recurse. // Retry/recurse.
return await _manageChainHeightSubscription(); return await _manageChainHeightSubscription();
} }
}
// Check if the subscription has been running for too long. // Check if the subscription has been running for too long.
if (ElectrumxChainHeightService.timeStarted[cryptoCurrency.coin] != null) { if (ElectrumxChainHeightService.timeStarted[cryptoCurrency.coin] != null) {