Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Fixed
-
None
-
None
-
None
Description
There are several places where we perform double-checked locking. Even though the practice is discouraged, I believe it is technically correct when the check is performed on the presence of a ConcurrentHashMap key, which is how we are using it.
However, in two places, AgentProviderService#getCurrentExports and AgentProviderService#getAllocatedPorts, containsKey is used instead of get to perform the check. I am seeing some indication that containsKey is not sufficient, and that get must be used for double-checked locking to be correct. There is a comment in the ConcurrentHashMap#containsKey method that says "same as get() except no need for volatile value read" – and I think that volatile value read is what we need for correctness.
Also, in the ConcurrentHashMap api doc, it specifically mentions get and does not mention containsKey: "Any non-null result returned from get(key) and related access methods bears a happens-before relation with the associated insertion or update" and "an update operation for a given key bears a happens-before relation with any (non-null) retrieval for that key reporting the updated value."