Skip to content

OkHttpFutureCallback may leave CompletableFuture incomplete after callback failure in onResponse #1595

Description

@rapiranet

Describe the bug

OkHttpFutureCallback.onResponse() does not complete its CompletableFuture when response processing fails with an exception other than TelegramApiRequestException.

In particular, ResponseBody.string() may throw an IOException, for example SocketException: Socket closed. The exception escapes from onResponse(), but the future is neither completed normally nor exceptionally.

Synchronous calls waiting in AbstractTelegramClient.execute() may therefore block indefinitely on CompletableFuture.get().

To reproduce

  1. Create an OkHttpFutureCallback.
  2. Pass it a successful HTTP response whose ResponseBody throws SocketException while being read.
  3. Invoke onResponse().
  4. Observe that the exception escapes and the future remains incomplete.

A regression test reproducing this behavior is included in the proposed fix.

Expected behavior

Every response-processing failure should complete the future exceptionally. No execution path from onResponse() should leave the future incomplete.

Environment

  • TelegramBots: 10.1.1
  • Java: 17
  • Affected component: telegrambots-client

Additional context

OkHttp has already delivered the call to onResponse(), so onFailure() is not subsequently invoked for an exception thrown while processing that response.

The proposed fix catches response-processing exceptions, completes the future exceptionally, and adds a regression test using SocketException: Socket closed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions