Skip to content

Commit 219ebc1

Browse files
committed
better comments
1 parent 10ef0a0 commit 219ebc1

4 files changed

Lines changed: 35 additions & 25 deletions

File tree

  • instrumentation
    • servlet
      • servlet-3.0/testing/src/main/java/io/opentelemetry/instrumentation/servlet/v3_0
      • servlet-5.0/testing/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0
    • tomcat
      • tomcat-10.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/v10_0
      • tomcat-7.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/v7_0

instrumentation/servlet/servlet-3.0/testing/src/main/java/io/opentelemetry/instrumentation/servlet/v3_0/TestServlet3.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,19 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) {
157157
context.complete();
158158
} else if (EXCEPTION.equals(endpoint)) {
159159
resp.setStatus(endpoint.getStatus());
160+
// Workaround for a sporadic bug in older Tomcat versions: when the servlet
161+
// throws after writing the async response body, the connection is sometimes
162+
// reset before the response is flushed, which surfaces on the client as
163+
// ClosedSessionException. Other servlet containers (and current Tomcat)
164+
// handle the basic write-then-throw pattern fine, so this workaround is
165+
// scoped to old Tomcat only: set Content-Length so the response is
166+
// self-delimiting, and close the writer to force the flush before the throw.
160167
if (req.getClass().getName().contains("catalina") && !testLatestDeps()) {
161-
// Set Content-Length so the response is self-delimiting; combined with
162-
// the writer.close() below, this lets the client read a complete 500
163-
// response on older Tomcat before the post-throw connection reset.
164168
resp.setContentLength(endpoint.getBody().length());
165169
}
166170
PrintWriter writer = resp.getWriter();
167171
writer.print(endpoint.getBody());
168172
if (req.getClass().getName().contains("catalina") && !testLatestDeps()) {
169-
// Older Tomcat versions may close the connection before sending an async
170-
// response when the servlet throws after writing the response body.
171173
writer.close();
172174
}
173175
throw new IllegalStateException(endpoint.getBody());

instrumentation/servlet/servlet-5.0/testing/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/TestServlet5.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,19 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) {
155155
context.complete();
156156
} else if (EXCEPTION.equals(endpoint)) {
157157
resp.setStatus(endpoint.getStatus());
158+
// Workaround for a sporadic bug in older Tomcat versions: when the servlet
159+
// throws after writing the async response body, the connection is sometimes
160+
// reset before the response is flushed, which surfaces on the client as
161+
// ClosedSessionException. Other servlet containers (and current Tomcat)
162+
// handle the basic write-then-throw pattern fine, so this workaround is
163+
// scoped to old Tomcat only: set Content-Length so the response is
164+
// self-delimiting, and close the writer to force the flush before the throw.
158165
if (isOldTomcat(req)) {
159-
// Set Content-Length so the response is self-delimiting; combined with
160-
// the writer.close() below, this lets the client read a complete 500
161-
// response on older Tomcat before the post-throw connection reset.
162166
resp.setContentLength(endpoint.getBody().length());
163167
}
164168
PrintWriter writer = resp.getWriter();
165169
writer.print(endpoint.getBody());
166170
if (isOldTomcat(req)) {
167-
// Older Tomcat versions may close the connection before sending an async
168-
// response when the servlet throws after writing the response body.
169171
writer.close();
170172
}
171173
throw new IllegalStateException(endpoint.getBody());
@@ -242,17 +244,19 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
242244
resp.sendError(endpoint.getStatus(), endpoint.getBody());
243245
} else if (EXCEPTION.equals(endpoint)) {
244246
resp.setStatus(endpoint.getStatus());
247+
// Workaround for a sporadic bug in older Tomcat versions: when the servlet throws
248+
// after writing the async response body, the connection is sometimes reset before
249+
// the response is flushed, which surfaces on the client as ClosedSessionException.
250+
// Other servlet containers (and current Tomcat) handle the basic write-then-throw
251+
// pattern fine, so this workaround is scoped to old Tomcat only: set
252+
// Content-Length so the response is self-delimiting, and close the writer to force
253+
// the flush before the throw.
245254
if (isOldTomcat(req)) {
246-
// Set Content-Length so the response is self-delimiting; combined with the
247-
// writer.close() below, this lets the client read a complete 500 response on
248-
// older Tomcat before the post-throw connection reset.
249255
resp.setContentLength(endpoint.getBody().length());
250256
}
251257
PrintWriter writer = resp.getWriter();
252258
writer.print(endpoint.getBody());
253259
if (isOldTomcat(req)) {
254-
// Older Tomcat versions may close the connection before sending an async
255-
// response when the servlet throws after writing the response body.
256260
writer.close();
257261
}
258262
throw new IllegalStateException(endpoint.getBody());

instrumentation/tomcat/tomcat-10.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/v10_0/AsyncServlet.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,19 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) {
6060
resp.getWriter().print(endpoint.getBody());
6161
} else if (endpoint.equals(EXCEPTION)) {
6262
resp.setStatus(endpoint.getStatus());
63+
// Workaround for a sporadic bug in older Tomcat versions: when the servlet
64+
// throws after writing the async response body, the connection is sometimes
65+
// reset before the response is flushed, which surfaces on the client as
66+
// ClosedSessionException. Other servlet containers (and current Tomcat)
67+
// handle the basic write-then-throw pattern fine, so this workaround is
68+
// scoped to old Tomcat only: set Content-Length so the response is
69+
// self-delimiting, and close the writer to force the flush before the throw.
6370
if (!testLatestDeps()) {
64-
// Set Content-Length so the response is self-delimiting; combined with
65-
// the writer.close() below, this lets the client read a complete 500
66-
// response on older Tomcat before the post-throw connection reset.
6771
resp.setContentLength(endpoint.getBody().length());
6872
}
6973
PrintWriter writer = resp.getWriter();
7074
writer.print(endpoint.getBody());
7175
if (!testLatestDeps()) {
72-
// Older Tomcat versions may close the connection before sending an async
73-
// response when the servlet throws after writing the response body.
7476
writer.close();
7577
}
7678
throw new IllegalStateException(endpoint.getBody());

instrumentation/tomcat/tomcat-7.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/v7_0/AsyncServlet.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,19 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) {
5959
resp.getWriter().print(endpoint.getBody());
6060
} else if (endpoint.equals(EXCEPTION)) {
6161
resp.setStatus(endpoint.getStatus());
62+
// Workaround for a sporadic bug in older Tomcat versions: when the servlet
63+
// throws after writing the async response body, the connection is sometimes
64+
// reset before the response is flushed, which surfaces on the client as
65+
// ClosedSessionException. Other servlet containers (and current Tomcat)
66+
// handle the basic write-then-throw pattern fine, so this workaround is
67+
// scoped to old Tomcat only: set Content-Length so the response is
68+
// self-delimiting, and close the writer to force the flush before the throw.
6269
if (!testLatestDeps()) {
63-
// Set Content-Length so the response is self-delimiting; combined with
64-
// the writer.close() below, this lets the client read a complete 500
65-
// response on older Tomcat before the post-throw connection reset.
6670
resp.setContentLength(endpoint.getBody().length());
6771
}
6872
PrintWriter writer = resp.getWriter();
6973
writer.print(endpoint.getBody());
7074
if (!testLatestDeps()) {
71-
// Older Tomcat versions may close the connection before sending an async
72-
// response when the servlet throws after writing the response body.
7375
writer.close();
7476
}
7577
throw new IllegalStateException(endpoint.getBody());

0 commit comments

Comments
 (0)