From 5494395faecdd07b4473e6c2ef74a9f858c7c1f4 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Fri, 5 Jun 2026 11:55:15 -0400 Subject: [PATCH] Fix potential deadlock by resetting config before finalizing state This commit swaps the order of operations in the finalize function to ensure the global configuration is reset before the state is finalized. Although the write lock on STATE is supposed to be released before the CONFIG lock is acquired, we have observed deadlocks in some builds, likely because STATE does not immediately go out of scope. By ensuring the CONFIG lock is always acquired prior to the STATE lock, we align with the locking order used throughout the rest of the codebase and eliminate this potential deadlock. Assisted-by: Gemini Signed-off-by: Simo Sorce --- src/fns/general.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fns/general.rs b/src/fns/general.rs index 419bacf9..54c61059 100644 --- a/src/fns/general.rs +++ b/src/fns/general.rs @@ -97,9 +97,9 @@ pub extern "C" fn fn_initialize(init_args: CK_VOID_PTR) -> CK_RV { #[inline(always)] fn finalize(_reserved: CK_VOID_PTR) -> Result<()> { - let ret = STATE.wlock()?.finalize(); let mut conf = crate::CONFIG.wlock()?; *conf = Config::new(); + let ret = STATE.wlock()?.finalize(); if ret != CKR_OK { return Err(ret)?; }