Agent: Add sleep back into _check_tcp_ports()

This commit is contained in:
Mike Salvatore 2022-02-10 10:49:21 -05:00 committed by Ilija Lazoroski
parent 21ede3e341
commit 36a2b3ff6b
1 changed files with 7 additions and 0 deletions

View File

@ -1,6 +1,7 @@
import logging
import select
import socket
import time
from typing import Iterable, Mapping, Tuple
from infection_monkey.i_puppet import PortScanData, PortStatus
@ -9,6 +10,8 @@ from infection_monkey.utils.timer import Timer
logger = logging.getLogger(__name__)
POLL_INTERVAL = 0.5
def scan_tcp_ports(
host: str, ports_to_scan: Iterable[int], timeout: float
@ -83,6 +86,10 @@ def _check_tcp_ports(
timer.set(timeout)
while (not timer.is_expired()) and sockets_to_try:
# The call to select() may return sockets that are writeable but not actually
# connected. Adding this sleep prevents excessive looping.
time.sleep(min(POLL_INTERVAL, timer.time_remaining))
sock_objects = [s[1] for s in sockets_to_try]
_, writeable_sockets, _ = select.select([], sock_objects, [], timer.time_remaining)