Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request CompletableFuture will never complete if an exception occurs #677

Open
3 tasks done
RodionKorneev opened this issue Jun 22, 2022 · 0 comments
Open
3 tasks done
Labels

Comments

@RodionKorneev
Copy link
Contributor

RodionKorneev commented Jun 22, 2022

Issue Template Requirements

Issue must be of the specified template and follow these requirements or it will be ignored.

  • Please do not post screenshots of code output
  • Wrap code in a codeblock
  • exhibiting spam behavior or "evil" behavior will have the issue ignored
  • Please make sure your issue hasn't been asked before
  • Please complete the checklist below (mark boxes [ ] with [x])

Pre-Checklist

I made sure...

  • I read the README.md
  • I looked over the examples and wiki
  • my issue hasn't been asked or solved before

Describe the bug

CompletableFuture returned from IGClient.sendRequest(IGRequest<T> req) will never complete if an IOException occurs while processing the response body in ResponseBody.string(). Actually thrown exception does not affect CompletableFuture state and .get() or .join() call will await endlessly.

public <T extends IGResponse> CompletableFuture<T> sendRequest(@NonNull IGRequest<T> req) {
        CompletableFuture<Pair<Response, String>> responseFuture = new CompletableFuture<>();
        log.info("Sending request : {}", req.formUrl(this).toString());
        this.httpClient.newCall(req.formRequest(this)).enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException exception) {
                responseFuture.completeExceptionally(exception);
            }

            @Override
            public void onResponse(Call call, Response res) throws IOException {
                log.info("Response for {} : {}", call.request().url().toString(), res.code());
                try (ResponseBody body = res.body()) {
                    responseFuture.complete(new Pair<>(res, body.string())); //<---- HERE
                }
            }

        });

        return responseFuture
                .thenApply(res -> {
                    setFromResponseHeaders(res.getFirst());
                    log.info("Response for {} with body (truncated) : {}",
                            res.getFirst().request().url(),
                            IGUtils.truncate(res.getSecond()));

                    return req.parseResponse(res);
                })
                .exceptionally((tr) -> {
                    return this.exceptionallyHandler.handle(tr, req.getResponseType());
                });
    }

How to reproduce

  1. Specify steps to reproduce
    You could mock Response parameter and throw IOException on string() call.

Expected behavior

CompletableFuture.get() completes with exception response

Actual behavior

CompletableFuture.get() endlessly await

@RodionKorneev RodionKorneev changed the title Request CompletableFuture will never complete if an exception occurs Request CompletableFuture will never complete if an exception occurs Jun 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant