Backport of core [[https://github.com/bitcoin/bitcoin/pull/15771/files | PR15771]] (and [[https://github.com/bitcoin/bitcoin/pull/14788/files | PR14788]]).
This is expected to fix the TSAN errors like
https://build.bitcoinabc.org/viewLog.html?buildTypeId=BitcoinABC_ResourceIntensiveBuilds_BitcoinAbcMasterTsan&buildId=28539.