Add synchronisation around map `getOrElse`.

Needed for proper visibility, in case this code
is called on different threads.
(cherry picked from commit 6181d7f57da6c28cc6f6ef0df873a2f7a960ba73)