Repository: bitcoin
Issue: net: redundant connections with a single peer
Labels: Bug

To reproduce: 
https://github.com/bitcoin/bitcoin/issues/22559#issuecomment-887305332 (general I2P setup, in+out)
https://github.com/bitcoin/bitcoin/issues/22559#issuecomment-903851322 (multiple in, multiple out)

I've observed my node keeping multiple connections to the same peer in various ways, due to long I2P connection negotiation times, as it's not uncommon to take 10 to 20 seconds to connect to a peer. If additional connections are made during negotiation with the same peer, all connections remain open.

The bug should occur on any network. This is an I2P only node, which I'm sure increases the chances of this bug happening because my peers.dat is tiny. Note that I do not have any `addnode=` in my config or anything like that.

**1 inbound + 1 outbound:**
I connected to a peer. 18 seconds later, it connected to me. These connections persisted for over 12 hours. Both peers are running on a very recent `22.99.0` master branch.

**2 inbound or 2 inbound:** (2 can be any number with the right timing)
A node connected to me twice around the same time. These connections persisted for over 12 hours. This peer was running the `22.0.0` release.

`getpeerinfo` data for **1 inbound + 1 outbound** peers:
```
  {
    "id": 680,
    "addr": "jz3s4eurm5vzjresf4mwo7oni4bk36daolwxh4iqtewakylgkxmq.b32.i2p:0",
    "addrbind": "bitcornrd36coazsbzsz4pdebyzvaplmsalq4kpoljmn6cg6x5zq.b32.i2p:0",
    "network": "i2p",
    "services": "0000000000000409",
    "servicesnames": [
      "NETWORK",
      "WITNESS",
      "NETWORK_LIMITED"
    ],
    "relaytxes": true,
    "lastsend": 1627290126,
    "lastrecv": 1627290124,
    "last_transaction": 1627290120,
    "last_block": 0,
    "bytessent": 3930317,
    "bytesrecv": 13011805,
    "conntime": 1627229534,
    "timeoffset": -2,
    "pingtime": 3.264304,
    "minping": 0.719468,
    "version": 70016,
    "subver": "/Satoshi:22.99.0(@dunxen)/",
    "inbound": false,
    "bip152_hb_to": false,
    "bip152_hb_from": false,
    "startingheight": 692610,
    "synced_headers": 692727,
    "synced_blocks": 692727,
    "inflight": [
    ],
    "addr_processed": 7049,
    "addr_rate_limited": 1244,
    "permissions": [
    ],
    "minfeefilter": 0.00001000,
    "bytessent_per_msg": {
      "addrv2": 121698,
      "feefilter": 32,
      "getaddr": 24,
      "getdata": 442191,
      "getheaders": 1053,
      "headers": 8480,
      "inv": 3307633,
      "notfound": 316,
      "ping": 16128,
      "pong": 16128,
      "sendaddrv2": 24,
      "sendcmpct": 66,
      "sendheaders": 24,
      "tx": 16345,
      "verack": 24,
      "version": 127,
      "wtxidrelay": 24
    },
    "bytesrecv_per_msg": {
      "addrv2": 167167,
      "feefilter": 32,
      "getdata": 2090,
      "getheaders": 1053,
      "headers": 12190,
      "inv": 3572569,
      "notfound": 122,
      "ping": 16128,
      "pong": 16128,
      "sendaddrv2": 24,
      "sendcmpct": 66,
      "sendheaders": 24,
      "tx": 9208916,
      "verack": 24,
      "version": 136,
      "wtxidrelay": 24
    },
    "connection_type": "outbound-full-relay"
  },
  {
    "id": 682,
    "addr": "jz3s4eurm5vzjresf4mwo7oni4bk36daolwxh4iqtewakylgkxmq.b32.i2p:0",
    "addrbind": "bitcornrd36coazsbzsz4pdebyzvaplmsalq4kpoljmn6cg6x5zq.b32.i2p:0",
    "network": "i2p",
    "services": "0000000000000409",
    "servicesnames": [
      "NETWORK",
      "WITNESS",
      "NETWORK_LIMITED"
    ],
    "relaytxes": true,
    "lastsend": 1627290124,
    "lastrecv": 1627290123,
    "last_transaction": 1627290013,
    "last_block": 0,
    "bytessent": 1585430,
    "bytesrecv": 7794448,
    "conntime": 1627229552,
    "timeoffset": -13,
    "pingtime": 1.396263,
    "minping": 0.759804,
    "version": 70016,
    "subver": "/Satoshi:22.99.0(@dunxen)/",
    "inbound": true,
    "bip152_hb_to": false,
    "bip152_hb_from": false,
    "startingheight": 692610,
    "synced_headers": 692727,
    "synced_blocks": 692727,
    "inflight": [
    ],
    "addr_processed": 6055,
    "addr_rate_limited": 1188,
    "permissions": [
    ],
    "minfeefilter": 0.00001000,
    "bytessent_per_msg": {
      "addrv2": 124133,
      "feefilter": 32,
      "getdata": 90391,
      "getheaders": 1053,
      "headers": 8455,
      "inv": 1201793,
      "ping": 16160,
      "pong": 16160,
      "sendaddrv2": 24,
      "sendcmpct": 66,
      "sendheaders": 24,
      "tx": 126964,
      "verack": 24,
      "version": 127,
      "wtxidrelay": 24
    },
    "bytesrecv_per_msg": {
      "addrv2": 145913,
      "cmpctblock": 2422,
      "feefilter": 32,
      "getaddr": 24,
      "getdata": 14266,
      "getheaders": 1053,
      "headers": 12084,
      "inv": 5364780,
      "notfound": 61,
      "ping": 16160,
      "pong": 16160,
      "sendaddrv2": 24,
      "sendcmpct": 66,
      "sendheaders": 24,
      "tx": 2221195,
      "verack": 24,
      "version": 136,
      "wtxidrelay": 24
    },
    "connection_type": "inbound"
  }
```

`getpeerinfo` data for **2 inbound** peers:
```
 {
    "id": 1326,
    "addr": "acgncqkgqekcxaagpes6ubfiuhg54ijklwcupbnitte5svh3a3bq.b32.i2p:0",
    "addrbind": "bitcornrd36coazsbzsz4pdebyzvaplmsalq4kpoljmn6cg6x5zq.b32.i2p:0",
    "network": "i2p",
    "services": "0000000000000409",
    "servicesnames": [
      "NETWORK",
      "WITNESS",
      "NETWORK_LIMITED"
    ],
    "relaytxes": true,
    "lastsend": 1629449575,
    "lastrecv": 1629449571,
    "last_transaction": 1629449402,
    "last_block": 0,
    "bytessent": 4675731,
    "bytesrecv": 5393457,
    "conntime": 1629383715,
    "timeoffset": -3,
    "pingtime": 2.009341,
    "minping": 0.5858950000000001,
    "version": 70016,
    "subver": "/Satoshi:22.0.0(MN@ca)/",
    "inbound": true,
    "bip152_hb_to": false,
    "bip152_hb_from": false,
    "startingheight": 696531,
    "synced_headers": 696664,
    "synced_blocks": 696664,
    "inflight": [
    ],
    "addr_relay_enabled": true,
    "addr_processed": 6062,
    "addr_rate_limited": 0,
    "permissions": [
    ],
    "minfeefilter": 0.00001000,
    "bytessent_per_msg": {
      "addrv2": 149637,
      "cmpctblock": 5710,
      "feefilter": 32,
      "getdata": 2941,
      "getheaders": 1053,
      "headers": 9752,
      "inv": 3847162,
      "notfound": 3173,
      "ping": 17568,
      "pong": 17312,
      "sendaddrv2": 24,
      "sendcmpct": 66,
      "sendheaders": 24,
      "tx": 621102,
      "verack": 24,
      "version": 127,
      "wtxidrelay": 24
    },
    "bytesrecv_per_msg": {
      "addrv2": 130686,
      "feefilter": 32,
      "getaddr": 24,
      "getdata": 51317,
      "getheaders": 1053,
      "headers": 10519,
      "inv": 5142494,
      "ping": 17312,
      "pong": 17568,
      "sendaddrv2": 24,
      "sendcmpct": 66,
      "sendheaders": 24,
      "tx": 22157,
      "verack": 24,
      "version": 133,
      "wtxidrelay": 24
    },
    "connection_type": "inbound"
  },
  {
    "id": 1327,
    "addr": "acgncqkgqekcxaagpes6ubfiuhg54ijklwcupbnitte5svh3a3bq.b32.i2p:0",
    "addrbind": "bitcornrd36coazsbzsz4pdebyzvaplmsalq4kpoljmn6cg6x5zq.b32.i2p:0",
    "network": "i2p",
    "services": "0000000000000409",
    "servicesnames": [
      "NETWORK",
      "WITNESS",
      "NETWORK_LIMITED"
    ],
    "relaytxes": true,
    "lastsend": 1629449578,
    "lastrecv": 1629449580,
    "last_transaction": 1629448430,
    "last_block": 0,
    "bytessent": 4869327,
    "bytesrecv": 5433036,
    "conntime": 1629383742,
    "timeoffset": -21,
    "pingtime": 1.979714,
    "minping": 0.69269,
    "version": 70016,
    "subver": "/Satoshi:22.0.0(MN@ca)/",
    "inbound": true,
    "bip152_hb_to": false,
    "bip152_hb_from": false,
    "startingheight": 696531,
    "synced_headers": 696664,
    "synced_blocks": 696664,
    "inflight": [
    ],
    "addr_relay_enabled": true,
    "addr_processed": 6188,
    "addr_rate_limited": 0,
    "permissions": [
    ],
    "minfeefilter": 0.00001000,
    "bytessent_per_msg": {
      "addrv2": 148858,
      "feefilter": 32,
      "getdata": 1932,
      "getheaders": 1053,
      "headers": 9858,
      "inv": 3872869,
      "notfound": 2259,
      "ping": 17568,
      "pong": 17312,
      "sendaddrv2": 24,
      "sendcmpct": 66,
      "sendheaders": 24,
      "tx": 797297,
      "verack": 24,
      "version": 127,
      "wtxidrelay": 24
    },
    "bytesrecv_per_msg": {
      "addrv2": 133478,
      "feefilter": 32,
      "getaddr": 24,
      "getdata": 68654,
      "getheaders": 1053,
      "headers": 10625,
      "inv": 5171958,
      "ping": 17312,
      "pong": 17568,
      "sendaddrv2": 24,
      "sendcmpct": 66,
      "sendheaders": 24,
      "tx": 12037,
      "verack": 24,
      "version": 133,
      "wtxidrelay": 24
    },
    "connection_type": "inbound"
  }
```