scapy icon indicating copy to clipboard operation
scapy copied to clipboard

TestSockets are not closed in many tests

Open gpotter2 opened this issue 3 years ago • 9 comments

I've been investigating issues failures on PyPy and it appears it fails because we open too many pipes during the tests:

image

Why PyPy and not CPython you may ask? Because PyPy has a different gc (see this) that doesn't call __del__ as early as CPython (as soon as the socket is unreferenced), so sockets pile up.

It's easy to find tons of unclosed sockets in the tests (grep -R TestSocket then a little bit of looking around) https://github.com/secdev/scapy/blob/9420c2229bf5330c2cc580f114f63f920a68db10/test/contrib/isotpscan.uts#L72 https://github.com/secdev/scapy/blob/9420c2229bf5330c2cc580f114f63f920a68db10/test/contrib/isotpscan.uts#L163-L164

So we should probably perform some cleanups on those. Feel free to contribute !

gpotter2 avatar Jul 04 '22 20:07 gpotter2

Thanks a lot for this investigation.. I'll take care of it, since I caused these issues.

What do you recommend? Using with Testsocket() as sock: all over the place?

polybassa avatar Jul 05 '22 06:07 polybassa

You could do that or just call .close(), ideally in a try finally but as those are tests it's fine if you don't. (we would have a failure anyways)

It's alright those happen a lot. At some point automatons broke down entirely because of a similar issue.. they're pretty hard to troubleshoot

gpotter2 avatar Jul 05 '22 07:07 gpotter2

found a easy solution.. hope it holds.

polybassa avatar Jul 05 '22 08:07 polybassa

Okay that was clearly not enough. Tests on PyPy2 are still crashing after thousands of TestSocket are hold in memory. There is a bug somewhere in an automotive test that keeps creating some and doesn't close them

For instance: https://github.com/secdev/scapy/runs/7296916202?check_suite_focus=true

gpotter2 avatar Jul 12 '22 07:07 gpotter2

Thanks.. I'll have a look!

polybassa avatar Jul 12 '22 08:07 polybassa

This log looked like a race-condition was triggered... I've added some debug code to fail fast, once this error happens again.

polybassa avatar Jul 12 '22 08:07 polybassa

I do not believe that the issue here is that it fails to close the sockets, the real issue is that there are thousand and thousands of sockets to close in one go in the first place. You can't rely on __del__ on PyPy to clear the sockets for you, because of limitations, so you're supposed to have closed them manually beforehand.

gpotter2 avatar Jul 12 '22 08:07 gpotter2

In the current version on master, I'm already calling close manually on all opened sockets after each testcase: https://github.com/secdev/scapy/blob/2c92b0350ab5df8ea0adc164fb4441c979bec568/test/contrib/automotive/scanner/uds_scanner.uts#L82

This should ensure, that all sockets get closed, but maybe I'm missing something.

polybassa avatar Jul 12 '22 08:07 polybassa

The current CI results, clearly show, that OSX and Windows CI works as expected. Only on Linux based systems, somehow the objects doesn't get destroyed.

polybassa avatar Jul 12 '22 09:07 polybassa

Looks much more stable ! Thanks a lot polybassa

gpotter2 avatar Aug 18 '22 02:08 gpotter2

Finally :-D

polybassa avatar Aug 18 '22 06:08 polybassa

I'm still seeing a little instability in the GMLAN Scanner tests. To let you know that I have an eye on that ones.

polybassa avatar Aug 18 '22 06:08 polybassa