From e32c3008849328cfca9b1124c634d1c9ae946d44 Mon Sep 17 00:00:00 2001 From: NathanFallet Date: Sat, 24 Oct 2026 10:03:23 +0100 Subject: [PATCH 1/2] fix: never replay the webhook test call after a 5xx or a lost connection POST /webhook-endpoints/{id}/test sends a real webhook.test event and the API ignores its Idempotency-Key, so the automatic retry could send a second event. The call is now retried on a 429 only (answered before the event leaves), and on: the connection is refused or times out while connecting. Other calls keep their retries. --- .../com/controlresell/sdk/ControlResell.java | 13 +++++- .../java/com/controlresell/sdk/Retrier.java | 21 +++++++++- .../controlresell/sdk/ControlResellTest.java | 42 +++++++++++++++++++ 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/controlresell/sdk/ControlResell.java b/src/main/java/com/controlresell/sdk/ControlResell.java index f74aa7a..08165ac 100644 --- a/src/main/java/com/controlresell/sdk/ControlResell.java +++ b/src/main/java/com/controlresell/sdk/ControlResell.java @@ -160,6 +160,11 @@ public T call(boolean write, HttpCall call) { return retrier.execute(write, call); } + /** Like {@link #call} for a write the API does not deduplicate: see {@link Retrier#execute(boolean, boolean, HttpCall)}. */ + private T callOnce(HttpCall call) { + return retrier.execute(true, false, call); + } + /** The seller the API key acts for. */ public MeResource me() { return new MeResource(); } @@ -591,8 +596,12 @@ public WebhookSecret rotateSecret(UUID id, Long expiresInHours, String idempoten return call(false, h -> api.rotateWebhookSecret(id, key, body)); } - /** Sends a {@code webhook.test} event to the endpoint now and returns how it answered. */ - public WebhookTestResult test(UUID id) { return call(true, h -> api.testWebhookEndpoint(id, h)); } + /** + * Sends a {@code webhook.test} event to the endpoint now and returns how it answered. The API ignores the + * {@code Idempotency-Key} of a test, so it is never replayed after a 5xx or a lost connection (only on a 429 or a + * failed connect): a replay would send a second event. + */ + public WebhookTestResult test(UUID id) { return callOnce(h -> api.testWebhookEndpoint(id, h)); } /** One page of the deliveries of an endpoint over the last 7 days, the most recent first; filters may be {@code null}. */ public WebhookDeliveryPage deliveries(UUID id, Integer limit, String cursor, String status, String type) { diff --git a/src/main/java/com/controlresell/sdk/Retrier.java b/src/main/java/com/controlresell/sdk/Retrier.java index 1a85a5a..81f0b78 100644 --- a/src/main/java/com/controlresell/sdk/Retrier.java +++ b/src/main/java/com/controlresell/sdk/Retrier.java @@ -27,6 +27,15 @@ final class Retrier { } T execute(boolean write, ControlResell.HttpCall call) { + return execute(write, true, call); + } + + /** + * @param replayable false for a call that sends something real and whose {@code Idempotency-Key} the API ignores + * (the webhook test): it is retried on a 429 and on a failed connection only, since any other failure may come + * after the request reached the server + */ + T execute(boolean write, boolean replayable, ControlResell.HttpCall call) { Map headers = new HashMap<>(); if (write) headers.put(IDEMPOTENCY_KEY_HEADER, UUID.randomUUID().toString()); for (int attempt = 0; ; attempt++) { @@ -35,17 +44,25 @@ T execute(boolean write, ControlResell.HttpCall call) { return call.call(headers); } catch (ApiException e) { if (e.getCode() == 0) { - if (last) throw new ControlResellError.Connection(e.getCause() != null ? e.getCause() : e); + if (last || (!replayable && !failedBeforeSending(e))) { + throw new ControlResellError.Connection(e.getCause() != null ? e.getCause() : e); + } pause(policy.backoff(attempt)); continue; } - if (last || !shouldRetry(e)) throw Errors.of(e, mapper); + if (last || (!replayable && e.getCode() != 429) || !shouldRetry(e)) throw Errors.of(e, mapper); Long retryAfter = Errors.retryAfter(e); pause(retryAfter != null ? policy.capped(Duration.ofSeconds(retryAfter)) : policy.backoff(attempt)); } } } + /** A refused connection or a connect timeout: nothing reached the server. */ + private static boolean failedBeforeSending(ApiException e) { + Throwable cause = e.getCause(); + return cause instanceof java.net.ConnectException || cause instanceof java.net.http.HttpConnectTimeoutException; + } + private boolean shouldRetry(ApiException e) { if (RETRIED_STATUSES.contains(e.getCode())) return true; return e.getCode() == 409 && IDEMPOTENCY_IN_PROGRESS.equals(Errors.of(e, mapper).code()); diff --git a/src/test/java/com/controlresell/sdk/ControlResellTest.java b/src/test/java/com/controlresell/sdk/ControlResellTest.java index e27449e..1e1eaf6 100644 --- a/src/test/java/com/controlresell/sdk/ControlResellTest.java +++ b/src/test/java/com/controlresell/sdk/ControlResellTest.java @@ -505,4 +505,46 @@ void webhooksTestPostsWithAKeyAndReadsTheResult() throws Exception { assertEquals("/api/public/v1/webhook-endpoints/" + ENDPOINT_ID + "/test", exchanges.get(0).getRequestURI().getPath()); assertFalse(keys.get(0) == null || keys.get(0).isEmpty()); } + + @Test + void webhooksTestIsNeverReplayedAfterA5xxButIsAfterA429() throws Exception { + for (int status : new int[] {500, 502, 503, 504}) { + exchanges.clear(); + ControlResell sdk = client(n -> new Reply(status, "{\"error\":{\"code\":\"unavailable\",\"message\":\"m\",\"params\":{},\"requestId\":\"r\"}}")); + assertThrows(ControlResellError.class, () -> sdk.webhooks().test(java.util.UUID.fromString(ENDPOINT_ID))); + assertEquals(1, exchanges.size(), "status " + status + " must not be replayed"); + server.stop(0); + } + exchanges.clear(); + ControlResell busy = client(n -> n == 1 + ? new Reply(429, "{\"error\":{\"code\":\"webhook_endpoint_busy\",\"message\":\"m\",\"params\":{},\"requestId\":\"r\"}}", "Retry-After", "0") + : new Reply(200, "{\"success\":true,\"statusCode\":200,\"durationMs\":12,\"error\":null}")); + assertTrue(busy.webhooks().test(java.util.UUID.fromString(ENDPOINT_ID)).getSuccess()); + assertEquals(2, exchanges.size()); + server.stop(0); + exchanges.clear(); + ControlResell other = client(n -> n == 1 + ? new Reply(503, "{\"error\":{\"code\":\"unavailable\",\"message\":\"m\",\"params\":{},\"requestId\":\"r\"}}") + : new Reply(200, ME)); + other.me().get(); + assertEquals(2, exchanges.size(), "other calls keep the retry on 503"); + } + + @Test + void webhooksTestIsNotReplayedAfterALostConnection() throws Exception { + server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); + java.util.concurrent.atomic.AtomicInteger hits = new java.util.concurrent.atomic.AtomicInteger(); + server.createContext("/", exchange -> { + hits.incrementAndGet(); + exchange.close(); + }); + server.start(); + ControlResell sdk = ControlResell.builder() + .apiKey(KEY) + .baseUrl("http://127.0.0.1:" + server.getAddress().getPort()) + .retry(new RetryPolicy(2, Duration.ofMillis(1), Duration.ofMillis(5))) + .build(); + assertThrows(ControlResellError.Connection.class, () -> sdk.webhooks().test(java.util.UUID.fromString(ENDPOINT_ID))); + assertEquals(1, hits.get()); + } } From 73d746aac1f5dd232d66b6229e7a4d7a206c7fb1 Mon Sep 17 00:00:00 2001 From: NathanFallet Date: Sat, 24 Oct 2026 10:07:21 +0100 Subject: [PATCH 2/2] fix: close the remaining replay paths of the webhook test call Kotlin: the 409 idempotency_in_progress retry skips the webhook test. Java: no retry on a network error for that call, since the JDK client reports some failures after the send as a ConnectException. --- .../java/com/controlresell/sdk/ControlResell.java | 3 +-- src/main/java/com/controlresell/sdk/Retrier.java | 13 ++++--------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/controlresell/sdk/ControlResell.java b/src/main/java/com/controlresell/sdk/ControlResell.java index 08165ac..a4e058c 100644 --- a/src/main/java/com/controlresell/sdk/ControlResell.java +++ b/src/main/java/com/controlresell/sdk/ControlResell.java @@ -598,8 +598,7 @@ public WebhookSecret rotateSecret(UUID id, Long expiresInHours, String idempoten /** * Sends a {@code webhook.test} event to the endpoint now and returns how it answered. The API ignores the - * {@code Idempotency-Key} of a test, so it is never replayed after a 5xx or a lost connection (only on a 429 or a - * failed connect): a replay would send a second event. + * {@code Idempotency-Key} of a test, so it is never replayed after a 5xx or a network error (only on a 429): a replay would send a second event. */ public WebhookTestResult test(UUID id) { return callOnce(h -> api.testWebhookEndpoint(id, h)); } diff --git a/src/main/java/com/controlresell/sdk/Retrier.java b/src/main/java/com/controlresell/sdk/Retrier.java index 81f0b78..8ded611 100644 --- a/src/main/java/com/controlresell/sdk/Retrier.java +++ b/src/main/java/com/controlresell/sdk/Retrier.java @@ -32,8 +32,9 @@ T execute(boolean write, ControlResell.HttpCall call) { /** * @param replayable false for a call that sends something real and whose {@code Idempotency-Key} the API ignores - * (the webhook test): it is retried on a 429 and on a failed connection only, since any other failure may come - * after the request reached the server + * (the webhook test): it is retried on a 429 only, since any other failure, a network error included (the JDK + * client reports some failures after the send as a {@code ConnectException}), may come after the request + * reached the server */ T execute(boolean write, boolean replayable, ControlResell.HttpCall call) { Map headers = new HashMap<>(); @@ -44,7 +45,7 @@ T execute(boolean write, boolean replayable, ControlResell.HttpCall call) return call.call(headers); } catch (ApiException e) { if (e.getCode() == 0) { - if (last || (!replayable && !failedBeforeSending(e))) { + if (last || !replayable) { throw new ControlResellError.Connection(e.getCause() != null ? e.getCause() : e); } pause(policy.backoff(attempt)); @@ -57,12 +58,6 @@ T execute(boolean write, boolean replayable, ControlResell.HttpCall call) } } - /** A refused connection or a connect timeout: nothing reached the server. */ - private static boolean failedBeforeSending(ApiException e) { - Throwable cause = e.getCause(); - return cause instanceof java.net.ConnectException || cause instanceof java.net.http.HttpConnectTimeoutException; - } - private boolean shouldRetry(ApiException e) { if (RETRIED_STATUSES.contains(e.getCode())) return true; return e.getCode() == 409 && IDEMPOTENCY_IN_PROGRESS.equals(Errors.of(e, mapper).code());