Tribler / tribler

Privacy enhanced BitTorrent client with P2P content discovery
https://www.tribler.org
GNU General Public License v3.0
4.85k stars 451 forks source link

Enabling statistics causes peers to drop to 0 #8146

Closed qstokkink closed 2 months ago

qstokkink commented 2 months ago

Run 1:

screenshot 1

Run 2:

screenshot 2

My peer counts go up after starting Tribler and then quickly go down to near zero.

egbertbouman commented 2 months ago

Strange, counts on my machine seem fine (Win10 + Python 3.9).

qstokkink commented 2 months ago

Seemingly, setting "statistics" to false in the configuration solves this. This might be a coincidence, I'll run a few more tests.

EDIT: Alright, I'm convinced, the statistics setting really seems to be the culprit (but why?).

qstokkink commented 2 months ago

😮 this might actually be an IPv8 bug.

If I remove StatisticsEndpoint.__getattribute__ and implement all the Endpoint methods like a normal subclass, everything works UPDATE: it works more often (perhaps some sort of threading issue??).

qstokkink commented 2 months ago

I thought the "interface": "UDPIPv6" could maybe play a part in this. I removed that but this still happens.

qstokkink commented 2 months ago

Ok, this took a turn for the weird.

I added a print statement to UDPEndpoint.datagram_received. Then I ran Tribler with and without statistics enabled.

When enabling statistics. datagram_received gets called a few hundred times and then never gets called again. The result is that a few peers come in and then get dropped (correctly) as we never receive another message.

Without enabling statistics, datagram_received never gets called but I get a lot of peers.

qstokkink commented 2 months ago

Just tested on Ubuntu 22.04 and this does not happen there. Only on Windows apparently.

Regarding "datagram_received never gets called but I get a lot of peers": this is because I installed the rust extension. That should be fine.

qstokkink commented 2 months ago

This might be https://bugs.python.org/issue44743

The bug manifests with all combinations: SelectorEventLoop and ProactorEventLoop, python 3.9 and 3.10, on Windows 10.

qstokkink commented 2 months ago

I just brought my consistently failing laptop from the office to my home and now it consistently works without any issues. Same machine and the same code.

qstokkink commented 2 months ago

I added a callback to the UDPEndpoint class.

    def error_received(self, exc):
        print("!!! ERROR RECEIVED !!!", exc)

Over a run, this outputted three times:

!!! ERROR RECEIVED !!! [WinError 1214] The format of the specified network name is invalid
!!! ERROR RECEIVED !!! [WinError 1234] No service is operating at the destination network endpoint on the remote system
!!! ERROR RECEIVED !!! [WinError 1231] The network location cannot be reached. For information about network troubleshooting, see Windows Help

After the third message, peers started dropping.

qstokkink commented 2 months ago

Wait, hold on, I just discovered that my DatagramTransport is a _ProactorDatagramTransport. This may be a case of not setting the selector event loop correctly.

qstokkink commented 2 months ago

Confirmed:

asyncio.set_event_loop(asyncio.SelectorEventLoop())
asyncio.run(main())

Gives me a <ProactorEventLoop running=True closed=False debug=False>.

asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())

Gives me a <_WindowsSelectorEventLoop running=True closed=False debug=False>.