33//! Session management functions
44
55use crate :: context:: Function ;
6- use crate :: error:: { Result , Rv } ;
6+ use crate :: error:: { Error , Result , Rv , RvError } ;
77use crate :: session:: { Session , SessionInfo , UserType } ;
88use crate :: types:: { AuthPin , RawAuthPin } ;
99
@@ -16,19 +16,13 @@ use std::convert::{TryFrom, TryInto};
1616
1717impl Drop for Session < ' _ > {
1818 fn drop ( & mut self ) {
19- #[ inline( always) ]
20- fn close ( session : & Session ) -> Result < ( ) > {
21- unsafe {
22- Rv :: from ( get_pkcs11 ! ( session. client( ) , C_CloseSession ) (
23- session. handle ( ) ,
24- ) )
25- . into_result ( Function :: CloseSession )
19+ match self . close_inner ( ) {
20+ Err ( Error :: Pkcs11 ( RvError :: SessionClosed , Function :: CloseSession ) ) => ( ) , // the session has already been closed: ignore.
21+ Ok ( ( ) ) => ( ) ,
22+ Err ( err) => {
23+ error ! ( "Failed to close session: {err}" ) ;
2624 }
2725 }
28-
29- if let Err ( err) = close ( self ) {
30- error ! ( "Failed to close session: {err}" ) ;
31- }
3226 }
3327}
3428
@@ -105,4 +99,13 @@ impl Session<'_> {
10599 SessionInfo :: try_from ( session_info)
106100 }
107101 }
102+
103+ // Helper function to be able to close a session only taking a reference.
104+ // This is used in the Drop trait function which only takes a reference as input.
105+ pub ( super ) fn close_inner ( & self ) -> Result < ( ) > {
106+ unsafe {
107+ Rv :: from ( get_pkcs11 ! ( self . client( ) , C_CloseSession ) ( self . handle ( ) ) )
108+ . into_result ( Function :: CloseSession )
109+ }
110+ }
108111}
0 commit comments