Repository navigation
Conversation
…t completes The open session in view interceptor closed the sessions it opened as soon as a request went asynchronous. A task that the action returned then failed to lazily load an association of an entity the action had loaded: with a LazyInitializationException, or with a NullPointerException from Hibernate when it got there while the session was being closed. The sessions are still taken off the thread when the request goes asynchronous, so that the thread's next request does not use them. They are now closed when the asynchronous request completes, from an AsyncListener on the request's asynchronous context, which the container also calls after a timeout or an error. Without an asynchronous context, they are closed at once, as before.
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A request that finishes without another dispatch leaves the default session open.
| released.add(sessionHolder.getSession()); | ||
| } | ||
| else { | ||
| super.afterConcurrentHandlingStarted(request); |
There was a problem hiding this comment.
A controller returns a pending result. After the chain returns, the request is completed with no further dispatch. The default session is taken off the thread and left open. Close it when the request finishes.
There was a problem hiding this comment.
I can't reproduce a default session left open here. In the datasources example application, an action that returns task { render(text: 'ok') } has its default session closed once the response is done. On 7.0.x before 97d88c2 (00f51d4), the same session is still open after the response; that's what 97d88c2 and this change fix.
An action that starts a task and doesn't return it is the one case left. On 7.0.x, WebPromises.task starts the asynchronous handling without setting ASYNC_STARTED, so that default session is left to Spring, as it was before. Which pending result did you use?
jdaugherty
left a comment
There was a problem hiding this comment.
Reviewed 8a00c27. I did not find a confirmed defect introduced by this patch. Unbinding the owned sessions at the asynchronous handoff and closing them from the completion listener addresses the lazy-loading regression while preserving thread cleanup.
Local verification passed:
- MultiDataSourceSessionSpec: 15 tests on Java 17.
- OsivRequestSessionSpec: 4 tests on Java 17 and 4 on Java 21.
- Full datasources integration suite: 36 tests on Java 21.
- git diff --check.
The Java 21 CI failure still needs an explanation before merge: the new lazy-loading feature receives HTTP 500 from /requestSession/lazyVolumes. I could not reproduce it locally. The job log also contains a recycled-request IllegalStateException from GrailsAsyncContext cleanup immediately before that feature, but that alone does not establish the cause of the 500: https://github.com/apache/grails-core/actions/runs/44716026355/job/136224475090
On the existing default-session discussion: a returned Promise sets ASYNC_STARTED and this patch includes its default session in completion cleanup. Starting a WebPromises task without returning it can miss that marker, but that condition and the fallback to the parent interceptor predate this patch. I would want the concrete pending-result reproducer before treating the existing comment as a defect in this change.
|
@jdaugherty thanks for the review. The Java 21 failure is #17094: the error of the feature before it, hitting the next request on the same connection.
A probe in the
So #17095 should go in first. Could you review it? I'll then merge 7.0.x into this branch and rerun CI. 8.0.x has the same On the default session: agreed, a task that is started without being returned predates this change. |
codeconsole
left a comment
There was a problem hiding this comment.
Approving. Closing the sessions from the AsyncListener fixes the lazy load in a returned task, and it matches what Spring's own interceptors do for a Callable or DeferredResult: the session stays open until the asynchronous request completes. The fallbacks hold:
- With no asynchronous context, or when
addListenerthrowsIllegalStateException, the sessions are closed at once, as before. onStartAsyncregisters the listener again, so a second asynchronous cycle still closes them.- A task that calls
complete()before the request thread has registered the listener is fine too. The container defers the completion until the original dispatch returns, so the listener is still called.
Merging still needs #17095 first and a green rerun. The Java 21 failure of the lazy-load feature is the recycled-request 500 from #17094, as you showed.
On the default-session thread, I agree with your answer. A returned promise sets ASYNC_STARTED (AsyncActionResultTransformer), so its default session is in the completion cleanup. A task that is started without being returned goes through startAsync() in AsyncWebRequestPromiseDecorator without setting it, and that predates this change. Connection retention belongs in #17099.
The inline comments don't block the merge. Two small things outside the diff:
- The class Javadoc of
GrailsOpenSessionInViewInterceptorstill says "a request that goes asynchronous closes the sessions it opened, so that they do not stay bound to the thread". It now unbinds them at the handoff and closes them when the asynchronous request completes. - The existing features of
MultiDataSourceSessionSpecthat callafterConcurrentHandlingStartedonwebRequest()("a request that goes asynchronous closes the sessions it opened…", "a request that Grails makes asynchronous closes its default session too", and so on) now cover only the request without an asynchronous context. Their names could say so.
| List<Session> released = new ArrayList<>(); | ||
| try { | ||
| closeAdditionalSessions(request, true); | ||
| releaseAdditionalSessions(request, true, released); |
There was a problem hiding this comment.
Non-blocking. Now that the task can use these sessions, this flush can run on the request thread while a task that started at once is already lazily loading through the same secondary session on its own thread. A Hibernate Session isn't thread-safe. It only happens under AUTO with hibernate.allow_update_outside_transaction (or inside a transaction at the handoff), since COMMIT is the default flush mode. The sleep(500) in the example tasks hides it. It's the same ownership question as #17099, so it could be folded in there.
There was a problem hiding this comment.
Agreed. The task can already be running when the request thread gets here, and this flush is the one thing the interceptor still does to a session the task may be using. It's the ownership question of #17099, so I've added it there: #17099 (comment). I'm leaving the flush as it is in this PR.
| released.add(sessionHolder.getSession()); | ||
| } | ||
| else { | ||
| super.afterConcurrentHandlingStarted(request); |
There was a problem hiding this comment.
In the Spring path (Callable, DeferredResult, SseEmitter), released now holds the additional sessions too, and startAsync() has been called, so they are also kept open until the request completes instead of being closed here. I think that's right: a Callable can now lazily load an entity of another datasource, as it already could for the default one. But the description says this path is unchanged, and no feature covers it. "a request that Spring makes asynchronous leaves its default session open for Spring to close" uses webRequest(), which has no asynchronous context. Could you add a feature with a ServletWebRequest that calls startAsync() without ASYNC_STARTED? It would check that the default session is left to Spring and that the secondary one stays open until asyncContext.complete().
There was a problem hiding this comment.
You're right, the description was wrong about that path. WebAsyncManager calls startAsync() before the handler returns, so for a Callable or DeferredResult the other datasources' sessions get the listener too. I've kept that and corrected the description.
0043798 adds "a request that Spring makes asynchronous closes the session of another datasource when the asynchronous request completes, and leaves its default session to Spring". It uses a ServletWebRequest that calls startAsync() without org.grails.ASYNC_STARTED. After the handoff, both sessions are unbound and open. Once the asynchronous context completes, the secondary session is closed and the default one is still open. The feature fails on aee4c04, where the secondary session is closed at the handoff.
| assert RequestSessions.asyncSecondarySession != null | ||
| } | ||
|
|
||
| and: 'the container times it out' |
There was a problem hiding this comment.
As the description says, this feature passes without the change, because the sessions are already closed at the handoff, long before the timeout. Checking that they are still open while the request waits would make it fail without the listener. With a longer timeout in AsyncRequestSessionController.timesOut (startAsync().timeout = 3000), something like:
when: 'the request goes asynchronous and waits past the handoff'
final asyncResponse = CompletableFuture.supplyAsync({ http('/asyncRequestSession/timesOut') } as Supplier<TestHttpResponse>)
new PollingConditions(timeout: 10).eventually {
assert RequestSessions.asyncDefaultSession != null
assert RequestSessions.asyncSecondarySession != null
}
sleep(500)
then: 'its sessions are still open'
RequestSessions.asyncDefaultSession.open
RequestSessions.asyncSecondarySession.open
when: 'the container times it out'
asyncResponse.get(30, TimeUnit.SECONDS)
then: 'both sessions are closed'
...…re a timeout For a Callable or DeferredResult, Spring starts the asynchronous request before the open session in view interceptor runs, so the sessions of the other datasources are closed when the request completes there too. A new MultiDataSourceSessionSpec feature covers that path. The timeout feature of OsivRequestSessionSpec now checks that the sessions stay open while the request waits, so that it fails without the change. The class Javadoc of GrailsOpenSessionInViewInterceptor says when the sessions are closed, and the features that run without an asynchronous context say so.
|
@codeconsole thanks for the review. 0043798 takes your points:
The Hibernate 5 plugin passes 21 tests, The push dismissed your approval. Could you approve again? Merging still waits for #17095. |
✅ All tests passed ✅🏷️ Commit: 0043798 Learn more about TestLens at testlens.app/docs. |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A request that finishes without another dispatch leaves the default session open.
| released.add(sessionHolder.getSession()); | ||
| } | ||
| else { | ||
| super.afterConcurrentHandlingStarted(request); |
There was a problem hiding this comment.
A controller starts asynchronous processing directly and completes it after the first dispatch, with no further dispatch. The default session is taken off the thread and left open. Close it when the request finishes.
Since aee4c04, the open session in view interceptor closes the sessions it opened as soon as a request goes asynchronous. A task that the action returns can then no longer lazily load an association of an entity the action loaded:
The task fails with a
LazyInitializationException. Without the wait that the example in the tests adds, it fails most of the time with aNullPointerExceptionfrom Hibernate instead ("this.session" is null, inAbstractPersistentCollection.withTemporarySessionIfNeeded), because it gets there while the session is being closed. This worked before aee4c04, for the default datasource and for the others.The change
AsyncListeneron the request's asynchronous context. The container calls the listener after a timeout or an error too.CallableorDeferredResult, Spring has already started the asynchronous request when the interceptor runs, so the other datasources' sessions are now closed when it completes too, instead of at the handoff. TheCallablecan then lazily load through them, as it already could through the default session. The default session is still left to the interceptors Spring registers for theCallableorDeferredResult.compileOnlydependency, and the listener is in a nested class of its own.Tests
MultiDataSourceSessionSpec:OsivRequestSessionSpec, in thedatasourcesexample:hasManyand abelongsToproxy of entities of the default datasource, and ahasManyof an entity of the secondary datasource, after the request has gone asynchronous. It fails without the change, with aLazyInitializationException.Without the change, both new
MultiDataSourceSessionSpecfeatures, the lazy-load feature, the timeout feature and the existing asynchronous feature fail.With it, the Hibernate 5 plugin passes 21 tests,
datasources36, andmongodb/hibernate54.codeStylepasses for the plugin.The same change for Hibernate 7 is #17093. This one reaches the Hibernate 5 module of 8.0.x through the merge-up.