Commit 1: Add missing lock in getblocktemplate(...)
Reading the variable chainActive requires holding the mutex cs_main.
Prior to this commit the cs_main mutex was not held when accessing chainActive in:
while (chainActive.Tip()->GetBlockHash() == hashWatchedChain && IsRPCRunning())
Commit 2: Work around Clang thread safety analysis quirks
The conditional lock ...
LOCK2(cs_main, pwallet ? &pwallet->cs_wallet : nullptr)
... confuses Clang's thread safety analysis (it complains about not holding the mutex pwallet->cs_wallet even in the case when pwallet is non-NULL).
So does the access to pwallet->mapKeyMetadata via meta (it complains about not holding the mutex cs_wallet despite pwallet->cs_wallet – which is the correct mutex – is being held).
This commit introduces locking that Clang's thread safety analysis is able to understand correctly.