Interval#splitBy / #divideEqually hangs forever on a negative duration (still reproducible on 3.7.2, distinct from #186)
Describe the bug
Interval#splitBy(duration) only rejects a duration whose millisecond value is exactly zero (dur.as("milliseconds") === 0). It never checks sign or finiteness. With a negative duration (or divideEqually called with a negative or 0 numberOfParts), the loop's cursor moves away from the terminal condition, so while (s < this.e) never becomes false. The process spins at 100%+ CPU, pushing into an ever-growing results array, until forcibly killed or the process runs out of memory.
This looks related to #186, but it's a different, still-open gap: the fix that closed #186 (#261) only made Duration.fromObject({}) (an empty object, e.g. passing a bare DateTime in) return an Invalid Duration. A duration with real negative values, e.g. { hours: -1 }, is a perfectly valid Duration and is not caught by that fix — it still hits the same non-terminating loop today.
To Reproduce
import { Interval } from 'luxon';
// hangs forever, pins one core
Interval.fromISO('2024-01-01/2024-01-02').splitBy({ hours: -1 });
// same underlying bug via divideEqually
Interval.fromISO('2024-01-01/2024-02-01').divideEqually(-1);
Interval.fromISO('2024-01-01/2024-02-01').divideEqually(0); // length()/0 === Infinity, also never hits the ===0 guardVerified against the actual 3.7.2 source (not a reimplementation): the call never returns, and ps shows the process pinned at 100%+ CPU with memory still climbing several seconds in:
$ node poc.mjs &
$ ps -o pid,pcpu,pmem,etime,comm -p <pid>
PID %CPU %MEM ELAPSED COMM
13063 346.8 2.8 00:06 nodeActual vs Expected behavior
Actual: the call never returns; CPU pins at 100%+; memory grows unbounded.
Expected: splitBy/divideEqually should reject/return [] for a non-positive or non-finite duration, the same way it already does for exactly-zero.
Suggested fix, mirroring the existing zero-check in Interval#splitBy (src/interval.js):
splitBy(duration) {
const dur = Duration.fromDurationLike(duration);
const ms = dur.as("milliseconds");
if (!this.isValid || !dur.isValid || !Number.isFinite(ms) || ms <= 0) {
return [];
}
...and validating numberOfParts is a finite integer >= 1 in divideEqually before delegating to splitBy. A loop-progress assertion (e.g. if (+next <= +s) break;) would also be reasonable defense-in-depth.
Desktop (please complete the following information):
- OS: Linux/macOS (platform-independent — pure JS loop)
- Luxon version: 3.7.2 (current latest at time of testing; verified against source at GitHub tag
3.7.2) - Node: v24.16.0 (reproduces on any Node in luxon's supported range)
Additional context
Since a single request-supplied negative duration or non-positive part count can wedge Node's single-threaded event loop for the whole process (not just the caller's own request), this has denial-of-service implications for any server-side code that forwards a request-derived duration/part-count into splitBy/divideEqually (e.g. scheduling/billing/calendar-slicing endpoints). Happy to open a PR with the one-line fix above if that's useful.
Source: moment/luxon