Fix loader for kwargs mutation ruby4#207
Open
AlfonsoUceda wants to merge 3 commits intoShopify:mainfrom
Open
Conversation
Ruby 4.0 shares the internal kwargs hash when `...` is forwarded to multiple call sites in the same method. In `Loader.for(...)`, the kwargs captured by `loader_key_for(...)` are emptied when `new(...)` destructures them in `initialize`, corrupting the stored loader key and breaking batch grouping. Explicit `*group_args, **group_kwargs, &block` ensures each call site receives an independent copy of the kwargs hash.
Author
|
I have signed the CLA! |
Reproduces a bug where kwargs forwarded through ... get mutated when destructured and re-assembled by an intermediary method (e.g. RecordLoader.load → .for(model, key: key, ...)). Direct .for() calls don't trigger this.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hello!
I'm creating this PR because I've found a problem using this gem with Ruby 4 and a specific way of using loaders. The problem seems with the use of
...operator forwarding in ruby 4 and the loader class.On Ruby 4.0, when kwargs are destructured by a caller method and re-passed to Loader.for(...), the ... forwarding shares the internal kwargs hash across all expansion sites. When new(...) destructures them in initialize, it empties the same hash already stored in the executor's loader key, breaking loader reuse.
This only manifests when .for() is called through an intermediary that captures kwargs as named parameters and re-passes the, a common pattern in subclasses:
Direct .for(key: 'value')calls are unaffected, which is why the bug is subtle.I've made thanks to Claude that helped me to identify this problem in our codebase and pointed out to this.