-
Notifications
You must be signed in to change notification settings - Fork 89
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
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 |
|
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!
|
I've tested my code with your PR @hug-dev, and I can't manage to close the session before the drop: Code: |
|
We reverted a commit on main recently that might affect you! How is everything working now on your side? |
hug-dev
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 the recent changes should help a bit!
…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>
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> Signed-off-by: Elise Chouleur <elise.chouleur@doctolib.com>
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Elise Chouleur <elise.chouleur@doctolib.com>
Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com> 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>
…removal between 2 sessions And add a test file to check close logs behaviour. Signed-off-by: Elise Chouleur <elise.chouleur@doctolib.com>
Signed-off-by: Elise Chouleur <elise.chouleur@doctolib.com>
a58c407 to
e44a14c
Compare
Signed-off-by: Elise Chouleur <74413127+EliseChouleur@users.noreply.github.com>
Signed-off-by: Elise Chouleur <elise.chouleur@doctolib.com>
I think cryptoki should include the automatically Dropping wrapper alongside the non Dropping variant and let the user choose. The Dropping variant is not that straightforward since it needs to call finalize only if finalize has not been previously called. This would make easy things easy (I'd usually use the Dropping variant) and hard things possible (the non Dropping for manual lifecycle management). Does that make things clearer or more muddled? 😅 |
|
That's fine for me! Then I think we all agree that we can implement the 2 option you proposed @EliseChouleur :) |
|
Ok, so if I understand this well, I'll create a a Cargo feature (e.g. |
Signed-off-by: Elise Chouleur <elise.chouleur@doctolib.com>
|
Maybe instead of a Cargo feature we can modify the API to be able to choose if we want the To not add breaking changes we could add new constructors,
Yes exactly! |
Signed-off-by: Elise Chouleur <elise.chouleur@doctolib.com>
…elf' function 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>
Signed-off-by: Elise Chouleur <74413127+EliseChouleur@users.noreply.github.com>
Signed-off-by: Elise Chouleur <elise.chouleur@doctolib.com>
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.
Okay, this is definitely a high-quality PR with quite some goodies in here - thanks for your time 🙇
There's a little bit of stuff to unpack there so I left a couple of nits, maybe @hug-dev will see and tell what they think about them.
I like that this thing is tested, it's quite an intricate behavior and your added entire mock and logger lib to test everything thoroughly 👏
Funnily enough I've been playing with writing a Rust-PKCS#11 token too (https://gitlab.archlinux.org/wiktor/pkcs11-proxy/-/blob/main/src/lib.rs?ref_type=heads#L182, heavily incomplete) and I wish I had something simpler that we could use to replace the C code (which isn't bad, it's very readable).
One small concern I have with the test logger is that it tests if the log was captured which may be brittle if the message gets edited. Sadly I don't see a better way since the session is managed by the PKCS#11 object 🤔 maybe more flags in the mock pkcs#11?
Thanks for your contribution, I hope we find a way to merge it soon 🙏
…xample Signed-off-by: Elise Chouleur <elise.chouleur@doctolib.com>
Signed-off-by: Elise Chouleur <elise.chouleur@doctolib.com>
hug-dev
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.
Thanks a lot for the changes! Good to have your example in there as well.
| // SPDX-License-Identifier: Apache-2.0 | ||
| //! Tests for Drop error handling in Session. | ||
| //! | ||
| //! These tests use a mock PKCS#11 library that can simulate token removal, |
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.
Really appreciate the effort you put into adding a whole mock PKCS11 library to be able to test this but I am wondering if it might not be a bit "too much" for the scope of this change. I am thinking about the tradeoff between testing this and adding a lot of code that we will have to maintain in the future 😅
Would it be possible somehow to get drop to fail with something else than "already close" without the need of the entire mock library?
If not maybe in this case we can say that it's been "tested on your machine" 😅
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.
In addition to be able to test this case it was also to suggest a mock version of tests to be able to test edge cases later on, but I'm not sure there's really some other edge cases to test ^^
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 see, then I think we could maybe do a simple test triggering a fault with a new_from_raw method as described below and maybe revisit this mock-PKCS11 idea once we have another use case?
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.
Yup, I've done this in 38aaf5a but couldn't find a way to test without checking the logs which isn't a nice test assertion, so I think you're right, it's better to remove.
Signed-off-by: Elise Chouleur <elise.chouleur@doctolib.com>
… without the mock library 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>
…sefull compared to the complexity Signed-off-by: Elise Chouleur <elise.chouleur@doctolib.com>
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 it looks small and good 👌
| // Helper function to be able to close a session only taking a reference. | ||
| // This is used in the Drop trait function which only takes a reference as input. | ||
| pub(super) fn close_inner(&self) -> Result<()> { | ||
| self.closed.set(true); |
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.
@hug-dev did you mean that the set should be positioned after the call to C_CloseSession?
Or maybe this one should be done in drop() when the close_inner() function returns Ok(())?
🤔
Co-authored-by: Wiktor Kwapisiewicz <wiktor@metacode.biz> Signed-off-by: Elise Chouleur <74413127+EliseChouleur@users.noreply.github.com>
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.