Skip to content

templates: reconcile fork refcounts and sweep orphans on boot#215

Draft
sjmiller609 wants to merge 1 commit intohypeship/fork-shared-memfilefrom
hypeship/template-gc
Draft

templates: reconcile fork refcounts and sweep orphans on boot#215
sjmiller609 wants to merge 1 commit intohypeship/fork-shared-memfilefrom
hypeship/template-gc

Conversation

@sjmiller609
Copy link
Copy Markdown
Collaborator

Stacked on: #214 (shared mem-file) — review #213#214 first.

Summary

  • Adds Registry.Reconcile(ctx, observed map[string]int) that walks every template and rewrites ForkCount to match the observed map.
  • Adds manager-side reconcileTemplateState that scans every instance's metadata, sums ForkOfTemplate references, calls Registry.Reconcile, then sweeps registry entries whose source instance is gone and ForkCount is 0.
  • Wires reconciliation into manager init and exposes ReconcileTemplateState for callers that want to re-run it.
  • Tests: TestFileRegistry_Reconcile exercises 3 templates with starting counts 5/0/7 reconciled to 2/3/0.

Why

ForkCount drifts when forks are deleted out from under hypeman (crash, SIGKILL, on-disk surgery). Without reconciliation a template can be permanently "in use" and refuse delete. Reconciling on boot keeps the registry honest and lets Delete block on actual live forks instead of stale counters.

Test plan

  • go test ./lib/templates/...
  • go test ./lib/instances/...
  • Manual: kill a fork's process, drop its metadata, restart hypeman, observe Reconcile repaired the count and the template can be deleted

🤖 Generated with Claude Code

Drift between the templates registry and the actual fork population can
appear after a crash, an out-of-band fork delete, or any path that
bypassed the increment/decrement pair. The new Registry.Reconcile takes
a map of observed fork counts and rewrites every template's ForkCount
in place.

The instances manager wires this in two ways:
  - On NewManager, walks instance metadata, sums ForkOfTemplate, and
    calls Reconcile so a restart heals stale counts.
  - Sweeps orphaned registry entries whose source instance no longer
    exists and that have zero live forks; entries with live forks are
    left alone because the forks still hold the mem-file open via the
    shared symlink.

Exposes ReconcileTemplateState on the manager so a host process can
schedule periodic GC ticks alongside other sweepers.
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.

1 participant