fix(itertools): add re-entrancy guard to tee object#5931
fix(itertools): add re-entrancy guard to tee object#5931youknowone merged 2 commits intoRustPython:mainfrom
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
| 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.
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())) |
Reference
Summary by CodeRabbit