-
Notifications
You must be signed in to change notification settings - Fork 87
Don't raise an error at close when it's expected #325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Don't raise an error at close when it's expected #325
Conversation
…lid as it means it's already closed Signed-off-by: Elise Chouleur <elise.chouleur@doctolib.com>
Signed-off-by: Elise Chouleur <elise.chouleur@doctolib.com>
Signed-off-by: Elise Chouleur <elise.chouleur@doctolib.com>
8887ff1 to
43551e6
Compare
|
Thanks for the PR!
I am wondering if it's possible for the session to be closed by other means if the API has been used only using safe blocks? If not, it could be good to still have this message to show that something unexpected happened? But maybe I am missing a flow where this could happen! |
Yep, it sounds as if there would be a double close in this scenario, which would be good to have in logs. I wonder about the use case of other means of closing the session... 👀 |
|
I implemented something similar than what you propose here as part of #326 but checking explicitly is the session is already closed |
|
Hi ! This happens to me when I remove the physical token before dropping the session. In this case, the session is automatically closed by the library in charge of this token. |
|
Would my PR suffice then? That would allow you to explicitly call |
|
I wanted to test the behaviour with your PR but the new implementation with the lifetime parameter on the session is a breaking change not compatible with my current implementation with the Pkcs11 context in a mutex and the session in a thread_local ref cell 😅 |
|
oh no 🙈 Could you maybe produce a minimal example of your usage of Session and Pkcs11 so that we could see how to adapt our code to make it work? Could also add examples to make sure we do not regress |
Demonstrates safe multi-threaded PKCS11 usage with: - Shared Pkcs11 context (Mutex) - Thread-local Sessions (RefCell) - Session reuse and automatic cleanup 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
|
I've added an example (helped by Claude ^^ so very verbose sorry) |
|
Thanks to the example, a way to resolve the issue I had with your branch is to use a OnceLock for PKCS11 context, I'll try that on my work and get back to you with the outcome :) |
wiktor-k
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this looks good 👌 thanks!
| } // Session auto-closes here via Drop | ||
|
|
||
| // Store context in global Mutex | ||
| *PKCS11_CTX.lock().unwrap() = Some(pkcs11); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this would work:
| *PKCS11_CTX.lock().unwrap() = Some(pkcs11); | |
| *PKCS11_CTX.lock()? = Some(pkcs11); |
| /// | ||
| /// This function makes multiple calls to with_session() to demonstrate | ||
| /// session reuse within the same thread. | ||
| fn generate_and_sign(thread_id: usize) -> testresult::TestResult<()> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
() should be the default:
| fn generate_and_sign(thread_id: usize) -> testresult::TestResult<()> { | |
| fn generate_and_sign(thread_id: usize) -> testresult::TestResult { |
| Ok(()) | ||
| } | ||
|
|
||
| fn main() -> testresult::TestResult { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could import the test result and just reuse it everywhere:
| fn main() -> testresult::TestResult { | |
| use testresult::TestResult; | |
| fn main() -> TestResult { |
|
I've tested my code with your PR @hug-dev, and I can't manage to close the session before the drop: Code: |
If session's close raises a
SessionHandleInvaliderror, that just means the session is already closed by other means.So don't raise an error but just a warn message in this case.