This is an automated email from the ASF dual-hosted git repository.
dsmiley pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git
The following commit(s) were added to refs/heads/main by this push:
new 9ec8b72b1af SOLR-17069: HttpServletRequest; use eagerly to avoid
errors (#3324)
9ec8b72b1af is described below
commit 9ec8b72b1af7a170eb6fa69fa6a0774c466287a1
Author: David Smiley <[email protected]>
AuthorDate: Mon Apr 14 23:11:15 2025 -0400
SOLR-17069: HttpServletRequest; use eagerly to avoid errors (#3324)
HttpServletRequest should be used only by the thread dispatched receiving
it, or else it _might_ produce errors (particularly in Jetty 12). Thus aspects
of the request that are needed should be eagerly consumed instead of deferred,
to avoid risks.
* Cache Span in HttpSolrCall to avoid NPE from Jetty 12 async request loss
* javadocs: warning for HttpSolrCall.getReq
* Cache the security UserPrincipal in SolrRequestInfo, and not the request
* CancellableQueryTracker: don't use HttpServletRequest
* HttpSolrCall: eager parse the user agent
* SolrRequestParsers: eagerly get the principal from the request
---------
Co-authored-by: Sanjay Dutt <[email protected]>
---
.../apache/solr/core/CancellableQueryTracker.java | 2 +-
.../solr/handler/component/HttpShardHandler.java | 2 +-
.../org/apache/solr/request/SolrRequestInfo.java | 9 ++++---
.../java/org/apache/solr/servlet/HttpSolrCall.java | 28 ++++++++++++++++------
.../apache/solr/servlet/SolrRequestParsers.java | 10 +++-----
.../org/apache/solr/update/SolrCmdDistributor.java | 2 +-
6 files changed, 31 insertions(+), 22 deletions(-)
diff --git
a/solr/core/src/java/org/apache/solr/core/CancellableQueryTracker.java
b/solr/core/src/java/org/apache/solr/core/CancellableQueryTracker.java
index f3b79c35837..911c24120b0 100644
--- a/solr/core/src/java/org/apache/solr/core/CancellableQueryTracker.java
+++ b/solr/core/src/java/org/apache/solr/core/CancellableQueryTracker.java
@@ -56,7 +56,7 @@ public class CancellableQueryTracker {
}
}
- activeQueriesGenerated.put(queryID,
req.getHttpSolrCall().getReq().getQueryString());
+ activeQueriesGenerated.put(queryID, req.getParamString());
return queryID;
}
diff --git
a/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java
b/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java
index 7592eed86fc..d5cb3954a81 100644
--- a/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java
@@ -209,7 +209,7 @@ public class HttpShardHandler extends ShardHandler {
req.setMethod(SolrRequest.METHOD.POST);
SolrRequestInfo requestInfo = SolrRequestInfo.getRequestInfo();
if (requestInfo != null) {
- req.setUserPrincipal(requestInfo.getReq().getUserPrincipal());
+ req.setUserPrincipal(requestInfo.getUserPrincipal());
}
return httpShardHandlerFactory.newLBHttpSolrClientReq(req, urls);
diff --git a/solr/core/src/java/org/apache/solr/request/SolrRequestInfo.java
b/solr/core/src/java/org/apache/solr/request/SolrRequestInfo.java
index c30d6f2c29e..ce0d8c139e2 100644
--- a/solr/core/src/java/org/apache/solr/request/SolrRequestInfo.java
+++ b/solr/core/src/java/org/apache/solr/request/SolrRequestInfo.java
@@ -51,12 +51,12 @@ public class SolrRequestInfo {
private SolrQueryRequest req;
private SolrQueryResponse rsp;
private Date now;
- public HttpServletRequest httpRequest;
private TimeZone tz;
private ResponseBuilder rb;
private List<AutoCloseable> closeHooks;
private SolrDispatchFilter.Action action;
private boolean useServerToken = false;
+ private Principal principal;
private static final Logger log =
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
@@ -151,6 +151,7 @@ public class SolrRequestInfo {
public SolrRequestInfo(SolrQueryRequest req, SolrQueryResponse rsp) {
this.req = req;
this.rsp = rsp;
+ this.principal = req != null ? req.getUserPrincipal() : null;
}
public SolrRequestInfo(
@@ -160,8 +161,8 @@ public class SolrRequestInfo {
}
public SolrRequestInfo(HttpServletRequest httpReq, SolrQueryResponse rsp) {
- this.httpRequest = httpReq;
this.rsp = rsp;
+ this.principal = httpReq != null ? httpReq.getUserPrincipal() : null;
}
public SolrRequestInfo(
@@ -171,9 +172,7 @@ public class SolrRequestInfo {
}
public Principal getUserPrincipal() {
- if (req != null) return req.getUserPrincipal();
- if (httpRequest != null) return httpRequest.getUserPrincipal();
- return null;
+ return principal;
}
public Date getNOW() {
diff --git a/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java
b/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java
index 492ad7e5118..7334a5bc47d 100644
--- a/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java
+++ b/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java
@@ -50,6 +50,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
+import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@@ -145,6 +146,8 @@ public class HttpSolrCall {
protected final HttpServletRequest req;
protected final HttpServletResponse response;
protected final boolean retry;
+ private final SolrVersion userAgentSolrVersion; // of the client
+ private final Span span;
protected SolrCore core = null;
protected SolrQueryRequest solrReq = null;
private boolean mustClearSolrRequestInfo = false;
@@ -176,18 +179,30 @@ public class HttpSolrCall {
this.response = response;
this.retry = retry;
this.requestType = RequestType.UNKNOWN;
+ this.userAgentSolrVersion = parseUserAgentSolrVersion();
+ this.span =
Optional.ofNullable(TraceUtils.getSpan(req)).orElse(Span.getInvalid());
+ this.path = ServletUtils.getPathAfterContext(req);
+
req.setAttribute(HttpSolrCall.class.getName(), this);
// set a request timer which can be reused by requests if needed
req.setAttribute(SolrRequestParsers.REQUEST_TIMER_SERVLET_ATTRIBUTE, new
RTimerTree());
// put the core container in request attribute
req.setAttribute("org.apache.solr.CoreContainer", cores);
- path = ServletUtils.getPathAfterContext(req);
}
public String getPath() {
return path;
}
+ /**
+ * WARNING: This method returns a non-null {@link HttpServletRequest}, but
calling certain methods
+ * on it — such as {@link HttpServletRequest#getAttribute(String)} — may
throw {@link
+ * NullPointerException} if accessed outside the original servlet thread
(e.g., in asynchronous
+ * tasks).
+ *
+ * <p>Always cache required request data early during request handling if it
needs to be accessed
+ * later.
+ */
public HttpServletRequest getReq() {
return req;
}
@@ -639,12 +654,7 @@ public class HttpSolrCall {
/** Get the Span for this request. Not null. */
public Span getSpan() {
- var s = TraceUtils.getSpan(req);
- if (s != null) {
- return s;
- } else {
- return Span.getInvalid();
- }
+ return span;
}
// called after init().
@@ -1174,6 +1184,10 @@ public class HttpSolrCall {
* clients prior to 9.9 present themselves as 1.0 or 2.0.
*/
public SolrVersion getUserAgentSolrVersion() {
+ return userAgentSolrVersion;
+ }
+
+ private SolrVersion parseUserAgentSolrVersion() {
String header = req.getHeader("User-Agent");
if (header == null || !header.startsWith("Solr")) {
return null;
diff --git a/solr/core/src/java/org/apache/solr/servlet/SolrRequestParsers.java
b/solr/core/src/java/org/apache/solr/servlet/SolrRequestParsers.java
index 8902acf51a3..d208106c874 100644
--- a/solr/core/src/java/org/apache/solr/servlet/SolrRequestParsers.java
+++ b/solr/core/src/java/org/apache/solr/servlet/SolrRequestParsers.java
@@ -169,7 +169,7 @@ public class SolrRequestParsers {
SolrParams params = parser.parseParamsAndFillStreams(req, streams);
SolrQueryRequest sreq =
- buildRequestFrom(core, params, streams, getRequestTimer(req), req,
null);
+ buildRequestFrom(core, params, streams, getRequestTimer(req), req,
req.getUserPrincipal());
// Handlers and login will want to know the path. If it contains a ':'
// the handler could use it for RESTful URLs
@@ -200,7 +200,7 @@ public class SolrRequestParsers {
Collection<ContentStream> streams, // might be added to but caller
shouldn't depend on it
RTimerTree requestTimer,
final HttpServletRequest req,
- final Principal principal)
+ final Principal principal) // from req, if req was provided, otherwise
from elsewhere
throws Exception {
// ensure streams is non-null and mutable so we can easily add to it
if (streams == null) {
@@ -267,11 +267,7 @@ public class SolrRequestParsers {
new SolrQueryRequestBase(core, params, requestTimer) {
@Override
public Principal getUserPrincipal() {
- if (principal != null) {
- return principal;
- } else {
- return req == null ? null : req.getUserPrincipal();
- }
+ return principal;
}
@Override
diff --git a/solr/core/src/java/org/apache/solr/update/SolrCmdDistributor.java
b/solr/core/src/java/org/apache/solr/update/SolrCmdDistributor.java
index e899c6fbb20..a8c3f7ffe6f 100644
--- a/solr/core/src/java/org/apache/solr/update/SolrCmdDistributor.java
+++ b/solr/core/src/java/org/apache/solr/update/SolrCmdDistributor.java
@@ -308,7 +308,7 @@ public class SolrCmdDistributor implements Closeable {
// Copy user principal from the original request to the new update
request, for later
// authentication interceptor use
if (SolrRequestInfo.getRequestInfo() != null) {
-
req.uReq.setUserPrincipal(SolrRequestInfo.getRequestInfo().getReq().getUserPrincipal());
+
req.uReq.setUserPrincipal(SolrRequestInfo.getRequestInfo().getUserPrincipal());
}
if (req.synchronous) {