-
Notifications
You must be signed in to change notification settings - Fork 18
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Deadlock when using both read and write in the same handler #13
Comments
Why is Also from my quick glance at Maybe removing this division and the |
We've been iterating on this previously here. I personally avoid this issue by using a wrapper over |
I'm certainly open to improving and changing this. The reason we have to use interior mutability is because the underlying |
Seems like not In reality a simpler change to just fix this problem would possibly be to use some kind of reentrant/recursive lock. There's no recursive |
Yes, I knew that I'm certainly not against a new API. A fairly simple solution is what I mentioned above (the wrapper over |
@Ptrskay3 yes, seems reasonable in my opinion. |
Another thing to point out here is that it's unclear how often users are going to run into this in practice. It's a bigger issue with Additionally, it may be worth investigating with |
@maxcountryman correct me if I'm wrong, but I think the If there's some way to forbid this situation in the code - a compilation error or even a runtime panic in the worst case - this issue is solved without any major changes to the API. We'd just need to change the docs slightly. |
I think it's a fairly common thing to both read from and write to the session in the same handler. A straight forward examples are OAuth or Two-factor authentication. In my opinion the root cause of this issue is in this repository, and this is where it should be addressed - that's why I opened the issue here.
|
I would propose replacing the The handle is added as Guards should only be acquired as long as they are needed, and dropped afterwards. Or did I miss anything in my CSRF synchronizer token middleware? The write guard is dropped before calling the inner service. |
This is possible, but I'm not sure how obvious the API is. The tradeoff is the caller needs to directly manage the guard. This might be fine (it does make it explicit which seems to be part of the problem of abstraction here). To your point, this is already possible with the current implementation: applications can choose to use the |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Having read an issue in axum-login I realized that it's really easy to get a deadlock with the current API of this crate. Since
ReadableSession
andWritableSession
both using the same underlying resource behind anRwLock
.A simple axum handler to reproduce:
and this can be solved if you release the lock guard
This issue becomes especially awkward and more subtle when this becomes an implicit bound, just like here.
AuthContext
needs a write access, but also there's a session read in the same handler. The developer must be familiar with the implementation details of these structs to know to release the read guard.I don't really know how to solve this with the current API, but I'm experimenting..
The text was updated successfully, but these errors were encountered: