From 4cce4589334aea408a7883c922b181da41d9d70e Mon Sep 17 00:00:00 2001 From: Nikita Tkachenko Date: Wed, 17 Apr 2024 12:42:49 +0200 Subject: [PATCH] Do not abort CI Visibility spans dispatch on interrupt --- .../communication/http/OkHttpUtils.java | 53 ++++++++++++------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/communication/src/main/java/datadog/communication/http/OkHttpUtils.java b/communication/src/main/java/datadog/communication/http/OkHttpUtils.java index 2cbd59cceae..2a69509f31c 100644 --- a/communication/src/main/java/datadog/communication/http/OkHttpUtils.java +++ b/communication/src/main/java/datadog/communication/http/OkHttpUtils.java @@ -11,6 +11,7 @@ import datadog.trace.util.AgentProxySelector; import java.io.File; import java.io.IOException; +import java.io.InterruptedIOException; import java.net.ConnectException; import java.net.InetSocketAddress; import java.net.Proxy; @@ -356,31 +357,45 @@ public void writeTo(BufferedSink sink) throws IOException { } } + /** + * Retries a request in accordance with the provided retry policy. Important: + * interrupts to a thread executing this method are ignored (the thread's interruption flag is + * restored on exit) + */ public static Response sendWithRetries( OkHttpClient httpClient, HttpRetryPolicy retryPolicy, Request request) throws IOException { - while (true) { - try { - okhttp3.Response response = httpClient.newCall(request).execute(); - if (response.isSuccessful()) { - return response; - } - if (!retryPolicy.shouldRetry(response)) { - return response; - } else { - closeQuietly(response); + boolean interrupted = false; + try { + + while (true) { + try { + Response response = httpClient.newCall(request).execute(); + if (response.isSuccessful()) { + return response; + } + if (!retryPolicy.shouldRetry(response)) { + return response; + } else { + closeQuietly(response); + } + } catch (ConnectException | InterruptedIOException ex) { + if (!retryPolicy.shouldRetry(null)) { + throw ex; + } } - } catch (ConnectException ex) { - if (!retryPolicy.shouldRetry(null)) { - throw ex; + // If we get here, there has been an error, and we still have retries left + long backoffMs = retryPolicy.backoff(); + try { + Thread.sleep(backoffMs); + } catch (InterruptedException e) { + // remember interrupted status to restore the thread's interrupted flag later + interrupted = true; } } - // If we get here, there has been an error, and we still have retries left - long backoffMs = retryPolicy.backoff(); - try { - Thread.sleep(backoffMs); - } catch (InterruptedException e) { + + } finally { + if (interrupted) { Thread.currentThread().interrupt(); - throw new IOException(e); } } }