In add_p2p_connection() we connect to bitcoind from the Python client and check that it has received our version string.
This check looked up the last/newest entry from getpeerinfo RPC, assuming that it must be the connection we have just opened. But this will not be the case if a new inbound or outbound connection is made to/from bitcoind in the meantime.
Instead of the last entry in getpeerinfo, check all and find the one which corresponds to our connection using our outgoing address:port tuple which is unique.