Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
This is a partial backport of core#29553
https://github.com/bitcoin/bitcoin/pull/29553/commits/94b0adcc371540732453d70309c4083d4bd9cd6b
Bypasses a comment about the racy condition added in https://github.com/bitcoin/bitcoin/pull/29553/commits/94b0adcc371540732453d70309c4083d4bd9cd6b
Depends on D18047