Repository navigation
GH-1502: [JDBC] Close the server session on Connection#close without a catalog - #1504
AbhishekPathania wants to merge 4 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
384b190 to
84c8253
Compare
| fre, "Failed to close Flight SQL session.", "closing Flight SQL session"); | ||
| } else { | ||
| // Without a catalog the driver never asked for a session; the server may not support them. | ||
| logSuppressedCloseException(fre, "closing Flight SQL session"); |
There was a problem hiding this comment.
This suppresses every FlightRuntimeException when no catalog is configured, including failures such as UNAVAILABLE or UNAUTHENTICATED. In those cases Connection.close() succeeds even though the server session may remain open.
Could we suppress only statuses indicating that the server does not support session catalog, and propagate other failures as we do when a catalog is configured?
There was a problem hiding this comment.
Thanks, fair point. I narrowed it. Without a catalog, close now only ignores statuses that mean there's no session to close:
UNIMPLEMENTED: servers without CloseSession (the Java, Go and C++ defaults)INVALID_ARGUMENT: servers that don't know the action, like all Rust servers today (https://github.com/apache/arrow-rs/blob/9951b625879a19844ecd0b2dd352347a87ea26dd/arrow-flight/src/sql/server.rs#L451) and older Java/Go onesNOT_FOUND:ServerSessionMiddlewarewhen no session was createdUNAUTHENTICATED: the client isn't (or is no longer) authenticated, so there's no session. Without it, existing tests that connect without credentials (e.g.ConnectionTest#testGetBasicClientNoAuthShouldOpenConnection) fail on close.
Everything else goes through the catalog path.
There was a problem hiding this comment.
Why are we being so prescriptive? Why can't we just let it fail? Isn't the purpose to show that something went wrong?
There was a problem hiding this comment.
Without ignoring these "no session" statuses when catalog is unset, Connection#close() would throw SQLException against any Flight SQL server that does not implement CloseSession (including the default Java/Go/C++/Rust implementations) or for unauthenticated connections (such as ConnectionTest#testGetBasicClientNoAuthShouldOpenConnection). Genuine failures like UNKNOWN, DEADLINE_EXCEEDED, PERMISSION_DENIED, or non-GOAWAY INTERNAL still fail and propagate SQLException.
There was a problem hiding this comment.
@xborder if we let it fail, close() would throw on every server without CloseSession, which today is the default Java, Go, C++ and Rust ones. So it only skips the "no session" statuses, anything else still fails.
There was a problem hiding this comment.
@AbhishekPathania, I understand what it does. What I'm still wondering is why the driver should be the one handling incomplete server implementations. I know AI makes it easy to cover edge cases like this, but conceptually I'd like to push back a bit on the decision. If a new FlightStatusCode gets added, it's unclear if they should also be part of NO_SESSION_STATUS_CODES or not
There was a problem hiding this comment.
@xborder I think the driver needs to handle it, since most servers don't implement CloseSession today, including the reference Java, Go, C++ and Rust ones. If close() always sends it, it has to treat "not supported" as nothing to close, otherwise close() fails on all of them. ADBC's Go driver handles it in the driver too: https://github.com/apache/arrow-adbc/blob/40323c6da8cd22079439cec704e1b9ea67e94003/go/driver/flightsql/flightsql_connection.go#L1329-L1339
On new status codes, the list only has the ones that mean there's no session to close, so a new code isn't on it and fails like any other error. Adding one later is a one-line change if a server needs it.
84c8253 to
bc7f091
Compare
433267c to
e6c2ab8
Compare
b967096 to
c580987
Compare
…nt handlers Endpoint handlers are built from a copy of the connection's Builder and authenticate with the connection's credentials. On a server that keys sessions by bearer token, a CloseSession from a failed endpoint fetch would end the connection's own session. ADBC's Flight SQL driver likewise never closes sessions from its endpoint clients.
…on without a catalog
c580987 to
e1d7f15
Compare
| public void close() throws SQLException { | ||
| if (catalog.isPresent()) { | ||
| final CallOption[] baseOptions = getOptions(); | ||
| final CallOption[] closeSessionOptions = Arrays.copyOf(baseOptions, baseOptions.length + 1); | ||
| closeSessionOptions[baseOptions.length] = CLOSE_SESSION_TIMEOUT; | ||
| try { | ||
| if (builder.ownsSession) { | ||
| sqlClient.closeSession(new CloseSessionRequest(), closeSessionOptions); | ||
| } | ||
| } catch (FlightRuntimeException fre) { | ||
| if (catalog.isEmpty() && NO_SESSION_STATUS_CODES.contains(fre.status().code())) { | ||
| logSuppressedCloseException(fre, "closing Flight SQL session"); | ||
| } else { | ||
| handleBenignCloseException( | ||
| fre, "Failed to close Flight SQL session.", "closing Flight SQL session"); | ||
| } | ||
| } finally { | ||
| try { | ||
| sqlClient.closeSession(new CloseSessionRequest(), getOptions()); | ||
| AutoCloseables.close(sqlClient); | ||
| } catch (FlightRuntimeException fre) { | ||
| handleBenignCloseException( | ||
| fre, "Failed to close Flight SQL session.", "closing Flight SQL session"); | ||
| fre, "Failed to clean up client resources.", "closing Flight SQL client"); | ||
| } catch (final Exception e) { | ||
| throw new SQLException("Failed to clean up client resources.", e); |
There was a problem hiding this comment.
On main (via #1268 ), AutoCloseables.close(this::closeCatalogSession, sqlClient) already closes sqlClient when closeSession throws, and preserves the primary SQLException by attaching any secondary cleanup failure with addSuppressed.
With this try/catch/finally rewrite, if closeSession throws a non-suppressed SQLException and AutoCloseables.close(sqlClient) in finally also throws a non-benign exception, the exception from finally will silently mask the session-close SQLException. Also, baseOptions / closeSessionOptions don't need to be allocated when !builder.ownsSession.
Could we keep the AutoCloseables.close(this::closeSession, sqlClient) pattern from main?
For example:
@Override
public void close() throws SQLException {
try {
AutoCloseables.close(this::closeSession, sqlClient);
} catch (FlightRuntimeException fre) {
handleBenignCloseException(
fre, "Failed to clean up client resources.", "closing Flight SQL client");
} catch (SQLException e) {
throw e;
} catch (final Exception e) {
throw new SQLException("Failed to clean up client resources.", e);
}
}
private void closeSession() throws SQLException {
if (!builder.ownsSession) {
return;
}
final CallOption[] baseOptions = getOptions();
final CallOption[] closeSessionOptions = Arrays.copyOf(baseOptions, baseOptions.length + 1);
closeSessionOptions[baseOptions.length] = CLOSE_SESSION_TIMEOUT;
try {
sqlClient.closeSession(new CloseSessionRequest(), closeSessionOptions);
} catch (FlightRuntimeException fre) {
if (catalog.isEmpty() && NO_SESSION_STATUS_CODES.contains(fre.status().code())) {
logSuppressedCloseException(fre, "closing Flight SQL session");
} else {
handleBenignCloseException(
fre, "Failed to close Flight SQL session.", "closing Flight SQL session");
}
}
}There was a problem hiding this comment.
Looking at e1d7f156, close() is already using AutoCloseables.close(this::closeSession, sqlClient);, so the finally exception-masking concern is already resolved. Moving if (!builder.ownsSession) return; before getOptions() is just a minor non-blocking nit.
There was a problem hiding this comment.
Thanks, moved the ownsSession check to the top of closeSession().
| private static Object[] testCloseSessionWithoutCatalog() { | ||
| return new Object[] { | ||
| new Object[] {FlightStatusCode.UNIMPLEMENTED, true}, | ||
| new Object[] {FlightStatusCode.INVALID_ARGUMENT, true}, | ||
| new Object[] {FlightStatusCode.NOT_FOUND, true}, | ||
| new Object[] {FlightStatusCode.UNAVAILABLE, true}, | ||
| new Object[] {FlightStatusCode.UNAUTHENTICATED, true}, | ||
| new Object[] {FlightStatusCode.UNKNOWN, false}, | ||
| }; |
There was a problem hiding this comment.
Could we also include FlightStatusCode.DEADLINE_EXCEEDED (false) here? Since CLOSE_SESSION_TIMEOUT introduces a 5s deadline on closeSession, having an explicit test case confirms that a timeout when closing without a catalog still closes sqlClient and propagates SQLException.
There was a problem hiding this comment.
Considering this non-blocking as well — nice-to-have if you push another update, otherwise fine as is.
There was a problem hiding this comment.
Added. Flight maps DEADLINE_EXCEEDED to TIMED_OUT, so the new row is {TIMED_OUT, false}.
| sqlClient.closeSession(new CloseSessionRequest(), closeSessionOptions); | ||
| } | ||
| } catch (FlightRuntimeException fre) { | ||
| if (catalog.isEmpty() && NO_SESSION_STATUS_CODES.contains(fre.status().code())) { |
There was a problem hiding this comment.
Why is catalog such a relevant piece of information here? I don't understand why close was only called when there was a catalog and I don't understand why we surpress errors if it is empty.
This seems tuned to a specific server implementation
There was a problem hiding this comment.
@xborder makes sense. I kept the catalog path as it was to keep the change small, but these statuses all mean there's no session to close, catalog or not. Happy to drop the catalog check so both work the same way.
| fre, "Failed to close Flight SQL session.", "closing Flight SQL session"); | ||
| } else { | ||
| // Without a catalog the driver never asked for a session; the server may not support them. | ||
| logSuppressedCloseException(fre, "closing Flight SQL session"); |
There was a problem hiding this comment.
@AbhishekPathania, I understand what it does. What I'm still wondering is why the driver should be the one handling incomplete server implementations. I know AI makes it easy to cover edge cases like this, but conceptually I'd like to push back a bit on the decision. If a new FlightStatusCode gets added, it's unclear if they should also be part of NO_SESSION_STATUS_CODES or not
What's Changed
Connection#closenow sendsCloseSessioneven when nocatalogwas set, so servers that create a session per connection can free it right away.UNIMPLEMENTED,INVALID_ARGUMENT,NOT_FOUND,UNAUTHENTICATED) are logged at debug and ignored. Other errors are handled as with a catalog.closecannot hang on an unresponsive server.FlightClient#closealready waits up to 5 seconds for the channel to shut down.CloseSessionthrows.getStreamsbuilds for other endpoint locations never sendCloseSession. They authenticate with the connection's credentials, so on a server that keys sessions by bearer token, closing one after a failed fetch would end the connection's own session. ADBC's driver also closes only the main client's session.This matches ADBC's Flight SQL driver, which always sends
CloseSessionon close.The code and tests were drafted with an AI assistant (Claude). I reviewed every change and also ran it against our own Flight SQL server, where 25 reconnects left 25 sessions open before this change and none after.
Closes #1502.