Replace unsafe pointer cast with PyMutex in PyCode by youknowone ยท Pull Request #7055 ยท RustPython/RustPython
Repository navigation
Replace unsafe pointer cast with PyMutex in PyCode - #7055
Merged
youknowone merged 1 commit into RustPython:main from youknowone:pycode-path Feb 9, 2026
Merged
Replace unsafe pointer cast with PyMutex in PyCode #7055 youknowone merged 1 commit into RustPython:main from youknowone:pycode-path
Conversation
youknowone
commented
Feb 8, 2026
·
edited by coderabbitai
Bot
Loading
Copy link Copy Markdown
Member
Add source_path: PyMutex<&'static PyStrInterned> field to PyCode for interior mutability, replacing the UB-inducing #[allow(invalid_reference_casting)] + write_volatile pattern in update_code_filenames. Update all 12 read sites across the codebase to use the new source_path() method.
Summary by CodeRabbit
- Refactor
- Improved thread-safety and safer handling of code object source paths across the runtime by replacing direct internal field access and in-place mutations with controlled accessors and setters, reducing unsafe mutations and standardizing path retrieval in traceback, import, and warning/reporting flows.
coderabbitai
Bot
commented
Feb 8, 2026
·
edited
Loading
Copy link Copy Markdown
Contributor
๐ Walkthrough
WalkthroughEncapsulate PyCode.source_path behind a PyMutex with getter/setter and replace direct field accesses across the VM to use the new methods; PyCode construction updated to use PyCode::new. Changes
Estimated code review effort๐ฏ 2 (Simple) | โฑ๏ธ ~12 minutes Possibly related PRs
Suggested reviewers
Poem๐ฅ Pre-merge checks | โ 2 | โ 1 โ Failed checks (1 warning)
โ๏ธ Tip: You can configure your own custom pre-merge checks in the settings. โจ 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. โค๏ธ ShareComment |
Bot
reviewed Feb 9, 2026
View reviewed changes
coderabbitai
Bot
left a comment
Copy link Copy Markdown
Contributor
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more .
Actionable comments posted: 1
๐ค Fix all issues with AI agentsIn `@crates/vm/src/builtins/code.rs`:
- Around line 337-351: Replace direct reads of the CodeObject field
"frame.code.source_path" with the PyCode accessor "frame.code.source_path()" so
the mutex-protected value from PyCode::source_path is used; search for
occurrences of "frame.code.source_path" in faulthandler.rs and update them to
call the accessor (leveraging the existing PyCode::source_path and
set_source_path methods).
Replace unsafe pointer cast with PyMutex in PyCode
5103e88
Add `source_path: PyMutex<&'static PyStrInterned>` field to `PyCode` for interior mutability, replacing the UB-inducing `#[allow(invalid_reference_casting)]` + `write_volatile` pattern in `update_code_filenames`. Update all read sites across the codebase to use the new `source_path()` accessor method.youknowone force-pushed the pycode-path branch from
66eb96f to
5103e88
Compare
February 9, 2026 03:57 coderabbitai
Bot
reviewed Feb 9, 2026
View reviewed changes
coderabbitai
Bot
left a comment
Copy link Copy Markdown
Contributor
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more .
Actionable comments posted: 1
๐ค Fix all issues with AI agentsIn `@crates/stdlib/src/faulthandler.rs`:
- Around line 254-256: dump_frame_from_raw is called from the signal handler
faulthandler_fatal_error but calls frame.code.source_path() which acquires a
PyMutex and can deadlock; make this signal-safe by avoiding any mutex
acquisition: either add a signal-safe cached string pointer on PyCode (e.g., an
atomic/AtomicPtr holding a nul-terminated C string updated outside signal
handlers and read here) and read that in dump_frame_from_raw, or replace the
call to source_path() with a non-blocking attempt (call try_lock() on the
PyCode/source_path mutex and if it fails print a fallback like "<unknown>" or
the raw pointer/address), updating references to Frame/ PyCode/ source_path
accordingly so dump_frame_from_raw never blocks in a signal context.
Comment thread
crates/stdlib/src/faulthandler.rs
Comment on lines
254
to
256
| fn dump_frame_from_raw(fd: i32, frame: &Frame) { | ||
| let filename = frame.code.source_path.as_str(); | ||
| let filename = frame.code.source_path () .as_str(); | ||
| let funcname = frame.code.obj_name.as_str(); |
dc2d235
into
RustPython : main
Feb 9, 2026
youknowone
deleted the
pycode-path
branch
February 9, 2026 05:01
jaens commented Feb 9, 2026
Copy link Copy Markdown
|
@youknowone doesn't Since the lifetime is What the update code does is actually an atomic compare and exchange, I think. |
youknowone commented Feb 9, 2026
Copy link Copy Markdown
Member
Author
|
@jaens Thanks! That sounds right |
Bot
mentioned this pull request Feb 9, 2026 Replace unsafe pointer cast with AtomicPtr in PyCode #7063 Merged youknowone added a commit that referenced this pull request Sep 16, 2026
Replace unsafe pointer cast with PyMutex in PyCode (#7055)
bc4e396
Add `source_path: PyMutex<&'static PyStrInterned>` field to `PyCode` for interior mutability, replacing the UB-inducing `#[allow(invalid_reference_casting)]` + `write_volatile` pattern in `update_code_filenames`. Update all read sites across the codebase to use the new `source_path()` accessor method.Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment