Skip to content

Commit 6954733

Browse files
reaperhulkclaude
andauthored
Release the GIL during DH and DSA parameter generation (#15364)
* Release the GIL during DH and DSA parameter generation DH parameter generation runs for seconds to minutes at common key sizes and DSA parameter generation for hundreds of milliseconds, all while holding the GIL and freezing every other thread in the process. Wrap the OpenSSL calls in Python::detach; neither closure touches Python objects. Deliberately NOT detached: EC keygen (~20 us), and DH/DSA generate_private_key (sub-millisecond to low-millisecond). Re-attaching after a detach can wait up to sys.getswitchinterval() (5 ms default) when another thread is CPU-bound, so detaching operations much shorter than the switch interval makes them dramatically slower under contention (measured: EC keygen 55 us -> 5.1 ms per op with a busy background thread). Only operations that run for at least tens of milliseconds are worth detaching here. Benchmark (Linux x86-64, CPython 3.11, release build; background Python thread's progress while the main thread generates): dsa.generate_parameters(2048): background thread throughput 1.0M iters/s -> 27M iters/s (i.e. other threads now actually run) ec.generate_private_key / generate_private_key: unchanged Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01C5bsKENedjVD9gcLApf1x4 * Remove comments per review Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01C5bsKENedjVD9gcLApf1x4 --------- Co-authored-by: Claude <[email protected]>
1 parent 6893b94 commit 6954733

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

‎src/rust/src/backend/dh.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct DHParameters {
3434
#[pyo3::pyfunction]
3535
#[pyo3(signature = (generator, key_size, backend=None))]
3636
fn generate_parameters(
37+
py: pyo3::Python<'_>,
3738
generator: u32,
3839
key_size: u32,
3940
backend: Option<pyo3::Bound<'_, pyo3::PyAny>>,
@@ -54,7 +55,8 @@ fn generate_parameters(
5455
));
5556
}
5657

57-
let dh = openssl::dh::Dh::generate_params(key_size, generator)
58+
let dh = py
59+
.detach(|| openssl::dh::Dh::generate_params(key_size, generator))
5860
.map_err(|_| pyo3::exceptions::PyValueError::new_err("Unable to generate DH parameters"))?;
5961
Ok(DHParameters { dh })
6062
}

‎src/rust/src/backend/dsa.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ pub(crate) fn public_key_from_pkey(
5353
}
5454

5555
#[pyo3::pyfunction]
56-
fn generate_parameters(key_size: u32) -> CryptographyResult<DsaParameters> {
57-
let dsa = openssl::dsa::Dsa::generate_params(key_size)?;
56+
fn generate_parameters(py: pyo3::Python<'_>, key_size: u32) -> CryptographyResult<DsaParameters> {
57+
let dsa = py.detach(|| openssl::dsa::Dsa::generate_params(key_size))?;
5858
Ok(DsaParameters { dsa })
5959
}
6060

0 commit comments

Comments
 (0)