8000 Catch FileNotFoundError exception by MetalDream666 · Pull Request #74 · postgrespro/testgres · GitHub
[go: up one dir, main page]

Skip to content

Cat 8000 ch FileNotFoundError exception #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
  • Loading branch information
Sofia Kopikova committed Mar 30, 2023
commit 0e8b1c8f59bd1b57d31b7d31ba82c4efa6149d0a
13 changes: 9 additions & 4 deletions testgres/enums.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from enum import Enum, IntEnum
from six import iteritems
from psutil import NoSuchProcess


class XLogMethod(Enum):
Expand Down Expand Up @@ -68,11 +69,15 @@ def from_process(process):
],
} # yapf: disable

try:
cmdline = ''.join(process.cmdline())
except (FileNotFoundError, ProcessLookupError, NoSuchProcess):
return ProcessType.Unknown

# we deliberately cut special words and spaces
cmdline = ''.join(process.cmdline()) \
.replace('postgres:', '', 1) \
.replace('bgworker:', '', 1) \
.replace(' ', '')
cmdline = cmdline.replace('postgres:', '', 1) \
.replace('bgworker:', '', 1) \
.replace(' ', '')

for ptype in ProcessType:
if cmdline.startswith(ptype.value.replace(' ', '')):
Expand Down
14 changes: 14 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import time
import six
import unittest
import psutil

import logging.config

Expand Down Expand Up @@ -48,6 +49,7 @@
# NOTE: those are ugly imports
from testgres import bound_ports
from testgres.utils import PgVer
from testgres.node import ProcessProxy


def pg_version_ge(version):
Expand Down Expand Up @@ -965,6 +967,18 @@ def test_child_pids(self):
with self.assertRaises(TestgresException):
replica.source_walsender

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


if __name__ == '__main__':
if os.environ.get('ALT_CONFIG'):
Expand Down
0