GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(itertools): add re-entrancy guard to tee object by ever0de · Pull Request #5931 · RustPython/RustPython

Conversation

@ever0de

@ever0de ever0de commented Jul 10, 2025 •
edited by coderabbitai Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Reference

  • https://github.com/python/cpython/blob/f6650f9ad73359051f3e558c2431a109bc016664/Modules/itertoolsmodule.c#L948-L965
  • https://github.com/python/cpython/blob/f6650f9ad73359051f3e558c2431a109bc016664/Modules/itertoolsmodule.c#L772-L797

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ever0de
fix(itertools): add re-entrancy guard to tee object
aaf1805
@coderabbitai

coderabbitai Bot commented Jul 10, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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
  • X
  • Mastodon
  • Reddit
  • LinkedIn
🪧 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.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ever0de
ever0de force-pushed the fix/itertools-tee-reentrancy branch from 93a39b4 to aaf1805 Compare July 10, 2025 06:32
youknowone
youknowone requested changes Jul 10, 2025
View reviewed changes
Comment thread vm/src/stdlib/itertools.rs
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);
}

@youknowone youknowone Jul 10, 2025

Copy link
Copy Markdown
Member

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

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ever0de reacted with eyes emoji
All reactions
  • 👀 1 reaction
@ever0de
apply feedback PyRwLock -> PyMutex & remove AtomicCell<bool> lock field
17011c4
youknowone
youknowone approved these changes Jul 10, 2025
View reviewed changes

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

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!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@youknowone
youknowone merged commit 8c4c636 into RustPython:main Jul 10, 2025
@coderabbitai coderabbitai Bot mentioned this pull request Feb 11, 2026
Update doctest,test_generators from v3.14.2 and fix generator bugs #7067
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@youknowone youknowone youknowone approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@ever0de @youknowone
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.