Skip to content

Commit

Permalink
Provide a more helpful error message on spawn() failures
Browse files Browse the repository at this point in the history
Make a good-faith effort to catch the `RuntimeError` thrown when
improperly invoking the YumFinder on non-Linux platforms, and present
a useful error message to the user.

Drive-by: update `tox.ini` to work with 4.x

Fixes: Issue #31
  • Loading branch information
nisimond committed Jan 27, 2023
1 parent 0a49fd3 commit 4f5588c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
16 changes: 15 additions & 1 deletion soufi/finders/yum.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import abc
import lzma
import pickle # nosec
import sys
import warnings
from multiprocessing import Process, Queue
from types import SimpleNamespace
Expand Down Expand Up @@ -230,7 +231,20 @@ def do_task(target, *args):
"""Run the target callable in a subprocess and return its response."""
queue = Queue()
process = Process(target=target, args=(queue,) + args)
process.start()
try:
process.start()
except RuntimeError:
sys.exit(
"""
FATAL: Running this finder directly from the global scope is not supported on
this platform. To use this finder, call it instead from the main
module, e.g.:
if __name__ == '__main__':
soufi.finder.factory(*args, **kwargs).find()
Aborting."""
)
# We don't want to wait *forever*, but jobs can take several minutes to
# complete, so wait a relatively long time
response = queue.get(timeout=600)
Expand Down
10 changes: 10 additions & 0 deletions soufi/tests/finders/test_yum_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,16 @@ def test_do_task_reraises_exceptions(self):
err = self.assertRaises(RuntimeError, yum.do_task, kaboom, data)
self.assertEqual(data, str(err))

def test_do_task_handles_spawn_errors_on_silly_platforms(self):
# This simulates calling `process.start()` from the global scope on
# platforms that do not support such things. The default traceback
# is not intrinsically helpful, so test that we kick back a more
# useful error message. See issue #31.
process = self.patch(yum, 'Process')
process.return_value.start.side_effect = RuntimeError
err = self.assertRaises(SystemExit, yum.do_task, None)
self.assertIn('FATAL: ', str(err))


# A simple subprocess function that throws a test exception. Used by
# TestYumFinderHelpers.test_do_task_reraises_exceptions
Expand Down
14 changes: 9 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ setenv =
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt

whitelist_externals =
allowlist_externals =
bash
echo
mkdir
Expand Down Expand Up @@ -94,10 +94,14 @@ exclude =
.eggs
show-source = true
ignore =
D10 # ignore missing docstrings (for now)
D202 # No blank lines allowed after function docstring (caused by Black)
E203 # Whitespace before ':' (caused by Black)
W503 # line breaks after operators (caused by Black)
# ignore missing docstrings (for now)
D10
# No blank lines allowed after function docstring (caused by Black)
D202
# Whitespace before ':' (caused by Black)
E203
# line breaks after operators (caused by Black)
W503

[testenv:functional]
commands =
Expand Down

0 comments on commit 4f5588c

Please sign in to comment.