Skip to content

fix(metal-agent): fail closed when memory admission check cannot complete - #641

Merged
Defilan merged 3 commits into
defilantech:mainfrom
Defilan:fix/metal-agent-memory-admission-fail-closed
Jun 10, 2026
Merged

Defilan merged 3 commits into
defilantech:mainfrom
Defilan:fix/metal-agent-memory-admission-fail-closed

Conversation

@Defilan

@Defilan Defilan commented Jun 10, 2026 •

Copy link
Copy Markdown
Member

What

The metal-agent's pre-flight memory check now fails closed: when the check cannot be completed (unknown model size, budget resolution error, memory query failure), the agent refuses to start the inference process, records SchedulingStatus: MemoryCheckFailed on the InferenceService, and emits a MemoryCheckFailed warning event. A new --memory-check-mode flag (enforce | warn, default enforce) keeps the old log-and-proceed behavior available as an operational escape hatch.

To keep the stricter check from rejecting legitimate not-yet-downloaded models, estimateModelMemory gains two fallbacks:

  • A Model status.size of "0" is treated as "not measured yet" instead of a hard parse error.
  • When neither the disk nor the status carries a size, an http(s) source is probed with a HEAD request (10s timeout) and sized from Content-Length, so admission is decided before the download starts.

Why

The previous behavior logged proceeding without check and started the process anyway. On a fresh host (empty model store, status.size still "0"), every InferenceService bypassed the memory budget this way. On 2026-06-09 concurrent unsized llama-server loads wired 123.4 GB of a 128 GB Apple Silicon host into unevictable memory (--mlock weights + Metal KV buffers); the machine stalled and died with a watchdog-timeout kernel panic. Admitting a model blind is strictly worse than refusing it: wired memory cannot be paged or jetsam-killed, so the failure mode is a host panic rather than a failed pod.

Fixes #640

How

  • Extracted the admission block from ensureProcess into checkMemoryAdmission; all three previously fail-open paths route through failMemoryCheck, which enforces or warns based on the resolved mode.
  • Added remoteModelSize (HEAD probe) in pkg/agent/memory.go and restructured estimateModelMemory into an explicit fallback chain (local source path → model store stat → status size → remote HEAD) that accumulates the reasons each source was unusable into the final error.
  • Unknown --memory-check-mode values resolve to enforce so a typo cannot select the dangerous direction; the resolved mode is logged in the startup memory budget line.
  • TestEnsureProcess_InFlightGuard now writes a real stub model file: its unsized model is correctly rejected by admission before reaching the StartProcess race it exercises.

Checklist

  • Tests added/updated
  • make test passes locally
  • make lint passes locally (plus GOOS=linux golangci-lint run ./... for the cross-arch files)
  • Commit messages follow conventional commits
  • All commits are signed off (git commit -s) per DCO
  • Documentation updated (macos-metal guide: memory budgets section + MemoryCheckFailed troubleshooting entry)

Defilan added 2 commits June 10, 2026 00:04
…lete

The pre-flight memory check logged "proceeding without check" whenever
the model size could not be determined and started the process anyway.
On a fresh host (model store empty, Model status.size still "0") every
service bypassed the budget this way; concurrent unsized llama-server
loads wired 123 GB of a 128 GB host into unevictable memory and the
machine died with a watchdog-timeout kernel panic.

Three changes:

- estimateModelMemory gains fallbacks: a status.size of "0" is treated
  as not-yet-populated instead of a hard parse error, and when neither
  the disk nor the status carries a size, an http(s) source is probed
  with a HEAD request and sized from Content-Length.
- The admission block is extracted into checkMemoryAdmission, and an
  estimation/budget failure now refuses to start the process, records
  SchedulingStatus=MemoryCheckFailed on the InferenceService, and emits
  a MemoryCheckFailed warning event.
- A new --memory-check-mode flag (enforce|warn, default enforce) keeps
  the old log-and-proceed behavior available as an operational escape
  hatch; unknown values resolve to enforce.

Signed-off-by: Christopher Maher <chris@mahercode.io>
…-mode

Signed-off-by: Christopher Maher <chris@mahercode.io>
@codecov

codecov Bot commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.11111% with 39 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/agent/agent.go 71.05% 23 Missing and 10 partials ⚠️
cmd/metal-agent/main.go 0.00% 4 Missing ⚠️
pkg/agent/memory.go 88.23% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

The watcher retries a refused InferenceService every poll interval and
zap attaches stacktraces at error level, flooding the agent log. The
condition is already surfaced via SchedulingStatus and a Kubernetes
warning event; warn matches the existing over-budget rejection path.

Signed-off-by: Christopher Maher <chris@mahercode.io>
@Defilan
Defilan merged commit cfe53aa into defilantech:main Jun 10, 2026
23 checks passed
@github-actions github-actions Bot mentioned this pull request Jun 9, 2026
@Defilan
Defilan deleted the fix/metal-agent-memory-admission-fail-closed branch June 10, 2026 07:39
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.

[BUG] Metal-agent memory admission fails open when model size cannot be determined

1 participant