Fix AsyncSniffer early stop when timeout is set
Summary
- Always register a control
ObjectPipeinAsyncSniffer._run(), not only when the sniff socket is blocking. - On nonblocking backends (e.g. BPF on macOS),
stop()now wakesselect()immediately instead of leaving the sniff thread blocked until the configuredtimeoutexpires.
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)intest/regression.uts(no root required; uses a minimalSuperSocketthat blocks inselectfor the fullremain). - [x]
python3 -m scapy.tools.UTscapy -t test/regression.uts -F "4890"— new test passes.
Made with Cursor
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: |
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
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. macOSregression.uts).- Answering machine tests on PyPy — previously failed with
TypeError: test_am() missing 1 required positional argument: 'mock_sniff'because@mock.patchdoes not survive UTscapy’s cross-block calls on PyPy. Fixed by usingmock.patchas a context manager insidetest_am()/test_WiFi_am(). Latest run: 15 passed / 0 failed intest/answering_machines.utsonubuntu-latest pypy3.11 root -K scanner. - All Ubuntu CPython scanner jobs (3.7–3.14) — pass.
-k scanneron 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.
@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.
Hi @polybassa is there a reason your comment was marked as outdated?
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
This PR is too complicated: we discussed this and will recommand using join() instead of timeout on AsyncSniffer.