From f2c3d19e64d34b733247e31cd60e346995eb8842 Mon Sep 17 00:00:00 2001 From: Kevin King <4kevinking@gmail.com> Date: Tue, 5 Nov 2024 10:53:30 -0800 Subject: [PATCH] Release GIL during cipher update --- src/rust/src/backend/ciphers.rs | 34 ++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/rust/src/backend/ciphers.rs b/src/rust/src/backend/ciphers.rs index 8c90fe32e3d8..379dc0269977 100644 --- a/src/rust/src/backend/ciphers.rs +++ b/src/rust/src/backend/ciphers.rs @@ -179,22 +179,26 @@ impl CipherContext { } let mut total_written = 0; - for chunk in data.chunks(1 << 29) { - // SAFETY: We ensure that outbuf is sufficiently large above. - unsafe { - let n = if self.py_mode.bind(py).is_instance(&types::XTS.get(py)?)? { - self.ctx.cipher_update_unchecked(chunk, Some(&mut buf[total_written..])).map_err(|_| { - pyo3::exceptions::PyValueError::new_err( - "In XTS mode you must supply at least a full block in the first update call. For AES this is 16 bytes." - ) - })? - } else { - self.ctx - .cipher_update_unchecked(chunk, Some(&mut buf[total_written..]))? - }; - total_written += n; + let is_xts = self.py_mode.bind(py).is_instance(&types::XTS.get(py)?)?; + py.allow_threads::, _>(|| { + for chunk in data.chunks(1 << 29) { + // SAFETY: We ensure that outbuf is sufficiently large above. + unsafe { + let n = if is_xts { + self.ctx.cipher_update_unchecked(chunk, Some(&mut buf[total_written..])).map_err(|_| { + pyo3::exceptions::PyValueError::new_err( + "In XTS mode you must supply at least a full block in the first update call. For AES this is 16 bytes." + ) + })? + } else { + self.ctx + .cipher_update_unchecked(chunk, Some(&mut buf[total_written..]))? + }; + total_written += n; + } } - } + Ok(()) + })?; Ok(total_written) }