Skip to content

fix(hibernate5): close the sessions of an asynchronous request when it completes - #17092

Open
matrei wants to merge 2 commits into
7.0.xfrom
fix/close-sessions-when-async-request-completes-7.0.x
Open

matrei wants to merge 2 commits into
7.0.xfrom
fix/close-sessions-when-async-request-completes-7.0.x

Conversation

@matrei

@matrei matrei commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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:

import static grails.async.web.WebPromises.task

class ShelfController {

    def show(Long id) {
        Shelf shelf = Shelf.get(id)
        task {
            render(text: shelf.volumes*.title.join(','))
        }
    }
}

The task fails with a LazyInitializationException. Without the wait that the example in the tests adds, it fails most of the time with a NullPointerException from Hibernate instead ("this.session" is null, in AbstractPersistentCollection.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

  • When the request goes asynchronous, the interceptor still takes its sessions off the thread, so that the thread's next request doesn't use them.
  • It now closes them when the asynchronous request completes, from an AsyncListener on the request's asynchronous context. The container calls the listener after a timeout or an error too.
  • Without an asynchronous context, the sessions are closed at once, as before.
  • The flush of the other datasources' sessions at the handoff is unchanged.
  • For a Callable or DeferredResult, 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. The Callable can 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 the Callable or DeferredResult.
  • The Servlet API is a compileOnly dependency, and the listener is in a nested class of its own.

Tests

  • MultiDataSourceSessionSpec:
    • A servlet request that Grails makes asynchronous has its sessions unbound but open, and closed once the asynchronous context completes.
    • A servlet request that Spring makes asynchronous has the session of the other datasource unbound but open, and closed once the asynchronous context completes. Its default session stays open for Spring to close.
    • The features that run without an asynchronous context, where the sessions are still closed at the handoff, say so in their names.
  • OsivRequestSessionSpec, in the datasources example:
    • A task reads a hasMany and a belongsTo proxy of entities of the default datasource, and a hasMany of an entity of the secondary datasource, after the request has gone asynchronous. It fails without the change, with a LazyInitializationException.
    • A request that goes asynchronous and is timed out by the container keeps both sessions open while it waits, and has them closed after the timeout.
    • The existing asynchronous feature now expects the session open while the request waits, and closed once it has completed.

Without the change, both new MultiDataSourceSessionSpec features, the lazy-load feature, the timeout feature and the existing asynchronous feature fail.

With it, the Hibernate 5 plugin passes 21 tests, datasources 36, and mongodb/hibernate5 4. codeStyle passes 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.

…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 jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A request that finishes without another dispatch leaves the default session open.

released.add(sessionHolder.getSession());
}
else {
super.afterConcurrentHandlingStarted(request);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 jdaugherty left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@matrei

matrei commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

@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.

  • At 10:40:13.499, the cleanup of the previous feature's asynchronous request threw IllegalStateException: The request object has been recycled in GrailsAsyncContext.start, from WebUtils.clearGrailsWebRequest(). That's the error you saw in the log, right before the failing feature.
  • Tomcat's AsyncContextImpl$RunnableWrapper (10.1.61) catches what a Runnable passed to AsyncContext.start() throws. It logs "Error during processing of asynchronous Runnable via AsyncContext.start()", then sets status 500 and the error flag on the coyote response it holds. That coyote request and response belong to the connection's processor and serve its next request. The test client sends its requests over one keep-alive connection.
  • The next request was /requestSession/lazyVolumes, and it got the 500 without its action running. Between 10:40:13.6 and the failure, the log has no SQL and no error from the application's threads, though SQL logging is on.

A probe in the datasources application makes the race deterministic:

  • Request A completes its asynchronous response, and its Runnable then waits 300 ms, so the cleanup runs after Tomcat has recycled the request.
  • Request B goes out at once on the same connection and takes 600 ms.
This pull request (8a00c27) With #17095
Request B, 5 rounds fails in all 5 (IOException: chunked transfer encoding), and the recycled-request error is logged 5 times 200 in all 5, no error

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 finally (d990092 is only on 9.0.x), so #17093 passed by timing, and #17095 fixes it there through the merge-up.

On the default session: agreed, a task that is started without being returned predates this change.

codeconsole
codeconsole previously approved these changes Nov 20, 2026

@codeconsole codeconsole left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 addListener throws IllegalStateException, the sessions are closed at once, as before.
  • onStartAsync registers 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 GrailsOpenSessionInViewInterceptor still 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 MultiDataSourceSessionSpec that call afterConcurrentHandlingStarted on webRequest() ("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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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'
        ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 0043798, as you suggested. The timeout is now 3 s, and the feature checks that both sessions are still open 500 ms after the handoff, before the container times the request out. It now fails on aee4c04, at RequestSessions.asyncDefaultSession.open.

…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.
@github-actions
github-actions Bot dismissed codeconsole’s stale review November 21, 2026 07:36

Dismissed automatically: the pull request changed after this approval, from 8a00c27 to 0043798.

@matrei

matrei commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

@codeconsole thanks for the review. 0043798 takes your points:

  • The class Javadoc of GrailsOpenSessionInViewInterceptor now says that a request that goes asynchronous has its sessions taken off the thread and closed when the asynchronous request completes. The Javadoc of afterConcurrentHandlingStarted names the Callable and the DeferredResult too.
  • The MultiDataSourceSessionSpec features that run on a request without an asynchronous context say so: in their names, or, where the name is about the flush, in the block that checks the closing.
  • A new feature covers the Spring path, and the timeout feature checks that the sessions stay open while the request waits (see the inline replies). I've updated the description.
  • The handoff flush is now in An asynchronous request holds the connection of its request session until it completes #17099.

The Hibernate 5 plugin passes 21 tests, datasources 36 and mongodb/hibernate5 4. Without the change, both new MultiDataSourceSessionSpec features fail, and so do three OsivRequestSessionSpec features, now including the timeout one.

The push dismissed your approval. Could you approve again? Merging still waits for #17095.

@testlens-app

testlens-app Bot commented Nov 21, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 0043798
▶️ Tests: 58128 executed
⚪️ Checks: 57/57 completed


Learn more about TestLens at testlens.app/docs.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A request that finishes without another dispatch leaves the default session open.

released.add(sessionHolder.getSession());
}
else {
super.afterConcurrentHandlingStarted(request);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants