Skip to content

Fix container state and memory leaks, and move reports into a service - #5

Merged
armartinez merged 1 commit into
try-containers:mainfrom
armartinez:container-fixes-and-reports-service
Oct 24, 2026
Merged

armartinez merged 1 commit into
try-containers:mainfrom
armartinez:container-fixes-and-reports-service

Conversation

@armartinez

Copy link
Copy Markdown
Contributor

Important

For all but trivial fixes, make sure to first create a GitHub issue that concisely describes the bug or desired enhancement as justification for the change. Large PRs with no justifying issue will be closed.

Related Issue

Closes #

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Motivation and Context

Starting or stopping several containers at once left their state dots wrong: a row was disabled, but its dot still showed the old state. All container boots and stops shared one lock, so each container waited for the others' stop timeouts before its own state changed.

Memory profiling with leaks, heap and vmmap found that closed report and container detail windows were never freed. Retain cycles kept their views alive, with about 160 MB of window graphics. It also found that a container whose start fails kept its virtual machine object, log file handle and IP address until the app quit. The container also couldn't be deleted or started again.

Reports were written through ReportManager, and other managers created a ReportManager of their own to delete reports. That broke the rule that a manager never uses another manager.

Description

Container state and concurrency

  • One lock per container: ContainersService now uses a lock for each container instead of one for all of them, so containers start and stop in parallel. The lock still keeps one container's boot, start, stop and delete from overlapping.
  • Start and delete take the lock: startProcess and delete now use it too. A stop can no longer land between a container's boot and the start of its process. A container that is booting can't be deleted, which would have left its virtual machine running with nothing to stop it.
  • Stopping reads fresh state: a stop now reads the container's state again after waiting for it to stop. Before, it wrote back an older copy: that could erase the exit code and bring back an auto-remove container that had already been deleted.
  • Failed starts let go of the VM: a container whose start fails now releases its virtual machine right away, along with its IP address and log file handle.
  • Container list refreshes in order: ContainersView runs its list refreshes one at a time, so an older list can no longer overwrite a newer one.

ContainersService structure

  • Moved and split: ContainersService now lives in Runtime/Services/Containers/, split by area into +Lifecycle, +Configuration, +Processes, +Networking and +Streams.
  • Duplicated code removed:
    • Stopping a container and handling its exit now share one teardown, markStopped(id:exitCode:).
    • Shared helpers replace the repeated "not found" and "not running" checks.
    • Building the stdio writers, the bundle path and the IP addresses is simpler.

Memory leaks

  • Toolbar binders: the report, dashboard and detail toolbar binders now release their controllers' closures in dismantleNSView. Each controller is kept in the @State of the same view those closures capture.
  • Detail toolbar observation: DetailToolbarController's display-mode observation captures self weakly.
  • Error alerts: .errorAlert holds its window weakly. Holding it strongly kept closed windows alive.

Reports

  • New ReportsService: an actor that the runtime creates and owns, like the other services. ReportManager records reports through it.
  • Deleting reports: ContainerManager, ImageManager and VolumeManager delete reports through the runtime's getReportsService(), not through a ReportManager of their own.
  • API renames: ReportManager.remove(_:) is now delete(ids:), and entry(for:) is now get(id:).

App

  • Image name suggestions: they now use .task(id:) instead of a Combine debounce.
  • Back to the dashboard after a run: creating a container with "Run" opens the dashboard's Containers tab, where you can follow the start. This uses a new containers app link.
  • Status bar wording: status messages read "Container system" instead of "Container System".

Docs

  • AGENTS.md: updated for the new Runtime/ layout, the runtime's service accessors and ReportsService.
  • Doc comments: added to the public API of the managers.

Testing

  • Tested locally
  • Added/updated tests
  • Added/updated docs

Added ReportManagerTests and a test that deleting a volume deletes its reports, and updated MockContainerRuntime to provide a ReportsService.

Checked with leaks, heap and vmmap after opening and closing report and container detail windows several times, starting and stopping containers, and starting a container whose image has no entry point:

  • leaks: 19 leaks before, 0 after.
  • Detail views and toolbar controllers left after closing their windows: 6 views and 12 controllers before, none after.
  • Window graphics memory: 161 MB before, 2.3 MB after.
  • Container objects with no VM running: 1 before, none after.
  • stdio.log handles left open: 1 before, none after.

@armartinez armartinez self-assigned this Oct 24, 2026
@armartinez
armartinez merged commit 684314e into try-containers:main Oct 24, 2026
1 check passed
@armartinez
armartinez deleted the container-fixes-and-reports-service branch November 10, 2026 20:27
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