This is a backport of PR #9053 -- we passed on backporting it right after it was merged, but it's been in master for over a month now with no issue.
This resolves some frequent annoying misbehavior on testnet (in theory the problems it solves are also possible on mainnet but aren't likely there without miner funny business).