@@ -1278,21 +1278,39 @@ def _terminate_processes_matching_listen_ports(self) -> None:
1278
1278
# If any processes were not terminated and are listening on the ports
1279
1279
# we have set on listen_ports, terminate those processes.
1280
1280
found_processes = []
1281
- for process in psutil .process_iter (["net_connections" ]):
1282
- try :
1283
- for connection in process .net_connections ():
1284
- if connection .status != psutil .CONN_LISTEN :
1285
- # We only care about listening services
1286
- continue
1287
- if connection .laddr .port in self .check_ports :
1288
- found_processes .append (process )
1289
- # We already found one connection, no need to check the others
1290
- break
1291
- except psutil .AccessDenied : # pragma: no cover
1292
- # We've been denied access to this process net_connections. Carry on.
1293
- continue
1294
- except psutil .ZombieProcess :
1295
- continue
1281
+ psutil_majorver , _ , _ = psutil .version_info
1282
+ if psutil_majorver < 6 :
1283
+ for process in psutil .process_iter (["connections" ]):
1284
+ try :
1285
+ for connection in process .connections ():
1286
+ if connection .status != psutil .CONN_LISTEN :
1287
+ # We only care about listening services
1288
+ continue
1289
+ if connection .laddr .port in self .check_ports :
1290
+ found_processes .append (process )
1291
+ # We already found one connection, no need to check the others
1292
+ break
1293
+ except psutil .AccessDenied : # pragma: no cover
1294
+ # We've been denied access to this process connections. Carry on.
1295
+ continue
1296
+ except psutil .ZombieProcess :
1297
+ continue
1298
+ else :
1299
+ for process in psutil .process_iter (["net_connections" ]):
1300
+ try :
1301
+ for connection in process .net_connections ():
1302
+ if connection .status != psutil .CONN_LISTEN :
1303
+ # We only care about listening services
1304
+ continue
1305
+ if connection .laddr .port in self .check_ports :
1306
+ found_processes .append (process )
1307
+ # We already found one connection, no need to check the others
1308
+ break
1309
+ except psutil .AccessDenied : # pragma: no cover
1310
+ # We've been denied access to this process net_connections. Carry on.
1311
+ continue
1312
+ except psutil .ZombieProcess :
1313
+ continue
1296
1314
if found_processes :
1297
1315
log .debug (
1298
1316
"The following processes were found listening on ports %s: %s" ,
0 commit comments