◐ Shell
clean mode source ↗

Switch to newer thread::LocalKey convenience methods by coolreader18 · Pull Request #6123 · RustPython/RustPython

Walkthrough

Introduces scoped TLS and scopeguard dependencies. Refactors multiple thread-local RefCell access patterns to with_borrow/with_borrow_mut helpers. Converts VM current-thread storage to scoped TLS, revising VM entry/access control flow. Adjusts sys/thread context accessors and cleanup logic. Minor iterator-based update in Levenshtein buffer initialization.

Changes

Cohort / File(s) Summary
Dependencies
Cargo.toml, vm/Cargo.toml
Added dependencies: scoped-tls and scopeguard; wired vm crate to workspace versions.
Thread-local borrow refactors
common/src/str.rs, stdlib/src/contextvars.rs, vm/src/stdlib/sys.rs, vm/src/stdlib/thread.rs, wasm/lib/src/vm_class.rs
Replaced RefCell borrow patterns with with_borrow/with_borrow_mut helpers; streamlined reads/writes; adjusted cleanup in thread sentinel handling; minor iterator initialization optimization in Levenshtein buffer.
VM TLS mechanism
vm/src/vm/thread.rs
Migrated VM_CURRENT from manual RefCell TLS to scoped_tls::scoped_thread_local; updated enter/access helpers to use scoped set/with and scopeguard for stack unwind.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Caller
  participant VM as VirtualMachine
  participant TLS as VM_CURRENT (scoped TLS)
  participant Stack as VM_STACK

  rect rgb(240,248,255)
    note over Caller,VM: enter_vm(vm, f)
    Caller->>VM: enter_vm(vm, f)
    VM->>Stack: push(vm)
    VM->>TLS: set(vm) {scoped}
    activate TLS
    TLS-->>VM: run f()
    VM->>VM: scopeguard defer Stack.pop()
    deactivate TLS
    VM-->>Caller: result
  end

  rect rgb(255,250,240)
    note over Caller,TLS: with_current_vm(f)
    Caller->>TLS: with(f) (requires is_set)
    TLS-->>Caller: f(&VM)
  end
Loading
sequenceDiagram
  autonumber
  participant Py as Python code
  participant CV as ContextVar
  participant Ctxs as CONTEXTS (TLS)
  participant Ctx as PyContext

  rect rgb(245,255,250)
    note over Py,Ctxs: current(vm)
    Py->>Ctxs: with_borrow_mut
    alt last exists
      Ctxs-->>Py: &last
    else create
      Ctxs->>Ctx: new default
      Ctxs->>Ctxs: push(Ctx)
      Ctxs-->>Py: &first
    end
  end

  rect rgb(250,245,255)
    note over Py,CV: CV.get(default)
    Py->>Ctxs: with_borrow
    Ctxs-->>Py: last context ref
    Py->>CV: check cache or lookup vars
    CV-->>Py: value or default
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • Wtf8-compatible fixes #5985 — Also modifies common/src/str.rs Levenshtein logic (byte-slice change) touching the same buffer init area.

Suggested reviewers

  • youknowone

Poem

I nuzzle the stack where VMs now dwell,
Scoped threads whisper, “all is well.”
Contexts hop neatly, borrow-light,
Guards stand watch through unwind night.
With paws on hooks and buffers anew—
I thump: refactors done, carrots due! 🥕🐇

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit 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.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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.