LinkedIn Java SDK: Unclear cancellation behavior of asynchronous requests
0 reputation · 19 May 2023, 13:34 UTC
0 reputation · 19 May 2023, 13:34 UTC
Investigate how cancelling an asynchronous LinkedIn API call via the official Java SDK affects the underlying HTTP transaction, response parsing, and connection pool usage.
The SDK’s asynchronous methods return a ListenableFuture that lacks a direct cancel() method, relying instead on the underlying HTTP client’s cancellation mechanics. Documentation does not clarify whether LinkedIn’s server aborts processing when the client closes the connection, nor does it specify the HTTP status code that would be returned for a cancelled request. Additionally, the SDK’s timeout configuration applies only to socket and connect timeouts, with no explicit total request deadline that encompasses retries.
ListenableFuture is cancelled, does the SDK immediately terminate the HTTP request on the wire, or does it continue until a response is received?Cancelling the ListenableFuture returned by the LinkedIn Java SDK's async methods does not reliably abort the HTTP request on the wire. In most SDK/HttpClient combinations, cancel(true) interrupts the waiting thread but the underlying Apache HttpClient request continues until the server responds or the socket timeout fires. There is also no built-in total request deadline that spans retries — you must wrap the future yourself.
Confirmed by general Java/HttpClient behavior:
ListenableFuture.cancel(boolean) only signals the future and (with true) interrupts the executing thread. Whether that closes the socket depends entirely on whether the SDK's internal request class forwards cancellation to the HTTP client's abort mechanism — and the SDK does not document that it does.connectTimeout and socketTimeout settings govern connection establishment and idle-read gaps only. Neither caps total elapsed time across retries.Likely, but unverified for your SDK version:
Futures.withTimeout(...) (or orTimeout on a converted CompletableFuture), and treat expiry as "result unknown" rather than "failed safely".Because behavior varies between SDK 2.x/3.x and the bundled HttpClient version, confirm empirically:
cancel() delegates to the HTTP client's abort/cancel method.sudo tcpdump -i any -w linkedin-cancel.pcap host api.linkedin.comIf you see a TCP FIN/RST immediately after cancel, the connection is aborted; if the exchange continues to a full response, cancellation is local-only.
Which SDK version (and which underlying HTTP client) are you on? If your version's async request class does propagate cancellation to the client, connection-pool impact changes substantially — the tcpdump test above settles it in minutes.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.