discourse/spec/lib/service
Loïc Guitaut f92a035936
DEV: Fix isolation bugs in service framework's each step (#40311)
Three independent issues in `each`'s isolation, each surfacing as soon
as the step is used outside the simplest shapes:

1. Nested `each` blocks crashed inside `with_isolation`'s `ensure`. The
isolation kept its snapshot in a single slot that the inner call would
overwrite then null, so the outer's cleanup ran against `nil`. The
snapshot now lives on a stack, making isolation re-entrant.

2. ActiveRecord models in the context lost their primary key inside an
`each` block. The snapshot used `deep_dup`, which recurses into AR
objects via `dup` and AR's `dup` returns an unpersisted copy with `id ==
nil`. Basic shapes like `model :user; each :things do ... end` silently
swapped the real user for a useless ghost. The deep copy was originally
an attempt at mutation isolation for collections, but that contract was
never documented, its supporting spec was removed before the original PR
merged, and the rest of the framework already lets steps mutate what
they receive. A shallow dup matches the documented "variables set inside
the loop don't leak" guarantee and stops corrupting models.

3. Non-persisted state leaked between iterations. The whole loop ran
inside one isolation, so iteration N could read scratch values iteration
N-1 had left around. Each iteration now gets its own isolation. Steps
inside the same iteration still share state freely (step 2 can act on
what step 1 produced); only cross-iteration carry-over now requires
`persist:`, which makes that dependency visible at the `each`
declaration.
2026-05-27 09:47:34 +02:00
..
base DEV: Fix isolation bugs in service framework's each step (#40311) 2026-05-27 09:47:34 +02:00
contract_base_spec.rb DEV: Handle nested attributes in contracts (#36348) 2025-12-12 11:41:48 +01:00
nested_contract_type_spec.rb DEV: Handle nested attributes in contracts (#36348) 2025-12-12 11:41:48 +01:00
runner_spec.rb DEV: Fix the lock step when using contracts (#38019) 2026-02-24 12:30:38 +01:00
steps_inspector_spec.rb DEV: Add each step to service framework for collection iteration (#38759) 2026-04-03 09:44:03 +02:00