First change is from @TheBlueMatt.
https://github.com/bitcoin/bitcoin/pull/8708/commits/462965635e8bf9c2754c27946f8b05560b8ddd84 added assertions to ensure that we never send out any messages before version negotiation has completed, but a few issues remain.
In cases where the version message failed to deserialize fully, we can end up with only some of the vars set, leading to sending messages that shouldn’t be sent.
Additionally, the ForEach* functions allow the caller to send messages to all nodes, including those that aren’t yet fully connected. To prevent that, filter out nodes that definitely shouldn’t be sending/processing messages.
As a follow-up, I’d like to remove the assert, as I think it’s done its job, but I’ll save that discussion for another PR because I think @TheBlueMatt mentioned that he’d prefer to keep it.