Hi,


We are building a tool for detecting non atomic usages of concurrent
collections.



We analyzed Tomcat’s code using our tool and found the following behavior:



The following code is located at the beginning of function setAttribute at
class ApplicationContext. This code fragment intends to check whether a key
is already inside the “attributes” collection. If it does, then a “replaced”
bit is turned on while the corresponding value is kept in “oldValue”. Then a
new value is inserted for the same key and the function continue while using
“replaced” and “oldValue”.



oldValue = attributes.get(name);
if (oldValue != null)
    replaced = true;
attributes.put(name, value);



This code can behave non atomically when two different threads call
setAttribute function with the same attribute name. In this case, there
exists a thread interleaving where two threads call the get function before
both of them launch the put operation. In this case, both threads will
continue running the function with “replaced” bit turned on and the oldValue
of both thread will point to the same location. This means that the value of
the first thread that did the put operation will not be treated as a value
that was replaced.



Could you please let me know whether you consider this as a bug?



A possible way of modifying the code in order to execute this code
atomically is as follows:



val = attributes.put(name, value);
if (val != null)
    replaced = true;



Thanks,

Ohad

Reply via email to