Skip to content

Commit

Permalink
[pkg] Don't initialize _packages to empty set
Browse files Browse the repository at this point in the history
When initializing _packages, a distinction must be made between not
having any package and not being initialized at all.
Otherwise, if a package manager returns no package, the query command
will execute hundreds of times.

This can reproduce with 'flatpak' on RHEL when no package is returned:

  # flatpak list
  --> no output

  # strace -fttTvyy -s 128 -e execve -o sos.strace -- ./bin/sos report
  [...]
  Press ENTER to continue, or CTRL-C to quit.
  ^C

  # grep -c ' execve("/usr/sbin/flatpak"' sos.strace
  350

Signed-off-by: Renaud Métrich <[email protected]>
  • Loading branch information
rmetrich authored and TurboTurtle committed Apr 22, 2024
1 parent bf53011 commit 3b15149
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions sos/policies/package_managers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class PackageManager():
files = None

def __init__(self, chroot=None, remote_exec=None):
self._packages = {}
self._packages = None
self.files = []
self.remote_exec = remote_exec

Expand All @@ -63,7 +63,7 @@ def __init__(self, chroot=None, remote_exec=None):

@property
def packages(self):
if not self._packages:
if self._packages is None:
self._generate_pkg_list()
return self._packages

Expand Down Expand Up @@ -181,6 +181,9 @@ def _generate_pkg_list(self):
'pkg_manager': 'package manager name'}}
"""
if self._packages is None:
self._packages = {}

if self.query_command:
cmd = self.query_command
pkg_list = self.exec_cmd(cmd, timeout=30, chroot=self.chroot)
Expand Down Expand Up @@ -340,6 +343,9 @@ def all_files(self):
return self.files

def _generate_pkg_list(self):
if self._packages is None:
self._packages = {}

self._packages.update(self.primary.packages)
for pm in self.fallbacks:
_pkgs = pm.packages
Expand Down

0 comments on commit 3b15149

Please sign in to comment.