Skip to content

fix: preserve explicit priority 0 in facility ordering#22

Open
AI-Reviewer-QS wants to merge 1 commit into
bitfinexcom:masterfrom
AI-Reviewer-QS:fix/priority-zero-mutation
Open

fix: preserve explicit priority 0 in facility ordering#22
AI-Reviewer-QS wants to merge 1 commit into
bitfinexcom:masterfrom
AI-Reviewer-QS:fix/priority-zero-mutation

Conversation

@AI-Reviewer-QS

Copy link
Copy Markdown

Summary

  • Fix facs() treating explicit priority 0 as unset due to JavaScript falsy semantics (!0 === true)
  • The if (!p[5]) p[5] = 1 check on base.js:152 mutates facilities registered with priority 0 to priority 1, corrupting the intended startup and shutdown ordering
  • Also fix f[5] || 0 in start() and stop() sort comparators which have the same issue

Problem

When a facility is registered with explicit priority 0 via setInitFacs():

this.setInitFacs([
  ['fac', 'hp-svc-facs-store', 's0', 's0', { storeDir }, 0],  // priority 0
  ['fac', 'hp-svc-facs-net', 'r0', 'r0', () => ({ fac_store: this.store_s0 }), 1],
  ['fac', 'svc-facs-logging', 'l0', 'l0', { name, mixin: this.loggerMixin.bind(this) }, 2]
])

The facs() method mutates priority 0 to 1 because !0 is true in JavaScript. After start(), the store facility and net facility share the same priority (1), making shutdown order non-deterministic. The intended shutdown order (log -> net -> store) is not guaranteed.

Fix

Replace falsy checks with nullish checks:

Location Before After
facs() default if (!p[5]) p[5] = 1 if (p[5] == null) p[5] = 1
start() sort f[5] || 0 f[5] ?? 0
stop() sort (f[5] || 0) * -1 (f[5] ?? 0) * -1

Test plan

  • Added unit tests verifying priority 0 is preserved after facs() runs
  • Added unit test verifying correct shutdown order (log -> net -> store)
  • Existing tests pass
  • Linter passes

The facs() method uses `if (!p[5]) p[5] = 1` to assign a default
priority to facilities that don't have one. However, `!0` is `true`
in JavaScript, so facilities explicitly registered with priority 0
are silently mutated to priority 1.

This corrupts the shutdown order: facilities at priority 0 (intended
to shut down last) end up sharing priority 1 with other facilities,
making teardown ordering between them non-deterministic.

Fix all three locations:
- facs(): `if (!p[5])` -> `if (p[5] == null)` for default assignment
- start(): `f[5] || 0` -> `f[5] ?? 0` for sorting
- stop(): `(f[5] || 0) * -1` -> `(f[5] ?? 0) * -1` for reverse sorting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants