Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions crates/chia-consensus/src/build_compressed_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,27 +212,18 @@ impl BlockBuilder {
/// the first bool indicates whether the bundles was added.
/// the second bool indicates whether we're done
#[pyo3(name = "add_spend_bundles")]
pub fn py_add_spend_bundle(
pub fn py_add_spend_bundles(
&mut self,
py: Python<'_>,
bundles: &Bound<'_, PyList>,
cost: u64,
constants: &ConsensusConstants,
) -> PyResult<(bool, bool)> {
let (added, result) = self.add_spend_bundles(
bundles.iter().map(|item| {
// ideally, the failures in here would be reported back as python
// exceptions, but map() is infallible, so it's not so easy to
// propagate errors back
// TODO: It would be nice to not have to clone the SpendBundle
// here
item.extract::<Bound<'_, SpendBundle>>()
.expect("spend bundle")
.get()
.clone()
}),
cost,
constants,
)?;
let sbs: Vec<SpendBundle> = bundles
.iter()
.map(|sb| Ok(sb.extract()?))
.collect::<PyResult<_>>()?;
let (added, result) = py.detach(|| self.add_spend_bundles(sbs, cost, constants))?;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

GIL release can trigger PyCell borrow errors

Medium Severity

Releasing the GIL around BlockBuilder::add_spend_bundles while holding &mut self (and constants borrowed from Python) can allow other Python threads to run and attempt to use the same BlockBuilder concurrently, which may now fail with a PyCell “already borrowed” runtime error instead of being serialized by the GIL.

Fix in Cursor Fix in Web

let done = matches!(result, BuildBlockResult::Done);
Ok((added, done))
}
Expand Down