Skip to content

Commit

Permalink
Improve exception handling in PeriodicSender Thread (#4152)
Browse files Browse the repository at this point in the history
  • Loading branch information
polybassa authored Oct 25, 2023
1 parent d54a457 commit b95fdc7
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions scapy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3171,8 +3171,8 @@ def whois(ip_address):


class PeriodicSenderThread(threading.Thread):
def __init__(self, sock, pkt, interval=0.5):
# type: (Any, _PacketIterable, float) -> None
def __init__(self, sock, pkt, interval=0.5, ignore_exceptions=True):
# type: (Any, _PacketIterable, float, bool) -> None
""" Thread to send packets periodically
Args:
Expand All @@ -3187,13 +3187,20 @@ def __init__(self, sock, pkt, interval=0.5):
self._socket = sock
self._stopped = threading.Event()
self._interval = interval
self._ignore_exceptions = ignore_exceptions
threading.Thread.__init__(self)

def run(self):
# type: () -> None
while not self._stopped.is_set() and not self._socket.closed:
for p in self._pkts:
self._socket.send(p)
try:
self._socket.send(p)
except (OSError, TimeoutError) as e:
if self._ignore_exceptions:
return
else:
raise e
self._stopped.wait(timeout=self._interval)
if self._stopped.is_set() or self._socket.closed:
break
Expand Down

0 comments on commit b95fdc7

Please sign in to comment.