Skip to content

No std pymutex - nonpoisoning variant of PyMutex - #6437

Open
Person-93 wants to merge 8 commits into
PyO3:mainfrom
Person-93:no_std_pymutex
Open

Person-93 wants to merge 8 commits into
PyO3:mainfrom
Person-93:no_std_pymutex

Conversation

@Person-93

Copy link
Copy Markdown
Contributor

Closes #6436

@Person-93 Person-93 added CI-skip-changelog Skip checking changelog entry no_std Require `no_std` builds to pass labels Sep 19, 2026
@Person-93 Person-93 changed the title No std pymutex No std pymutex - nonpoisoning variant of PyMutex Sep 19, 2026
@Person-93
Person-93 force-pushed the no_std_pymutex branch 2 times, most recently from 236c251 to f600212 Compare September 19, 2026 02:12
@Person-93 Person-93 removed the CI-skip-changelog Skip checking changelog entry label Sep 19, 2026
@Person-93
Person-93 marked this pull request as ready for review September 19, 2026 02:43

@davidhewitt davidhewitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems reasonable to me; did you have a particular use case or just fed up of .lock().unwrap()?

Comment thread src/sync/nonpoison.rs
Comment thread src/sync.rs
Comment thread src/sync/nonpoison.rs
}

#[cfg(test)]
mod tests {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do you think we could re-use the existing mutex tests using PyMutexTrait? Should we? (Except for poisoning tests.)

@Person-93 Person-93 Sep 22, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure because they aren't exactly the same. We could use some trait to hide the differences but that feels a bit more error prone than I'd like for a test case. Like the mini-framework itself would need testing.

On the other hand, any future changes to these tests would need to be duplicated in both file.

m2.inner().get(),
)
};
f(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder, if f panics should the mutex(es) be poisoned (if using the poisoning variant)? Probably yes. cc @ngoldbaum we might have overlooked this in the original introduction of this function.

I guess we could change the behaviour to poison on unwind, it would probably not break anyone and we could document it in the migration guide?

@Person-93 Person-93 Sep 21, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It looks like it already does poison on unwind.

pyo3/src/sync/mutex.rs

Lines 247 to 253 in 58cb851

impl<'a, T: ?Sized> Drop for PyMutexGuard<'a, T> {
fn drop(&mut self) {
self.inner.poison.done(&self.poison);
// SAFETY: valid pointer to mutex passed to `PyMutex_Unlock`
unsafe { crate::ffi::PyMutex_Unlock(self.inner.mutex.get()) };
}
}

pyo3/src/sync/mutex.rs

Lines 52 to 57 in 58cb851

#[cfg(panic = "unwind")]
fn done(&self, guard: &Guard) {
if !guard.panicking && thread::panicking() {
self.failed.store(true, Ordering::Relaxed);
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right, the critical section API bypasses it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you want that as part of this PR? or in a followup?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no_std Require `no_std` builds to pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

no_std support for PyMutex

2 participants