Tinkoff / invest-openapi-java-sdk

Apache License 2.0
167 stars 46 forks source link

Infinity CompletableFuture in case of IOException #91

Closed midnt closed 3 years ago

midnt commented 4 years ago

Для примера следующий метод: ### ru.tinkoff.invest.openapi.okhttp.MarketContextImpl#getMarketCandles

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                try {
                    final RestResponse<HistoricalCandles> result =
                            handleResponse(response, historicalCandlesTypeReference);
                    future.complete(Optional.of(result.payload));
                } catch (OpenApiException ex) {
                    if (ex.getCode().equals(INSTRUMENT_ERROR_MESSAGE_CODE)) {
                        future.complete(Optional.empty());
                    } else {
                        future.completeExceptionally(ex);
                    }
                }
            }

future.completeExceptionally будет вызвана только в случае OpenApiException. Реализация okHttp: okhttp3.RealCall.AsyncCall#execute

      boolean signalledCallback = false;
      transmitter.timeoutEnter();
      try {
        Response response = getResponseWithInterceptorChain();
        signalledCallback = true;
        responseCallback.onResponse(RealCall.this, response);
      } catch (IOException e) {
        if (signalledCallback) {
          // Do not signal the callback twice!
          Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);
        } else {
          responseCallback.onFailure(RealCall.this, e);
        }
      } catch (Throwable t) {

в случае IOException не будет исполнен responseCallback.onFailure и CompletableFuture "зависнет". Okhttp ответ разработчиков https://github.com/square/okhttp/issues/3458 This is working as designed. Use a try/catch block to recover from failures in onResponse().

Подобная проблема есть и в других запросах (не только getMarketCandles)

zlumyo commented 3 years ago

Исправлено