XChange icon indicating copy to clipboard operation
XChange copied to clipboard

Binanace getOrders not thread safe

Open m1dnightc0der opened this issue 2 years ago • 0 comments

When calling org.knowm.xchange.binance.service.BinanceTradeService.getOrder via the same BinanceTradeService instance from multiple treads, it periodically returns stale data as the getOrder method is not hitting the rest api, so there must be some class level/shared variable that is preventing org.knowm.xchange.binance.service.BinanceTradeService.getOrder from calling the rest end point via org.knowm.xchange.binance.BinanceAuthenticated.orderStatus.

It is most likely something in this bit of code

    orders.add(
                BinanceAdapters.adaptOrder(
                        orderStatusAllProducts(
                            instrument,
                                BinanceAdapters.id(orderId),
                                null,isMargin), instrument instanceof FuturesContract));
      }

but tock a quick look and nothing obvious came out, ideas welcomed as to what could be the issues, once I find the culprit I can then either synchronise it or wack it in threadlocal.

org.knowm.xchange.binance.service

@Override
  public Collection<Order>  getOrder(OrderQueryParams... params) throws IOException {
    Collection<Order> orders = new ArrayList<>();
    try {

      for (OrderQueryParams param : params) {

        if (!(param instanceof OrderQueryParamInstrument)) {
          throw new ExchangeException(
                  "Parameters must be an instance of OrderQueryParamInstrument");
        }
        OrderQueryParamInstrument orderQueryParamInstrument =
                (OrderQueryParamInstrument) param;
        if (orderQueryParamInstrument.getInstrument() == null
                || orderQueryParamInstrument.getOrderId() == null) {
          throw new ExchangeException(
                  "You need to provide the currency pair and the order id to query an order.");
        }

        String orderId;
        Instrument instrument;
        Boolean isMargin;
        if(orderQueryParamInstrument instanceof BinanceQueryOrderParams){
          BinanceQueryOrderParams binanceOrderQueryParamInstrument = (BinanceQueryOrderParams) orderQueryParamInstrument;
          orderId=binanceOrderQueryParamInstrument.getOrderId();
          instrument=binanceOrderQueryParamInstrument.getInstrument();
          isMargin =binanceOrderQueryParamInstrument.getIsMarginOrder();

        } else{
           orderId=orderQueryParamInstrument.getOrderId();
          instrument=orderQueryParamInstrument.getInstrument();
          isMargin=false;
        }
        orders.add(
                BinanceAdapters.adaptOrder(
                        orderStatusAllProducts(
                            instrument,
                                BinanceAdapters.id(orderId),
                                null,isMargin), instrument instanceof FuturesContract));
      }
      return orders;
    } catch (BinanceException e) {
      throw BinanceErrorAdapter.adapt(e);
    } finally {
      return orders;
    }
  }

m1dnightc0der avatar Mar 25 '23 14:03 m1dnightc0der