|
Expand Up
|
@@ -175,6 +175,18 @@ private enum PoolState { |
|
|
@GuardedBy("poolLock") |
|
|
private final SessionCreationBudget budget; |
|
|
|
|
|
// Handles that reserved a session-creation budget slot in createSession() and have not yet |
|
|
// released it. A slot is released exactly once: as a success when the session reaches READY |
|
|
// (onSessionReady), or as a failure when the session terminates without ever becoming READY |
|
|
// (onSessionClose). Tracking the reservation on the handle -- instead of inferring it from the |
|
|
// close-time prevState -- is what makes the release exactly-once: a session that goes |
|
|
// STARTING -> WAIT_SERVER_CLOSE (e.g. a server GO_AWAY before the open handshake completes) |
|
|
// closes with prevState == WAIT_SERVER_CLOSE, which the abnormal-close branch skips, so the |
|
|
// prevState == STARTING check alone would leak the slot forever. SessionHandle uses identity |
|
|
// equality, so a plain HashSet keys on the handle instance. |
|
|
@GuardedBy("poolLock") |
|
|
private final Set<SessionHandle> sessionsHoldingBudget = new HashSet<>(); |
|
|
|
|
|
private final ClientConfigurationManager configManager; |
|
|
private final ClientConfigurationManager.ListenerHandle configListenerHandle; |
|
|
|
|
Expand Down
Expand Up
|
@@ -455,35 +467,65 @@ private void createSession(OpenParams openParams) { |
|
|
// Explicit create session streams in a detached context |
|
|
// We don't want to propagate the rpc deadline nor the trace context |
|
|
Context prevContext = Context.ROOT.attach(); |
|
|
// Tracks the handle once registered so the catch below can tell whether the reservation was |
|
|
// ever bound to a handle (and therefore what cleanup is required). |
|
|
SessionHandle handle = null; |
|
|
try { |
|
|
try (Scope ignored = io.opentelemetry.context.Context.root().makeCurrent()) { |
|
|
|
|
|
// Build the metadata before registering the handle so the only step between newHandle and |
|
|
// session.start (whose own failures are handled by the session's abort path once the |
|
|
// listener is published) is the non-throwing sessionsHoldingBudget.add. |
|
|
Metadata localMd = new Metadata(); |
|
|
localMd.merge(openParams.metadata()); |
|
|
|
|
|
SessionStream stream = factory.createNew(); |
|
|
Session session = new SessionImpl(metrics, info, sessionNum++, stream, timer); |
|
|
SessionHandle handle = sessions.newHandle(session); |
|
|
handle = sessions.newHandle(session); |
|
|
// Bind the budget reservation made by tryReserveSession() above to this handle so it is |
|
|
// released exactly once when the session becomes READY or terminates. |
|
|
sessionsHoldingBudget.add(handle); |
Comment thread
Comment on lines
+484
to
+487
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.

The sessions list and sessionsHoldingBudget set are non-thread-safe collections guarded by poolLock. Since createSession runs asynchronously and does not hold poolLock during stream creation, calling sessions.newHandle(session) and sessionsHoldingBudget.add(handle) directly here introduces a data race with other threads that access these collections under poolLock (e.g., in onSessionReady or onSessionClose).
Please wrap these registration steps in a poolLock critical section.
poolLock.lock();
try {
handle = sessions.newHandle(session);
// Bind the budget reservation made by tryReserveSession() above to this handle so it is
// released exactly once when the session becomes READY or terminates.
sessionsHoldingBudget.add(handle);
} finally {
poolLock.unlock();
}
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong. createSession is guarded by pool lock.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
|
|
|
|
Metadata localMd = new Metadata(); |
|
|
localMd.merge(openParams.metadata()); |
|
|
final SessionHandle startedHandle = handle; |
|
|
session.start( |
|
|
openParams.request(), |
|
|
localMd, |
|
|
new Listener() { |
|
|
@Override |
|
|
public void onReady(OpenSessionResponse msg) { |
|
|
SessionPoolImpl.this.onSessionReady(handle, msg); |
|
|
SessionPoolImpl.this.onSessionReady(startedHandle, msg); |
|
|
} |
|
|
|
|
|
@Override |
|
|
public void onGoAway(GoAwayResponse msg) { |
|
|
SessionPoolImpl.this.onSessionGoAway(handle, msg); |
|
|
SessionPoolImpl.this.onSessionGoAway(startedHandle, msg); |
|
|
} |
|
|
|
|
|
@Override |
|
|
public void onClose(SessionState prevState, Status status, Metadata trailers) { |
|
|
SessionPoolImpl.this.onSessionClose(handle, prevState, status, trailers); |
|
|
SessionPoolImpl.this.onSessionClose(startedHandle, prevState, status, trailers); |
|
|
} |
|
|
}); |
|
|
} |
|
|
} catch (RuntimeException | Error e) { |
|
|
// A synchronous failure here (e.g. factory.createNew, the SessionImpl constructor, or |
|
|
// metadata merge) means no terminal session callback will ever run for this reservation, so |
|
|
// the budget slot and any partially-registered handle would leak for the life of the pool. |
|
|
// Release the slot as a failure and unwind the handle ourselves. Note session.start's own |
|
|
// failures do NOT land here: it publishes the listener first and routes exceptions through |
|
|
// the session's abort path, which fires onClose -> onSessionClose (the normal release path). |
|
|
debugTagTracer.record(TelemetryConfiguration.Level.WARN, "session_create_failed"); |
|
|
logger.log(Level.WARNING, "Failed to create session, releasing reserved budget", e); |
|
|
// If a handle was registered it owns the reservation via the set; otherwise the reservation |
|
|
// was never bound to a handle and must be released directly. |
|
|
if (handle == null || sessionsHoldingBudget.remove(handle)) { |
|
|
budget.onSessionCreationFailure(); |
|
|
} |
|
|
if (handle != null) { |
|
|
sessions.removeUnstartedHandle(handle); |
|
|
} |
Comment thread
Comment on lines
+521
to
+526
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.

The operations in this catch block modify sessionsHoldingBudget, budget, and sessions (via removeUnstartedHandle), all of which are non-thread-safe resources guarded by poolLock. Since createSession runs asynchronously without holding poolLock, executing these modifications without synchronization introduces a critical data race with other threads (e.g., those executing onSessionClose or onSessionReady).
Please wrap these state updates in a poolLock critical section.
poolLock.lock();
try {
if (handle == null || sessionsHoldingBudget.remove(handle)) {
budget.onSessionCreationFailure();
}
if (handle != null) {
sessions.removeUnstartedHandle(handle);
}
} finally {
poolLock.unlock();
}
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above. CreateSession is already guarded by poolLock.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
|
// Let the pool recover instead of running permanently short a session. |
|
|
maybeScheduleCreateSessionRetry(); |
|
|
} finally { |
|
|
Context.ROOT.detach(prevContext); |
|
|
} |
|
Expand Down
Expand Up
|
@@ -540,7 +582,11 @@ private void onSessionReady(SessionHandle handle, OpenSessionResponse ignored) { |
|
|
} |
|
|
handle.onSessionStarted(); |
|
|
|
|
|
budget.onSessionCreationSuccess(); |
|
|
// Release the reservation as a success. Guarded by the set so we release exactly once even if |
|
|
// onSessionReady were ever delivered more than once. |
|
|
if (sessionsHoldingBudget.remove(handle)) { |
|
|
budget.onSessionCreationSuccess(); |
|
|
} |
|
|
|
|
|
// handle pending rpcs |
|
|
tryDrainPendingRpcs(); |
|
Expand Down
Expand Up
|
@@ -613,6 +659,21 @@ private void onSessionClose( |
|
|
|
|
|
poolLock.lock(); |
|
|
try { |
|
|
// Release the budget reservation FIRST, before any code below that can throw. This is the |
|
|
// session's terminal callback, so there is no later backstop: if the reservation is not |
|
|
// released here it leaks forever. In particular handle.onSessionClosed(prevState) below can |
|
|
// throw (e.g. IllegalStateException on an unexpected NEW / double-CLOSED prevState), which |
|
|
// would otherwise strand the slot -- the same leak this fix exists to prevent. |
|
|
// |
|
|
// Release as a failure if this session still holds a slot, i.e. it terminated without ever |
|
|
// reaching READY; sessions that reached READY were already removed from the set in |
|
|
// onSessionReady, so this never double-releases. Keying off the reservation instead of the |
|
|
// close-time prevState is also what covers the STARTING -> WAIT_SERVER_CLOSE (GO_AWAY) path |
|
|
// that the abnormal-close branch below skips. |
|
|
if (sessionsHoldingBudget.remove(handle)) { |
|
|
budget.onSessionCreationFailure(); |
|
|
} |
|
|
|
|
|
logger.fine( |
|
|
String.format("Removing closed session from pool %s", handle.getSession().getLogName())); |
|
|
|
|
Expand Down
Expand Up
|
@@ -642,9 +703,8 @@ private void onSessionClose( |
|
|
toBeClosed = popClosableRpcs(); |
|
|
} |
|
|
|
|
|
if (prevState == SessionState.STARTING) { |
|
|
budget.onSessionCreationFailure(); |
|
|
} |
|
|
// Budget release for STARTING-phase closes is handled above via sessionsHoldingBudget, |
|
|
// which also covers the STARTING -> WAIT_SERVER_CLOSE (GO_AWAY) path this branch skips. |
|
|
|
|
|
// TODO: backoff creating a new session when consecutive failures > max? |
|
|
if (poolSizer.handleSessionClose(StatusProto.fromStatusAndTrailers(status, trailers))) { |
|
Expand Down
|
|