Skip to content

Commit

Permalink
Revert requirements for psutil and allow for connections / net_connec…
Browse files Browse the repository at this point in the history
…tions based on psutil version (#55)

* Revert requirements for psutil and allow for connections / net_connections based on psutil version

* Added changelog entry
  • Loading branch information
dmurphy18 authored Oct 22, 2024
1 parent cb4f180 commit 37cce29
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
1 change: 1 addition & 0 deletions changelog/55.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Revert requirements for psutil and allow for connections / net_connections based on psutil version
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pytest>=7.4.0
attrs>=22.1.0
psutil>=6.0.0
psutil>=5.0.0
pytest-helpers-namespace
pytest-skip-markers
48 changes: 33 additions & 15 deletions src/pytestshellutils/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,21 +1278,39 @@ def _terminate_processes_matching_listen_ports(self) -> None:
# If any processes were not terminated and are listening on the ports
# we have set on listen_ports, terminate those processes.
found_processes = []
for process in psutil.process_iter(["net_connections"]):
try:
for connection in process.net_connections():
if connection.status != psutil.CONN_LISTEN:
# We only care about listening services
continue
if connection.laddr.port in self.check_ports:
found_processes.append(process)
# We already found one connection, no need to check the others
break
except psutil.AccessDenied: # pragma: no cover
# We've been denied access to this process net_connections. Carry on.
continue
except psutil.ZombieProcess:
continue
psutil_majorver, _, _ = psutil.version_info
if psutil_majorver < 6:
for process in psutil.process_iter(["connections"]):
try:
for connection in process.connections():
if connection.status != psutil.CONN_LISTEN:
# We only care about listening services
continue
if connection.laddr.port in self.check_ports:
found_processes.append(process)
# We already found one connection, no need to check the others
break
except psutil.AccessDenied: # pragma: no cover
# We've been denied access to this process connections. Carry on.
continue
except psutil.ZombieProcess:
continue
else:
for process in psutil.process_iter(["net_connections"]):
try:
for connection in process.net_connections():
if connection.status != psutil.CONN_LISTEN:
# We only care about listening services
continue
if connection.laddr.port in self.check_ports:
found_processes.append(process)
# We already found one connection, no need to check the others
break
except psutil.AccessDenied: # pragma: no cover
# We've been denied access to this process net_connections. Carry on.
continue
except psutil.ZombieProcess:
continue
if found_processes:
log.debug(
"The following processes were found listening on ports %s: %s",
Expand Down

0 comments on commit 37cce29

Please sign in to comment.