Bugfix by stopping find_in_batches using the records after yielding. #1807

Merged
merged 1 commit into from Jul 14, 2011

Conversation

Projects
None yet
3 participants
Contributor

caius commented Jun 21, 2011

I've submitted this patch before (pull request 1435) but decided redefining String#id in the test was a bad idea, so I've refactored it slightly to achieve the same thing using a mocha stub instead.

Currently if the code that calls find_in_batches modifies the yielded array in place then find_in_batches can enter an infinite loop searching with ruby object ids in the database instead of the primary key of records in the database. This happens because it naively assumes the yielded array hasn't been modified before calling id on the last object in the array. And ruby (1.8 at least) alias' id to object_id so an integer is still returned no matter what the last object is.

By moving finding the id of the last object before yielding the array of records it means the calling code can do whatever it wants to the array in terms of modifying it in place, and find_in_batches doesn't care. Also moved the count of the array records before yielding in case elements are removed or added when yielded.

@caius caius Stop find_in_batches using the records after yielding.
Currently if the code that calls .find_in_batches modifies the yielded array in place then .find_in_batches can enter an infinite loop searching with ruby object ids in the database instead of the primary key of records in the database. This happens because it naively assumes the yielded array hasn't been modified before calling #id on the last object in the array. And ruby (1.8 at least) alias' #id to #object_id so an integer is still returned no matter what the last object is.

By moving finding the #id of the last object before yielding the array it means the calling code can do whatever it wants to the array in terms of modifying it in place, and .find_in_batches doesn't care.
96be08d
Contributor

tekin commented Jun 22, 2011

looks good to me.

@jake3030 jake3030 pushed a commit to jake3030/rails that referenced this pull request Jun 28, 2011

@airblade @lifo airblade + lifo Truncate helper accepts a :separator for a more legible result [#1807
…state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
5e190ef

@spastorino spastorino added a commit that referenced this pull request Jul 14, 2011

@spastorino spastorino Merge pull request #1807 from caius/find_in_batches_id_bug
Bugfix by stopping find_in_batches using the records after yielding.
d632e92

@spastorino spastorino merged commit d632e92 into rails:master Jul 14, 2011

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