Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
Version 5.0.0
-
None
-
None
Description
The race condition can lead to a rare exception in a multithreaded environment.
The class uses two fields and initialises them lazy. The problem is that the process of initializing the field is not atomic, and simply declaring the fields volatile is not sufficient.
As an example: Thread A enters getFieldPath(1).
fieldPaths is is null so buildPaths happen.
Thread A build the first Path (index 0). Then Thread A pauses and Thread B the methode getFieldPath(1)
Thread B sees that _fieldPath is not null so it tries to access element 1, but that element was not build yet-> So Thread B gets null and bad things happen
This issue has been around since basicly forever, I just never reported it.
I will attach a fairly old source file wich I used to workaround this issue
private volatile XPath _selectorPath; private volatile XPath[] _fieldPaths;
public Object getFieldPath(int index) { XPath[] p = _fieldPaths; if (p == null) { try { buildPaths(); p = _fieldPaths; } catch (XPath.XPathCompileException e) { assert false : "Failed to compile xpath. Should be caught by compiler " + e; return null; } } return p[index]; }