Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0e8b1c8

Browse files
author
Sofia Kopikova
committed
Catch FileNotFoundError exception
When we want to find some Postgres process in testgres psutils lib first gets list of all system processes (children() function in testgres's auxiliary_processes). Next testgres asks for process name (cmdline() function), so psutils goes to /proc/pid/ in Linux. In case any of processes listed in previous step, we get FileNotFoundError
1 parent ceb1ee9 commit 0e8b1c8

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

testgres/enums.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from enum import Enum, IntEnum
22
from six import iteritems
3+
from psutil import NoSuchProcess
34

45

56
class XLogMethod(Enum):
@@ -68,11 +69,15 @@ def from_process(process):
6869
],
6970
} # yapf: disable
7071

72+
try:
73+
cmdline = ''.join(process.cmdline())
74+
except (FileNotFoundError, ProcessLookupError, NoSuchProcess):
75+
return ProcessType.Unknown
76+
7177
# we deliberately cut special words and spaces
72-
cmdline = ''.join(process.cmdline()) \
73-
.replace('postgres:', '', 1) \
74-
.replace('bgworker:', '', 1) \
75-
.replace(' ', '')
78+
cmdline = cmdline.replace('postgres:', '', 1) \
79+
.replace('bgworker:', '', 1) \
80+
.replace(' ', '')
7681

7782
for ptype in ProcessType:
7883
if cmdline.startswith(ptype.value.replace(' ', '')):

tests/test_simple.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import time
1010
import six
1111
import unittest
12+
import psutil
1213

1314
import logging.config
1415

@@ -48,6 +49,7 @@
4849
# NOTE: those are ugly imports
4950
from testgres import bound_ports
5051
from testgres.utils import PgVer
52+
from testgres.node import ProcessProxy
5153

5254

5355
def pg_version_ge(version):
@@ -965,6 +967,18 @@ def test_child_pids(self):
965967
with self.assertRaises(TestgresException):
966968
replica.source_walsender
967969

970+
def test_child_process_dies(self):
971+
# test for FileNotFound exception during child_processes() function
972+
with subprocess.Popen(["sleep", "60"]) as process:
973+
self.assertEqual(process.poll(), None)
974+
# collect list of processes currently running
975+
children = psutil.Process(os.getpid()).children()
976+
# kill a process, so received children dictionary becomes invalid
977+
process.kill()
978+
process.wait()
979+
# try to handle children list -- missing processes will have ptype "ProcessType.Unknown"
980+
[ProcessProxy(p) for p in children]
981+
968982

969983
if __name__ == '__main__':
970984
if os.environ.get('ALT_CONFIG'):

0 commit comments

Comments
 (0)