Fix collection partial leaking fields between iterations (#64)#65
Open
jujustayfly wants to merge 1 commit into
Open
Fix collection partial leaking fields between iterations (#64)#65jujustayfly wants to merge 1 commit into
jujustayfly wants to merge 1 commit into
Conversation
`PbbuilderTemplate#_render_collection_with_options` allocated a single sub-message and reused it across every iteration of the partial, so any field not actively rewritten on a given iteration carried over from the previous one. In particular: - Repeated message fields written via `pb.field collection do |x| ... end` accumulated across iterations because `_append_repeated` pushes onto the shared message. - Conditionally-written fields leaked the previous element's value into elements that took the silent branch. Reset the working message on the shared `pb` between iterations inside `Pbbuilder::CollectionRenderer#build_rendered_template`, so each render starts with a clean sub-message of the right type. This brings the `partial:`/`collection:` shorthand to parity with the per-element allocation already done by manual iteration through `_append_repeated`. Adds a `_reset_message` helper on `Pbbuilder` (since the class inherits from `BasicObject` and doesn't expose `instance_variable_set`) plus two regression tests covering both leak shapes. Co-authored-by: Cursor <cursoragent@cursor.com>
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.
Summary
Fixes #64.
PbbuilderTemplate#_render_collection_with_optionsallocates one sub-message and reuses it for every iteration of the partial, so fields that aren't actively rewritten leak from the previous element. Two visible shapes:pb.field collection do |x| ... endinside the partial routes through_append_repeated, whichpushes onto the shared message's repeated field, so each iteration sees all prior elements.if/elsifthat writes a field only on some branches inherits the previous element's value on the silent branch.Manual iteration (
pb.field coll.each do |x| ... endat the top level) is unaffected because_append_repeatedalready allocates a fresh sub-message per element.Fix
Reset the working message on the shared
pbbetween iterations insidePbbuilder::CollectionRenderer#build_rendered_template. After snapshotting the body for the just-rendered element, swap@messagefor a fresh sub-message of the right type viapb_parent.new_message_for(field). The first iteration is already clean (initialized in_render_collection_with_options), so every iteration now starts from a fresh message.This brings the
partial:/collection:shorthand to parity with the per-element allocation manual iteration already does in_append_repeated— no extra wrapper allocation, just one extra protobuf message per element (same cost as the workaround documented in the issue).A small
_reset_message(message)helper was added toPbbuilderbecause the class inherits fromBasicObjectand doesn't exposeinstance_variable_set(calling it would route throughmethod_missing→set!and raise "Unknown field").