fix null price feed listener
Some checks are pending
CI / build (macos-13) (push) Waiting to run
CI / build (ubuntu-latest) (push) Waiting to run
CI / build (windows-latest) (push) Waiting to run
Codacy Coverage Reporter / Publish coverage (push) Waiting to run
CodeQL / Analyze (java) (push) Waiting to run

This commit is contained in:
woodser 2024-07-01 08:41:17 -04:00 committed by GitHub
parent 856faafd1c
commit c01d63490f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -33,7 +33,6 @@ import haveno.core.monetary.Price;
import haveno.core.monetary.TraditionalMoney;
import haveno.core.provider.PriceHttpClient;
import haveno.core.provider.ProvidersRepository;
import haveno.core.trade.HavenoUtils;
import haveno.core.trade.statistics.TradeStatistics3;
import haveno.core.user.Preferences;
import haveno.network.http.HttpClient;
@ -144,15 +143,17 @@ public class PriceFeedService {
public void awaitExternalPrices() {
CountDownLatch latch = new CountDownLatch(1);
ChangeListener<? super Number> listener = (observable, oldValue, newValue) -> {
if (hasExternalPrices() && latch.getCount() != 0) latch.countDown();
if (hasExternalPrices()) UserThread.execute(() -> latch.countDown());
};
updateCounter.addListener(listener);
if (hasExternalPrices()) {
updateCounter.removeListener(listener);
return;
UserThread.execute(() -> updateCounter.addListener(listener));
if (hasExternalPrices()) UserThread.execute(() -> latch.countDown());
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
UserThread.execute(() -> updateCounter.removeListener(listener));
}
HavenoUtils.awaitLatch(latch);
updateCounter.removeListener(listener);
}
public boolean hasExternalPrices() {
@ -377,17 +378,21 @@ public class PriceFeedService {
*/
public synchronized Map<String, MarketPrice> requestAllPrices() throws ExecutionException, InterruptedException, TimeoutException, CancellationException {
CountDownLatch latch = new CountDownLatch(1);
ChangeListener<? super Number> listener = (observable, oldValue, newValue) -> { if (latch.getCount() != 0) latch.countDown(); };
updateCounter.addListener(listener);
ChangeListener<? super Number> listener = (observable, oldValue, newValue) -> latch.countDown();
UserThread.execute(() -> updateCounter.addListener(listener));
requestAllPricesError = null;
requestPrices();
UserThread.runAfter(() -> {
if (latch.getCount() == 0) return;
requestAllPricesError = "Timeout fetching market prices within 20 seconds";
latch.countDown();
if (latch.getCount() > 0) requestAllPricesError = "Timeout fetching market prices within 20 seconds";
UserThread.execute(() -> latch.countDown());
}, 20);
HavenoUtils.awaitLatch(latch);
updateCounter.removeListener(listener);
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
UserThread.execute(() -> updateCounter.removeListener(listener));
}
if (requestAllPricesError != null) throw new RuntimeException(requestAllPricesError);
return cache;
}