Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sendpfast is stopped improperly #4690

Open
badcodebuilder opened this issue Mar 14, 2025 · 0 comments · May be fixed by #4691
Open

sendpfast is stopped improperly #4690

badcodebuilder opened this issue Mar 14, 2025 · 0 comments · May be fixed by #4691

Comments

@badcodebuilder
Copy link

Brief description

Sub process of tcpreplay invoked by sendpfast cannot be stopped when pressing Ctrl+C

More detailed, cmd created in sendrecv.py is not terminated manually

scapy/scapy/sendrecv.py

Lines 584 to 600 in 20a3468

with ContextManagerSubprocess(conf.prog.tcpreplay):
try:
cmd = subprocess.Popen(argv, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
except KeyboardInterrupt:
log_interactive.info("Interrupted by user")
except Exception:
os.unlink(f)
raise
else:
stdout, stderr = cmd.communicate()
if stderr:
log_runtime.warning(stderr.decode())
if parse_results:
results = _parse_tcpreplay_result(stdout, stderr, argv)
elif conf.verb > 2:
log_runtime.info(stdout.decode())

Scapy version

2.6.1

Python version

3.13

Operating system

Linux 6.13.6-arch1-1

Additional environment information

tcpreplay: 4.5.1-1

How to reproduce

In interactive environment

>>> def pkt_gen(dst_ip: str, dport: int):
...:    while True:
...:        yield Ether()/IP(dst=dst_ip)/UDP(dport=dport)/"Hello, world"

>>> gen = pkt_gen("192.168.56.1", 10000)
>>> pkts = PacketList([next(gen) for _ in range(0x400)])
>>> sendpfast(pkts, loop=0, iface="enp0s8")

wait for a few seconds and press Ctrl+C

Actual result

^CERROR: tcpreplay: execution failed (type)
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/scapy/sendrecv.py", line 559, in sendpfast
    stdout, stderr = cmd.communicate()
                     ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/subprocess.py", line 1207, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/subprocess.py", line 2059, in _communicate
    ready = selector.select(timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/selectors.py", line 415, in select
    fd_event_list = self._selector.poll(timeout)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyboardInterrupt
<ipython-input-5-c6c71ec35432>:1: ResourceWarning: unclosed file <_io.BufferedReader name=12>
  sendpfast(pkts, loop=0, iface="enp0s8")
ResourceWarning: Enable tracemalloc to get the object allocation traceback
<ipython-input-5-c6c71ec35432>:1: ResourceWarning: unclosed file <_io.BufferedReader name=14>
  sendpfast(pkts, loop=0, iface="enp0s8")
ResourceWarning: Enable tracemalloc to get the object allocation traceback

Here are explanations of the two bugs above (before explaining, I will pin relative codes here):

scapy/scapy/sendrecv.py

Lines 584 to 600 in 20a3468

with ContextManagerSubprocess(conf.prog.tcpreplay):
try:
cmd = subprocess.Popen(argv, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
except KeyboardInterrupt:
log_interactive.info("Interrupted by user")
except Exception:
os.unlink(f)
raise
else:
stdout, stderr = cmd.communicate()
if stderr:
log_runtime.warning(stderr.decode())
if parse_results:
results = _parse_tcpreplay_result(stdout, stderr, argv)
elif conf.verb > 2:
log_runtime.info(stdout.decode())

  1. ERROR: tcpreplay ...: after waiting for a few seconds, cmd has been created correctly without any exception, it is waiting for stdout and stderr at line 594. Then the Ctrl+C is pressed, and KeyboardInterrupt happens, caught by ContextManagerSubprocess at line 584, it raises the exceptions
  2. ResourceWarning ...: the pipelines of stdout and stderr are not read and closed, because the exception is raised when communicating, then codes after line 594 will not be executed

Expected result

^CINFO: Interrupted by user
WARNING:  User interrupt...

Related resources

No response

badcodebuilder added a commit to badcodebuilder/scapy that referenced this issue Mar 14, 2025
* Fix bugs if stopping sendpfast via Ctrl+C
badcodebuilder added a commit to badcodebuilder/scapy that referenced this issue Mar 14, 2025
* Fix bugs if stopping sendpfast via Ctrl+C
@badcodebuilder badcodebuilder linked a pull request Mar 14, 2025 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant