Skip to content

CHIA-3899 Release the GIL when calling BlockBuilder's add_spend_bundles#1361

Draft
AmineKhaldi wants to merge 1 commit intoChia-Network:mainfrom
AmineKhaldi:add_spend_bundles_drop_gil
Draft

CHIA-3899 Release the GIL when calling BlockBuilder's add_spend_bundles#1361
AmineKhaldi wants to merge 1 commit intoChia-Network:mainfrom
AmineKhaldi:add_spend_bundles_drop_gil

Conversation

@AmineKhaldi
Copy link
Copy Markdown
Contributor

@AmineKhaldi AmineKhaldi commented Feb 22, 2026

Note

Low Risk
Binding-layer change that only affects Python threading behavior; minimal logic changes beyond input extraction and running the Rust call without the GIL.

Overview
Updates the BlockBuilder Python binding for add_spend_bundles to release the Python GIL while executing the underlying Rust add_spend_bundles logic.

The wrapper now collects the input PyList into a Vec<SpendBundle> with proper PyResult error propagation, then runs the block-building work inside py.detach(...) to avoid blocking other Python threads.

Written by Cursor Bugbot for commit d3f2f11. This will update automatically on new commits. Configure here.

@AmineKhaldi AmineKhaldi self-assigned this Feb 22, 2026
@AmineKhaldi AmineKhaldi added the Changed Required label for PR that categorizes merge commit message as "Changed" for changelog label Feb 22, 2026
@AmineKhaldi AmineKhaldi force-pushed the add_spend_bundles_drop_gil branch from 9ede6bb to 7d65920 Compare February 22, 2026 11:37
@coveralls-official
Copy link
Copy Markdown

coveralls-official Bot commented Feb 22, 2026

Pull Request Test Coverage Report for Build 22314709672

Details

  • 7 of 7 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.001%) to 80.698%

Totals Coverage Status
Change from base Build 22299183288: 0.001%
Covered Lines: 14303
Relevant Lines: 17724

💛 - Coveralls

@AmineKhaldi AmineKhaldi changed the title Release the GIL when calling BlockBuilder's add_spend_bundles CHIA-3899 Release the GIL when calling BlockBuilder's add_spend_bundles Feb 22, 2026
@AmineKhaldi AmineKhaldi marked this pull request as ready for review February 22, 2026 12:50
arvidn
arvidn previously approved these changes Feb 23, 2026
Copy link
Copy Markdown
Contributor

@arvidn arvidn left a comment

Choose a reason for hiding this comment

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

looks good. I'm a little bit worried that this is going to be a bit slower, but probably doesn't matter much. I suppose we don't have any python benchmark that exercises this

Comment thread crates/chia-consensus/src/build_compressed_block.rs Outdated
@AmineKhaldi AmineKhaldi force-pushed the add_spend_bundles_drop_gil branch 2 times, most recently from 3abc0cc to fa4815c Compare February 23, 2026 15:00
Comment thread crates/chia-consensus/src/build_compressed_block.rs Outdated
@AmineKhaldi AmineKhaldi force-pushed the add_spend_bundles_drop_gil branch from fa4815c to d3f2f11 Compare February 23, 2026 16:18
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

.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

@arvidn
Copy link
Copy Markdown
Contributor

arvidn commented Feb 23, 2026

it's not clear what this achieves, given that we call this from the main python thread anyway

@arvidn arvidn marked this pull request as draft February 24, 2026 13:07
@arvidn
Copy link
Copy Markdown
Contributor

arvidn commented Feb 24, 2026

I moved this to draft as I don't think we should land this without some more discussion

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

Labels

Changed Required label for PR that categorizes merge commit message as "Changed" for changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants