-
Notifications
You must be signed in to change notification settings - Fork 336
fix(vertx-web): finish vertx.route-handler via RoutingContext.addEndHandler fallback #11312
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
Merged
gh-worker-dd-mergequeue-cf854d
merged 12 commits into
master
from
zarir/sles-2837-vertx-web-finish-route-handler
May 19, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
97f9933
fix(vertx-web): finish vertx.route-handler via RoutingContext.addEndH…
zarirhamza 01aeb85
Apply changes to vertex-web 3.4
rithikanarayan b6dc5d1
Add unit test for 3.x
rithikanarayan 04e85b4
Merge branch 'master' into zarir/sles-2837-vertx-web-finish-route-han…
rithikanarayan 490cd63
Clean up unit test
rithikanarayan 07787a9
Add junit jupiter to gradle setup
rithikanarayan a3b26df
wrap exception handling
rithikanarayan a43cc5b
Adding tests, cleaning up exception handling
rithikanarayan 9598048
cleaning comments
rithikanarayan ab7bd52
Merge branch 'master' into zarir/sles-2837-vertx-web-finish-route-han…
rithikanarayan 7b0e686
spotless
rithikanarayan ea7a165
Merge branch 'master' into zarir/sles-2837-vertx-web-finish-route-han…
rithikanarayan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...eb/vertx-web-3.4/src/test/java/io/vertx/core/http/impl/ResponseExceptionFiringHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package io.vertx.core.http.impl; | ||
|
|
||
| import io.vertx.core.http.HttpServerResponse; | ||
|
|
||
| /** | ||
| * Test-side bridge that fires the package-private HttpServerResponseImpl.handleException on a | ||
| * Vert.x 3.x server response. Used by server.RouteHandlerExceptionHandlerTest to deterministically | ||
| * reproduce the non-CLOSED_EXCEPTION I/O-failure path that Vert.x exposes via | ||
| * response.exceptionHandler(...). | ||
| */ | ||
| public final class ResponseExceptionFiringHelper { | ||
| private ResponseExceptionFiringHelper() {} | ||
|
|
||
| public static void fireException(HttpServerResponse response, Throwable cause) { | ||
| ((HttpServerResponseImpl) response).handleException(cause); | ||
| } | ||
| } |
126 changes: 126 additions & 0 deletions
126
.../vertx/vertx-web/vertx-web-3.4/src/test/java/server/RouteHandlerExceptionHandlerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package server; | ||
|
|
||
| import static datadog.trace.agent.test.assertions.SpanMatcher.span; | ||
| import static datadog.trace.agent.test.assertions.TraceMatcher.SORT_BY_START_TIME; | ||
| import static datadog.trace.agent.test.assertions.TraceMatcher.trace; | ||
|
|
||
| import datadog.trace.agent.test.AbstractInstrumentationTest; | ||
| import datadog.trace.api.DDSpanTypes; | ||
| import io.vertx.core.Vertx; | ||
| import io.vertx.core.http.HttpServer; | ||
| import io.vertx.core.http.impl.ResponseExceptionFiringHelper; | ||
| import io.vertx.ext.web.Router; | ||
| import java.io.IOException; | ||
| import java.net.HttpURLConnection; | ||
| import java.net.ServerSocket; | ||
| import java.net.URL; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.regex.Pattern; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Regression test for the vertx-web 3.x route-handler span lifecycle on the | ||
| * response.exceptionHandler path. | ||
| * | ||
| * <p>HttpServerResponseImpl.handleException is invoked by Vert.x on non-CLOSED_EXCEPTION I/O | ||
| * failures of the response. Neither endHandler nor bodyEndHandler fires on this path, so the | ||
| * route-handler span would leak without an exception handler registered. The route handler here | ||
| * fires handleException directly via ResponseExceptionFiringHelper (the package-private method | ||
| * Vert.x itself uses internally), then calls response.end() normally so the HTTP client gets a | ||
| * response. | ||
| */ | ||
| class RouteHandlerExceptionHandlerTest extends AbstractInstrumentationTest { | ||
|
|
||
| private static Vertx vertx; | ||
| private static HttpServer server; | ||
| private static int port; | ||
|
|
||
| @BeforeAll | ||
| static void startServer() throws Exception { | ||
| try (ServerSocket socket = new ServerSocket(0)) { | ||
| port = socket.getLocalPort(); | ||
| } | ||
|
|
||
| vertx = Vertx.vertx(); | ||
| Router router = Router.router(vertx); | ||
| router | ||
| .route("/fail") | ||
| .handler( | ||
| ctx -> { | ||
| ResponseExceptionFiringHelper.fireException( | ||
| ctx.response(), new IOException("simulated response I/O failure")); | ||
| try { | ||
| ctx.response().setStatusCode(500).end("error"); | ||
| } catch (IllegalStateException ignore) { | ||
| // handleException may have left the response in a state where end() is rejected; | ||
| // the span is already finished by our registered exception handler. | ||
| } | ||
| }); | ||
|
|
||
| CountDownLatch ready = new CountDownLatch(1); | ||
| server = | ||
| vertx | ||
| .createHttpServer() | ||
| .requestHandler(router::accept) | ||
| .listen( | ||
| port, | ||
| result -> { | ||
| if (result.failed()) { | ||
| throw new RuntimeException("Failed to start Vert.x server", result.cause()); | ||
| } | ||
| ready.countDown(); | ||
| }); | ||
| if (!ready.await(10, TimeUnit.SECONDS)) { | ||
| throw new IllegalStateException("Vert.x server did not start in time"); | ||
| } | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void stopServer() throws Exception { | ||
| if (server != null) { | ||
| CountDownLatch closed = new CountDownLatch(1); | ||
| server.close(ar -> closed.countDown()); | ||
| closed.await(10, TimeUnit.SECONDS); | ||
| } | ||
| if (vertx != null) { | ||
| CountDownLatch closed = new CountDownLatch(1); | ||
| vertx.close(ar -> closed.countDown()); | ||
| closed.await(10, TimeUnit.SECONDS); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void exceptionHandlerFinishesRouteHandlerSpan() throws Exception { | ||
| HttpURLConnection conn = | ||
| (HttpURLConnection) new URL("http://localhost:" + port + "/fail").openConnection(); | ||
| conn.setRequestMethod("GET"); | ||
| conn.setConnectTimeout(5000); | ||
| conn.setReadTimeout(5000); | ||
| try { | ||
| // We don't care about the response code or body — only that the trace flushes. | ||
| conn.getResponseCode(); | ||
| } catch (IOException ignore) { | ||
| // If end() was rejected after handleException, the client may see a closed connection. | ||
| } finally { | ||
| conn.disconnect(); | ||
| } | ||
|
|
||
| // The netty.request span is marked as errored because the route handler ends with | ||
| // HTTP 500; the route-handler span is finished by our exception handler before | ||
| // setStatusCode(500), so it sees status=200 (default) and is not errored. | ||
| assertTraces( | ||
| trace( | ||
| SORT_BY_START_TIME, | ||
| span() | ||
| .operationName(Pattern.compile(Pattern.quote("netty.request"))) | ||
| .type(DDSpanTypes.HTTP_SERVER) | ||
| .error(), | ||
| span() | ||
| .childOfPrevious() | ||
| .operationName(Pattern.compile(Pattern.quote("vertx.route-handler"))) | ||
| .type(DDSpanTypes.HTTP_SERVER))); | ||
| } | ||
| } |
122 changes: 122 additions & 0 deletions
122
...entation/vertx/vertx-web/vertx-web-3.4/src/test/java/server/RouteHandlerSendFileTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| package server; | ||
|
|
||
| import static datadog.trace.agent.test.assertions.SpanMatcher.span; | ||
| import static datadog.trace.agent.test.assertions.TraceMatcher.SORT_BY_START_TIME; | ||
| import static datadog.trace.agent.test.assertions.TraceMatcher.trace; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
rithikanarayan marked this conversation as resolved.
|
||
|
|
||
| import datadog.trace.agent.test.AbstractInstrumentationTest; | ||
| import datadog.trace.api.DDSpanTypes; | ||
| import io.vertx.core.Vertx; | ||
| import io.vertx.core.http.HttpServer; | ||
| import io.vertx.ext.web.Router; | ||
| import java.io.BufferedReader; | ||
| import java.io.InputStreamReader; | ||
| import java.net.HttpURLConnection; | ||
| import java.net.ServerSocket; | ||
| import java.net.URL; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.regex.Pattern; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Regression test for the vertx-web 3.x route-handler span lifecycle on the response.sendFile(...) | ||
| * path. | ||
| * | ||
| * <p>HttpServerResponseImpl.doSendFile (vertx-core 3.x) only invokes bodyEndHandler after the file | ||
| * is written; it never invokes endHandler. With only the endHandler registration (pre-fix), the | ||
| * vertx.route-handler span never finishes on this path, the trace fails to flush, and assertTraces | ||
| * times out. With the fallback addBodyEndHandler registration, the span finishes on every | ||
| * response-end path. | ||
| */ | ||
| class RouteHandlerSendFileTest extends AbstractInstrumentationTest { | ||
|
|
||
| private static Vertx vertx; | ||
| private static HttpServer server; | ||
| private static int port; | ||
| private static Path payload; | ||
|
|
||
| @BeforeAll | ||
| static void startServer() throws Exception { | ||
| payload = Files.createTempFile("vertx-sendfile-", ".txt"); | ||
| Files.write(payload, "vertx sendFile payload\n".getBytes(StandardCharsets.UTF_8)); | ||
| payload.toFile().deleteOnExit(); | ||
|
|
||
| try (ServerSocket socket = new ServerSocket(0)) { | ||
| port = socket.getLocalPort(); | ||
| } | ||
|
|
||
| vertx = Vertx.vertx(); | ||
| Router router = Router.router(vertx); | ||
| router | ||
| .route("/sendfile") | ||
| .handler(ctx -> ctx.response().sendFile(payload.toAbsolutePath().toString())); | ||
|
|
||
| CountDownLatch ready = new CountDownLatch(1); | ||
| server = | ||
| vertx | ||
| .createHttpServer() | ||
| .requestHandler(router::accept) | ||
| .listen( | ||
| port, | ||
| result -> { | ||
| if (result.failed()) { | ||
| throw new RuntimeException("Failed to start Vert.x server", result.cause()); | ||
| } | ||
| ready.countDown(); | ||
| }); | ||
| if (!ready.await(10, TimeUnit.SECONDS)) { | ||
| throw new IllegalStateException("Vert.x server did not start in time"); | ||
| } | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void stopServer() throws Exception { | ||
| if (server != null) { | ||
| CountDownLatch closed = new CountDownLatch(1); | ||
| server.close(ar -> closed.countDown()); | ||
| closed.await(10, TimeUnit.SECONDS); | ||
| } | ||
| if (vertx != null) { | ||
| CountDownLatch closed = new CountDownLatch(1); | ||
| vertx.close(ar -> closed.countDown()); | ||
| closed.await(10, TimeUnit.SECONDS); | ||
| } | ||
| if (payload != null) { | ||
| Files.deleteIfExists(payload); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void sendFileFinishesRouteHandlerSpan() throws Exception { | ||
| HttpURLConnection conn = | ||
| (HttpURLConnection) new URL("http://localhost:" + port + "/sendfile").openConnection(); | ||
| conn.setRequestMethod("GET"); | ||
| conn.setConnectTimeout(5000); | ||
| conn.setReadTimeout(5000); | ||
| assertEquals(200, conn.getResponseCode()); | ||
| try (BufferedReader reader = | ||
| new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { | ||
| assertEquals("vertx sendFile payload", reader.readLine()); | ||
| } | ||
|
|
||
| // Pre-fix: the route-handler span never finishes on the sendFile path, so the trace | ||
| // is never published and assertTraces times out waiting for the trace to flush. | ||
| assertTraces( | ||
| trace( | ||
| SORT_BY_START_TIME, | ||
| span() | ||
| .operationName(Pattern.compile(Pattern.quote("netty.request"))) | ||
| .type(DDSpanTypes.HTTP_SERVER), | ||
| span() | ||
| .childOfPrevious() | ||
| .operationName(Pattern.compile(Pattern.quote("vertx.route-handler"))) | ||
| .type(DDSpanTypes.HTTP_SERVER))); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.