scapy icon indicating copy to clipboard operation
scapy copied to clipboard

Fix AsyncSniffer early stop when timeout is set

Open T3pp31 opened this issue 6 months ago • 5 comments

Summary

  • Always register a control ObjectPipe in AsyncSniffer._run(), not only when the sniff socket is blocking.
  • On nonblocking backends (e.g. BPF on macOS), stop() now wakes select() immediately instead of leaving the sniff thread blocked until the configured timeout expires.

Fixes #4890

Problem

With AsyncSniffer(..., timeout=N) and a nonblocking listen socket (default on macOS with BPF), calling stop(join=False) set continue_sniff = False but the thread remained inside select(..., remain) for up to N seconds. running stayed True and results stayed None until that wait finished.

Solution

Use the same control-pipe mechanism already employed for blocking sockets: stop_cb() sends on the pipe so select() returns and the loop exits promptly.

Test plan

  • [x] Added regression test AsyncSniffer early stop with timeout on nonblocking socket (#4890) in test/regression.uts (no root required; uses a minimal SuperSocket that blocks in select for the full remain).
  • [x] python3 -m scapy.tools.UTscapy -t test/regression.uts -F "4890" — new test passes.

Made with Cursor

T3pp31 avatar May 31 '26 07:05 T3pp31

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 79.50%. Comparing base (ca08e6f) to head (cbb9124). :warning: Report is 25 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5007      +/-   ##
==========================================
- Coverage   80.03%   79.50%   -0.53%     
==========================================
  Files         384      384              
  Lines       95622    95622              
==========================================
- Hits        76527    76022     -505     
- Misses      19095    19600     +505     
Files with missing lines Coverage Δ
scapy/sendrecv.py 86.23% <100.00%> (-0.28%) :arrow_down:

... and 22 files with indirect coverage changes

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar May 31 '26 08:05 codecov[bot]

CI status (follow-up)

The latest CI run still reports some failing checks. Below is why those failures are not blockers for this PR and what was already addressed here.

What this PR fixes (verified in CI)

  • AsyncSniffer early stop with timeout on nonblocking socket (#4890) — passes (e.g. macOS regression.uts).
  • Answering machine tests on PyPy — previously failed with TypeError: test_am() missing 1 required positional argument: 'mock_sniff' because @mock.patch does not survive UTscapy’s cross-block calls on PyPy. Fixed by using mock.patch as a context manager inside test_am() / test_WiFi_am(). Latest run: 15 passed / 0 failed in test/answering_machines.uts on ubuntu-latest pypy3.11 root -K scanner.
  • All Ubuntu CPython scanner jobs (3.7–3.14) — pass.
  • -k scanner on macOS and Windows — pass.

The change in scapy/sendrecv.py only adds the control ObjectPipe when the socket is blocking or when a timeout is set, so stop() can wake select() without waiting the full timeout (#4890). Nonblocking sockets without a timeout keep the previous behavior.


Remaining red checks (not introduced by this PR)

1. ubuntu-latest pypy3.11 root -K scanner — test/fields.uts (7 failures)

Failures are in field tests (e.g. heterogeneous PacketListField with memory, MultiFlagsField, EnumField), not in sendrecv/AsyncSniffer or answering machines. These look like PyPy-specific / upstream test-suite issues, not regressions from this diff.

2. ubuntu-latest pypy3.11 root -k scanner — test/contrib/automotive/obd/scanner.uts (4 failures)

Same 4 passed / 4 failed pattern as on current master (e.g. run for ca08e6f8). Pre-existing on PyPy; not caused by the AsyncSniffer change.

3. macos-14 3.14 both -K scanner — Sending and receiving an TCP syn with flooding methods

Network test against external hosts (www.google.com, etc.) with retry_test. Flaky by nature; it also failed on an earlier run of this PR while the new #4890 test passed in the same job.

4. windows-latest 3.14 root -K scanner — CoAP socket tests (9 failures)

CoAP socket integration tests fail on upstream master as well (same job name / similar failures). Related to CoAP socket work on master (e.g. #5002), not this PR.

5. codecov/project

Coverage gate failure; may need a separate look. The functional regression for #4890 is covered by the new regression.uts test.


Summary

This PR targets #4890 only. Core behavior is tested and green on the main platforms; remaining failures match known flaky or pre-existing CI issues on master, especially PyPy scanner jobs and Windows CoAP.

Happy to split the PyPy mock.patch test fix into a separate PR if you prefer a single-purpose commit series here.

T3pp31 avatar May 31 '26 11:05 T3pp31

@gpotter2 what do you think about this approach?

We could streamline the nonblocking socket code entirely if we go this path. Not sure if this would have a performance decrease on blocking sockets, though.

polybassa avatar May 31 '26 11:05 polybassa

Hi @polybassa is there a reason your comment was marked as outdated?

gpotter2 avatar Jun 02 '26 08:06 gpotter2

I thought the code changed in the meantime. I didn't had a look on the full module, only the changes. So I didn't wanted to bother you with my idea, before checking the whole module against it

polybassa avatar Jun 02 '26 12:06 polybassa

This PR is too complicated: we discussed this and will recommand using join() instead of timeout on AsyncSniffer.

gpotter2 avatar Jul 22 '26 08:07 gpotter2