-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(itertools): add re-entrancy guard to tee object #5931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(itertools): add re-entrancy guard to tee object #5931
Conversation
WalkthroughThe Changes
Poem
📜 Recent review detailsConfiguration used: .coderabbit.yml 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
93a39b4
to
aaf1805
Compare
vm/src/stdlib/itertools.rs
Outdated
if self.values.read().len() == index { | ||
let result = raise_if_stop!(self.iterable.next(vm)?); | ||
self.values.write().push(result); | ||
if self.locked.swap(true) { | ||
return Err(vm.new_runtime_error("cannot re-enter the tee iterator")); | ||
} | ||
|
||
let result = self.iterable.next(vm); | ||
self.locked.store(false); | ||
|
||
let obj = raise_if_stop!(result?); | ||
self.values.write().push(obj); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While traditional locks prefer to lock the code, Rust locks prefer to lock the data.
After changing PyRwLock to PyMutex, this approach will work
if self.values.read().len() == index { | |
let result = raise_if_stop!(self.iterable.next(vm)?); | |
self.values.write().push(result); | |
if self.locked.swap(true) { | |
return Err(vm.new_runtime_error("cannot re-enter the tee iterator")); | |
} | |
let result = self.iterable.next(vm); | |
self.locked.store(false); | |
let obj = raise_if_stop!(result?); | |
self.values.write().push(obj); | |
} | |
let Some(values) = self.values.try_lock() else { | |
return Err(vm.new_runtime_error("cannot re-enter the tee iterator")); | |
}; | |
if values.len() == index { | |
let obj = raise_if_stop!(self.iterable.next(vm)?); | |
values.push(obj); | |
} | |
Ok(PyIterReturn::Return(values[index].clone())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Reference
Summary by CodeRabbit