-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[SHIRO-762] Mark SecurityUtils.securityManager
as volatile
#218
Conversation
As it can be modified and read by different threads
SecurityUtils.securityManager
as volatileSecurityUtils.securityManager
as volatile
@boris-petrov Thanks for the PR. |
@fpapon - thanks for the time. Exactly the reference is the problem. Marking this field as volatile means that whenever some thread sets the value (using And that's exactly what happens in our tests. We set initially a security manager, then we destroy it and set another one for the rest of the time. A few minutes later, a thread does
Because it still "sees" the previously set (and destroyed) security manager even though it's been a long time since a new one has been set. Marking this field as volatile is practically free and is making the code work correctly. |
@boris-petrov ok I see it now, it make sense ;) |
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.
LGTM
Thanks!
@bdemers can you have a second eyes on it? |
There could still be a race-condition on updating the variables. The volatile keyword will fix the read. If this is not enough for every use case (e.g. hold Threads while it is updated), we could go for |
@bmhm - yes, there could be a race-condition on updating the variable, but this is not Shiro's concern, it is the application's. I.e. it is a bug in the application. But |
I can agree on that. LGTM then. |
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.
👍
@boris-petrov If possible, I would like to know more about your use case and how/why you are setting/using the static instance. (i.e. is there something in Shiro that we could improve, or a framework we could support to help avoid this situation).
Thanks for your contribution!
@boris-petrov As we have an open release vote (1.5.3), I will merge your PR just after, don't worry if it take 2/3 days ;) |
@fpapon - great, no problem, thanks! @bdemers - well, I'm doing programmatic configuration (without any There is however a pain point that I see also others have hit before - using an |
Thanks for following up @boris-petrov! Re: needing to set the global SecurityManager, often it is used when creating subjects manually outside of another context like a servlet filter (or equivalent). No problem there, I was just wondering if there was a framework or something you were using. As for the thread local issue. I've created this JIRA: https://issues.apache.org/jira/browse/SHIRO-763 Thanks again! |
As it can be modified and read by different threads. This has been biting me for a very long time now.