Skip to content

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

Merged
merged 2 commits into from
Jul 10, 2025

Conversation

ever0de
Copy link
Contributor

@ever0de ever0de commented Jul 10, 2025

Reference

Summary by CodeRabbit

  • Bug Fixes
    • Improved stability of the tee iterator by preventing concurrent or recursive access, reducing the risk of runtime errors or inconsistent behavior. Users will now receive a clear error if re-entrancy is attempted.

Copy link
Contributor

coderabbitai bot commented Jul 10, 2025

Walkthrough

The PyItertoolsTeeData struct's values field was changed from a read-write lock to a mutex. The get_item method now uses a try_lock() on the mutex to detect re-entrance, returning a runtime error if the lock is already held, and caches iterator results accordingly.

Changes

File(s) Change Summary
vm/src/stdlib/itertools.rs Changed values from PyRwLock<Vec<PyObjectRef>> to PyMutex<Vec<PyObjectRef>> in PyItertoolsTeeData. Updated new and get_item to use mutex locking with try_lock() and return error on re-entrance.

Poem

A mutex now guards the tee's bright trail,
No more re-entry to make it fail.
Cached treasures locked tight in a row,
One hop at a time, steady and slow.
🐇🔐🌿


📜 Recent review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between aaf1805 and 17011c4.

📒 Files selected for processing (1)
  • vm/src/stdlib/itertools.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • vm/src/stdlib/itertools.rs
✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@ever0de ever0de force-pushed the fix/itertools-tee-reentrancy branch from 93a39b4 to aaf1805 Compare July 10, 2025 06:32
Comment on lines 1201 to 1212
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);
}

Copy link
Member

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

Suggested change
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()))

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@youknowone youknowone merged commit 8c4c636 into RustPython:main Jul 10, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants