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