Skip to content

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Dec 25, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced VM stability by improving internal lock management in classmethod, property, and partial function operations to prevent potential deadlocks.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 25, 2025

📝 Walkthrough

Walkthrough

Three builtin and stdlib modules refactored to prevent deadlocks by cloning callable objects and values under read locks, then releasing locks before invoking Python code. Changes affect classmethod descriptor access, property getter/setter/deleter invocation, and partial function callable/representation logic.

Changes

Cohort / File(s) Change Summary
Deadlock Prevention via Lock Release Before Python Invocation
crates/vm/src/builtins/classmethod.rs, crates/vm/src/builtins/property.rs, crates/vm/src/stdlib/functools.rs
Systematically clone callable objects and descriptor values while holding read locks, then release locks before invoking Python code. In classmethod.rs, callable is cloned in descr_get prior to "get" attribute fetch. In property.rs, getter/setter/deleter are cloned in descriptor_get and related paths; property name retrieval and abstract method checks updated to use cloned lock reads. In functools.rs, PyPartial's func/args/keywords cloned under read lock in Callable and Representable implementations before Python invocation. Pattern prevents deadlocks from re-entrant Python calls.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • implement more property features  #5828: Modifies property.rs descr_get and related property-access logic with similar lock-release patterns for getter/setter/deleter access.
  • fix pyexpat hang #6507: Implements the same deadlock prevention pattern — clone handler under read lock and release before invoking Python code.
  • PyPayload::into_ref #3744: Modifies classmethod.rs descr_get callable access and PyBoundMethod construction similarly.

Poem

🐰 A lock held tight can cause a knot,
When Python calls within its slot.
Release it first, the deadlock flees—
Clone and run with gentle ease! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix potential deadlocks' accurately reflects the main objective of the changeset, which involves refactoring lock handling across three files to release mutexes before invoking Python code and prevent deadlocks.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • 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

Comment @coderabbitai help to get the list of available commands and usage tips.

@youknowone youknowone marked this pull request as ready for review December 25, 2025 05:29
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
crates/vm/src/builtins/classmethod.rs (2)

142-158: Remaining potential deadlock: lock held during get_attr calls.

These getters still hold the lock while calling Python code, which could cause the same deadlock this PR aims to fix:

// Line 143 - lock held during get_attr
self.callable.lock().get_attr("__module__", vm)

Consider applying the same clone-and-release pattern:

🔎 Proposed fix
     #[pygetset]
     fn __module__(&self, vm: &VirtualMachine) -> PyResult {
-        self.callable.lock().get_attr("__module__", vm)
+        let callable = self.callable.lock().clone();
+        callable.get_attr("__module__", vm)
     }

     #[pygetset]
     fn __qualname__(&self, vm: &VirtualMachine) -> PyResult {
-        self.callable.lock().get_attr("__qualname__", vm)
+        let callable = self.callable.lock().clone();
+        callable.get_attr("__qualname__", vm)
     }

     #[pygetset]
     fn __name__(&self, vm: &VirtualMachine) -> PyResult {
-        self.callable.lock().get_attr("__name__", vm)
+        let callable = self.callable.lock().clone();
+        callable.get_attr("__name__", vm)
     }

     #[pygetset]
     fn __annotations__(&self, vm: &VirtualMachine) -> PyResult {
-        self.callable.lock().get_attr("__annotations__", vm)
+        let callable = self.callable.lock().clone();
+        callable.get_attr("__annotations__", vm)
     }

169-175: Remaining potential deadlock: lock held during set_attr.

The setter holds the lock while calling set_attr, which can invoke Python code.

🔎 Proposed fix
     #[pygetset(setter)]
     fn set___isabstractmethod__(&self, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
-        self.callable
-            .lock()
-            .set_attr("__isabstractmethod__", value, vm)?;
+        let callable = self.callable.lock().clone();
+        callable.set_attr("__isabstractmethod__", value, vm)?;
         Ok(())
     }
📜 Review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 92acf33 and 36fd0b0.

📒 Files selected for processing (3)
  • crates/vm/src/builtins/classmethod.rs
  • crates/vm/src/builtins/property.rs
  • crates/vm/src/stdlib/functools.rs
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.rs: Follow the default rustfmt code style by running cargo fmt to format Rust code
Always run clippy to lint Rust code (cargo clippy) before completing tasks and fix any warnings or lints introduced by changes
Follow Rust best practices for error handling and memory management
Use the macro system (pyclass, pymodule, pyfunction, etc.) when implementing Python functionality in Rust

Files:

  • crates/vm/src/builtins/classmethod.rs
  • crates/vm/src/stdlib/functools.rs
  • crates/vm/src/builtins/property.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Ensure compilation on various targets
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Check Rust code with clippy
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Run tests under miri
🔇 Additional comments (7)
crates/vm/src/builtins/classmethod.rs (1)

60-66: LGTM on the deadlock fix pattern.

The clone-and-release pattern correctly prevents deadlocks by ensuring no lock is held when invoking Python code (get_attr, call).

crates/vm/src/stdlib/functools.rs (2)

250-281: LGTM on the deadlock fix in call.

The scoped block pattern ensures the read lock is released before any Python code executes (func.call). The cloned values are safely used after lock release.


286-320: LGTM on the deadlock fix in repr_str.

Same correct clone-and-release pattern applied to the repr logic, ensuring locks are released before calling repr(vm) on func, args, and keyword values.

crates/vm/src/builtins/property.rs (4)

58-64: LGTM on the deadlock fix in descr_get.

Correctly clones the getter while holding the read lock (via the if let Some(getter) = zelf.getter.read().clone() pattern), releases the lock, then calls the getter.


74-81: LGTM on the deadlock fix in get_property_name.

Both the name field and getter are cloned before any Python code (get_attr) is invoked.


108-126: LGTM on the deadlock fix in descr_set.

Both the setter and deleter paths correctly clone under lock before calling Python code.


279-311: LGTM on the deadlock fixes in __isabstractmethod__ accessors.

Both the getter and setter correctly clone the relevant fields before invoking Python code (get_attr, try_to_bool, set_attr).

@youknowone youknowone merged commit 7eb0fe4 into RustPython:main Dec 25, 2025
13 checks passed
@youknowone youknowone deleted the potential-deadlock branch December 25, 2025 06:49
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.

1 participant