linkedin / rest.li

Rest.li is a REST+JSON framework for building robust, scalable service architectures using dynamic discovery and simple asynchronous APIs.
rest.li
Other
2.51k stars 546 forks source link

Improve synchronization on the R2 `RequestContext` local attributes. #900

Closed jpstewart closed 1 year ago

jpstewart commented 1 year ago

It is possible to cause a ConcurrentModificationException in the RequestContext because we synchronize it by using Collections.synchronizedMap(), which only synchronizes individual reads/writes. When making a RequestContext copy we must iterate over the collection, allowing other threads to interrupt it by modifying the map during the operation.

This fix synchronizes the whole copy operation as well. The default mutex used for synchronizing in a SynchronizedMap is this, so synchronizing on the map itself guards against modification.