runners/jobs: don't iterate non-iterable Target values in list_jobs#68969
Closed
SAY-5 wants to merge 1 commit into
Closed
runners/jobs: don't iterate non-iterable Target values in list_jobs#68969SAY-5 wants to merge 1 commit into
SAY-5 wants to merge 1 commit into
Conversation
bdrx312
reviewed
Apr 20, 2026
| targets = ret[item]["Target"] | ||
| if isinstance(targets, str): | ||
| targets = [targets] | ||
| elif not isinstance(targets, (list, tuple, set)): |
Contributor
There was a problem hiding this comment.
Can just check if the type is Iterable instead of trying to enumerate possible iterable types.
from collections.abc import Iterable
isinstance(obj, Iterable)
I suggest not silently masking errors though and instead log a warning or at least a debug/trace message.
Failed or aborted jobs can leave Target as None or some other non-iterable value in the job cache. list_jobs used to iterate it unconditionally, raising TypeError and aborting the whole listing, which made it impossible to even see the malformed entries in order to fix them. Replace the explicit (list, tuple, set) instance check with a single collections.abc.Iterable check per @bdrx312's review feedback - that covers any future iterable container the cache may grow without re-listing every type. Strings are pulled out beforehand so a single target string still gets wrapped in a list rather than being treated as a sequence of characters. When a non-iterable Target is hit, log it at DEBUG with the offending job id so operators have a breadcrumb back to the malformed cache row, rather than the previous silent skip. Add a regression test that mixes a normal job entry with one carrying Target=None, asserts the normal one is still matched, the bad one is filtered out, and the debug log carries the bad jid. The test fails on master with a missing log entry (because the old code silently skipped), confirming both behaviour changes. Closes saltstack#68780 Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com>
ed80d4a to
ed84164
Compare
Contributor
Author
|
Thanks @bdrx312 — applied both suggestions:
Verified locally on macOS, Python 3.13:
Net diff: jobs.py +5 / -2; new test |
Contributor
|
Also there is another PR also attempting to fix this #68862 |
Contributor
Author
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Hardens
salt.runners.jobs.list_jobsagainst non-iterableTargetvalues in the job cache.list_jobs'ssearch_targetfilter readsTargetfor each cached job, wraps strings in a list, then iterates:If
Targetis neither a string nor an iterable — which happens for failed or aborted jobs (#68780) —for target in targets:raisesTypeErrorand kills the whole query, not just the one bad entry.This PR adds a small
elif not isinstance(targets, (list, tuple, set)): targets = []guard so a malformed Target is treated the same way a missingTargetkey already is: no target to match, skip and move on. String/list/tuple/set behaviour is unchanged.What issues does this PR fix or reference?
Fixes #68780
Previous Behavior
Calling
jobs.list_jobs search_target=...against a job cache that contains any entry with a non-iterableTarget(e.g.Target: Nonefrom a failed job) raises:and
list_jobsreturns no result at all.New Behavior
Entries with a non-iterable
Targetare silently skipped fromsearch_targetmatching; the rest of the query returns as expected. String/list/tuple/set targets keep working exactly as before.Merge requirements satisfied?
changelog/68780.fixed.mdlist_jobsloop; happy to add a targeted unit test that seeds the cache withTarget: Noneand assertslist_jobsreturns rather than raises, if the maintainers prefer before merge.Commits signed with GPG?
DCO-signed off (
Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com>).