Skip to content

Make DetachedCriteria state read-only and snapshot-based (deferred criteria design, 9.0.x) #16867

Open

Description

@borinquenkid

Follow-up design issue for 9.0.x. The bugs below were each fixed locally (#16814 and its PRs #16818 and #16820, plus #16816); the pattern behind them is what this issue is about.

What keeps going wrong

DetachedCriteria exposes its internal state as live, mutable lists, and every datastore reads and sometimes writes them:

  • getCriteria() and getProjections() return the field lists themselves.
  • A deferred criteria (whereLazy, buildLazy) is materialized by applyLazyCriteria(), which nulls lazyQuery and replays the closure into the same object. The first call mutates shared state, which is the thread race in Datastore: Thread-safe deferred criteria (whereLazy) materialization on copies #16814.
  • Nothing makes a reader materialize first. About ten readers (the H5 and H7 builders, JpaQueryBuilder, DynamicFinder, the Mongo, Neo4j and SimpleMap queries) each have to remember, and a missed one silently drops the restriction.
  • clone() is shallow: junctions (or, and, not) and the projection list are shared by reference, so a derived criteria can edit a node the original still holds.
  • The H7 PredicateGenerator adds a default projection to the list it was handed, so even a read mutates the criteria.
  • applyLazyCriteria() exists twice (AbstractDetachedCriteria and grails.gorm.DetachedCriteria).

Where it came from

The lazy state, the live-list getters and the shallow clone are inherited from the original grails-data-mapping code, integrated in April 2025. What we added on 8.0.x: the H7 translators that write to the lists (April 2026) and the H7 whereLazy override that routes where queries through deferred criteria (April 2026). That second change made a rarely used path the common one on Hibernate 7, which is why the bugs surfaced now.

Proposal (9.0.x, breaking)

  • Expose criteria and projections as read-only views, or as an immutable snapshot taken at execution.
  • Make laziness a value: materialize once into a snapshot under a lock and hand out copies, instead of replaying into the live object.
  • Deep-copy junctions and associations in clone() with one identity map.
  • Translators build their own projection list and never write to the criteria.
  • One applyLazyCriteria().

Questions

  • Is a breaking change to QueryableCriteria.getCriteria() / getProjections() acceptable in 9.0, or should the views be a new method with the old ones deprecated?
  • Should RxGORM follow in the same change?

Interim: #16820 stays bounded (recursive junction copy, H5 materialization, no new public API) so 8.0.x gets the fixes without this redesign.

Activity

  1. borinquenkid commented on Nov 5, 2026

    @borinquenkid
    MemberAuthor

    Update from the fix of #16820 (df1a73a, 6d2431c). Doing it for real showed the design problem is wider than the lists: a criteria is a builder with two kinds of ways in, and each reviewer round fixed the case the last one tried. The in-place methods (the criterion DSL, order, sort, join, select, setAlias, createAlias, the projection methods) and the clone-and-modify methods (max, offset, withConnection, buildLazy, where) each reach the state, and so do the getters and the translators in the H5 builder, the H7 handlers, JpaQueryBuilder, DynamicFinder, MongoDB, Neo4j and the in-memory datastore. Only add, methodMissing and handleJunction applied the deferred closure.

    What #16820 does for 8.0.x is bounded: one chokepoint (applyLazyCriteria(), once, under a lock) called first by every method that touches state, queries read a copy, and a spec lists the ways in. That fixed the first-use race and the ordering, but it keeps the shape: mutable lists behind getters, a shallow-prone clone(), and DetachedProjections sharing a list. The proposal here still stands for 9.0.x: an immutable builder in which every method returns a new criteria and the deferral is part of that immutable state, so there is no "first use" to get wrong.

    https://claude.ai/code/session_01Dmki4Tgxp7dLBhzpe2xT93

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions