fix(pulse): validate cron schedules at config load (crash loop + hang) - #1644
Open
elhoim wants to merge 1 commit into
Open
fix(pulse): validate cron schedules at config load (crash loop + hang)#1644elhoim wants to merge 1 commit into
elhoim wants to merge 1 commit into
Conversation
A typo in the user-editable PULSE.toml could take the whole daemon down
in two different ways:
- Wrong field count ("0 9 * *") threw out of isDue(), outside the
per-job try/catch that only wraps execution. The throw escaped the
scheduler loop into main().catch -> process.exit(1), and both
supervisors restart on a 30s throttle, so one bad job definition
turned the dashboard into a 30-second crash cycle.
- Zero step ("*/0 * * * *") spun forever inside parseField: the loop
counter never advanced while the values array grew without bound,
blocking the event loop until the process ran out of memory.
Schedules are now validated by validateCron() as the config is read,
once, instead of throwing on every scheduler tick. An expression that
does not parse disables exactly one job and is reported with the job
name, the expression and the reason; the job stays in the list carrying
scheduleError so the dashboard can show what needs fixing. Everything
else keeps running.
The parser is now total and rejects the pathological class rather than
individual cases: zero and negative steps, out-of-range values,
inverted ranges, unparseable terms, oversized expressions and wrong
field counts. parseField can no longer loop. Day-of-week 7 and month
names, which used to parse into values that could never match, now fail
loudly at load instead of silently never firing. Both scheduler call
sites are guarded as a backstop: isDue() in the tick loop and
matchesCron() in msUntilNextDue().
Tests cover both failure modes plus the step, range, list and wildcard
forms the shipped config uses; the pathological-expression test runs in
a subprocess with a hard kill, so a regression fails the run instead of
hanging it.
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.
The problem
Two ways a single typo in the user-editable
PULSE.tomltakes the dashboard down. Nothing validates cron strings anywhere —jobsFromTomlonly castsj.schedule as string.1. Crash loop.
isDue()is called atpulse.ts:925, outside the per-jobtryat 937 which wraps only execution.isDue('0 9 * *')throwsInvalid cron (need 5 fields), the throw escapes the scheduling loop intomain().catch, and that callsprocess.exit(1). Both supervisors restart it — launchdKeepAlivewith a 30s throttle, systemdRestart=on-failurewithRestartSec=30— so a four-field schedule turns the daemon into a 30-second crash cycle instead of one skipped job.msUntilNextDueat line 401 makes the same unguarded call.2. Hang.
parseFieldinlib.ts:156accepts a zero step.matchesCron('*/0 * * * *', …)never terminates: the loop counter never advances while the values array grows without bound, so the event loop is blocked and the process OOMs. Reproduced by a run that had to be killed by timeout.Neither requires an attacker. Both are what a mistyped schedule does.
The fix
Validate at config load.
validateCron()checks field count, ranges, list and range forms, and step values. A job whose schedule fails validation is force-disabled at load with a message naming the job, the expression and the specific reason:The daemon keeps running and keeps serving every other job.
Reject the pathological class, not the two reported cases. Zero and negative steps, out-of-range values, inverted ranges, wrong field counts, and empty schedules are all refused, including when they appear inside a list or attached to a range (
*/0,*/0 * * * *,1-5/0 * * * *).Guard both evaluation call sites as a backstop.
matchesCron/isDuestill throw on an invalid expression, which is deliberate: an unvalidated expression reaching them is a programming error, not user input, and silently returningfalsewould hide it. Both live call sites are wrapped so that if one ever does slip through, the job is disabled once and named, rather than taking the process out. The scheduler tracks those jobs so the message appears once instead of every tick, and the sleep-calculation path treats an unusable schedule as never due without re-reporting.Tests
test/pulse/lib.test.ts, 53 cases:Independently verified with a separate bounded probe — the previously-hanging expression is exercised first, so a regression would show as a timeout kill rather than a pass. It completed normally:
Note for review
pulse-old.tsandpulse-unified.tscontain the same unguarded call sites and are deliberately left alone here — they are superseded dead code, and #1630 proposes deleting them. Worth confirming that is still the intent rather than my patching files that are on their way out.