Skip to content

Commit

Permalink
Use much better timeout for threading
Browse files Browse the repository at this point in the history
  • Loading branch information
gpotter2 committed Sep 22, 2024
1 parent 56e82b2 commit f4e06ce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
22 changes: 9 additions & 13 deletions scapy/sendrecv.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class debug:
Automatically enabled when a generator is passed as the packet
:param _flood:
:param threaded: if True, packets are sent in a thread and received in another.
defaults to False.
Defaults to True.
:param session: a flow decoder used to handle stream of packets
:param chainEX: if True, exceptions during send will be forwarded
:param stop_filter: Python function applied to each packet to determine if
Expand Down Expand Up @@ -158,7 +158,7 @@ def __init__(self,
self.noans = 0
self._flood = _flood
self.threaded = threaded
self.breakout = False
self.breakout = Event()
# Instantiate packet holders
if prebuild and not self._flood:
self.tobesent = list(pkt) # type: _PacketIterable
Expand Down Expand Up @@ -190,7 +190,7 @@ def __init__(self,
except KeyboardInterrupt as ex:
interrupted = ex

self.breakout = True
self.breakout.set()

# Ended. Let's close gracefully
if self._flood:
Expand Down Expand Up @@ -273,7 +273,7 @@ def _sndrcv_snd(self):
# Send packet
self.pks.send(p)
time.sleep(self.inter)
if self.breakout:
if self.breakout.is_set():
break
i += 1
if self.verbose:
Expand All @@ -296,16 +296,12 @@ def _sndrcv_snd(self):
elif not self._send_done:
self.notans = i
self._send_done = True
# In threaded mode, timeout.
if self.threaded and self.timeout is not None and not self.breakout:
t = time.monotonic() + self.timeout
while time.monotonic() < t:
if self.breakout:
break
time.sleep(0.1)
self._stop_sniffer_if_done()
# In threaded mode, timeout
if self.threaded and self.timeout is not None and not self.breakout.is_set():
self.breakout.wait(timeout=self.timeout)
if self.sniffer and self.sniffer.running:
self.sniffer.stop()
self._stop_sniffer_if_done()

def _process_packet(self, r):
# type: (Packet) -> None
Expand Down Expand Up @@ -346,7 +342,7 @@ def _sndrcv_rcv(self, callback):
self.sniffer = AsyncSniffer()
self.sniffer._run(
prn=self._process_packet,
timeout=None if self.threaded else self.timeout,
timeout=None if self.threaded and not self._flood else self.timeout,
store=False,
opened_socket=self.rcv_pks,
session=self.session,
Expand Down
2 changes: 1 addition & 1 deletion test/contrib/automotive/doip.uts
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ def server_tcp():
server_tcp_up.set()
connection, address = sock.accept()
connection.send(buffer)
connection.shutdown()
connection.shutdown(socket.SHUT_RDWR)
connection.close()
finally:
sock.close()
Expand Down

0 comments on commit f4e06ce

Please sign in to comment.