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
8288140: Avoid redundant Hashtable.get call in Signal.handle #9100
Closed
turbanoff
wants to merge
3
commits into
openjdk:master
from
turbanoff:avoid_redundant_Hashtable.get_in_Signal
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If you made this change instead:
Wouldn't you be able to remove the entire
synchronized
block?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.
Hello David, I suspect you mean
handlers.put(sig, handler)
and nothandlers.replace(...)
? And yes, I think what you suggest will help remove the synchronized block here.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.
Oops, yes you are correct!
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.
Hi,
I think the synchronized block was redundant already in original code. Since the entire handle method is
static synchronized
and it is the only method that modifies thehandlers
andsignals
maps.But even with so much redundant synchronization, the Signal class is not without races. There are multiple possible races in
dispatch(int number)
method which is called from native code to dispatch a signal:signals
andhandlers
maps respectively, assuming their inter-referenced state is consistent. Buthandle
method may be concurrently modifying them, sodispatch
may see updated state ofsignals
map while seeing old state ofhandlers
map or vice versa.signals
andhandlers
there is a 3rd map in native code, updated withhandle0
native method. Native code dispatches signals according to that native map, forwarding them to either native handlers or todispatch
Java method. Buthandle
method may be modifying that native map concurrently, so dispatch may be called as a consequence of updated native map while seeing old states ofsignals
andhandlers
maps.I'm sure I might have missed some additional races.
How to fix this? Is it even necessary? I think that this internal API is not used frequently so this was hardly an issue. But anyway, it would be a challenge. While the two java maps:
handlers
andsignals
could be replaced with a single map, there is the 3rd map in native code. We would not want to makedispatch
method synchronized, so with careful ordering of modifications it is perhaps possible to account for races and make them harmless...WDYT?
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.
Well, studying this further, I think some races are already accounted for and are harmless except one. The
handle
method 1st attempts to register handler in native code via call tohandle0
and then updates thesignals
map.. As soon as native code registers the handler,dispatch
may be called and the lookup intosignals
map may return null which would cause NPE in the following lookup intohandlers
map. So there are two possibilities to fix this:singnals
lookup, orsignals
map and a call tohandle0
native method. You could even update thesignals
map with.putIfAbsent()
to avoid multiple reassignment with otherwise equal instances. This may unnecessarily establish a mapping for a Signal the can later not be registered, so you can remove it from thesignals
map in that case to clean-up.The possible case when the lookup into
handlers
map returns null Handler is already taken into account, but there is a possible case where a NativeHandler is replaced with a java Handler implementation anddispatch
is therefore already called, buthandlers
map lookup still returns a NativeHandler. In that case the NativeHandler::handle method will throw exception. To avoid that, NativeHandler::handle could be an empty method instead.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 tried to reproduce this possible problem via jcstress, but never caught this race.
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 believe fixing this race is out of scope of current PR. Feel free to open a separate issue :)