[data-reveal]{opacity:1!important;transform:none!important}
DocumentationModule cache

Module cache

How dev, build and the other commands share .antelope/cache, which folders a prune keeps, and how runs on other hosts and in containers are protected.

Overview

.antelope/cache holds a folder for each module version the project has loaded, listed in its manifest.json. Once ajs project dev, ajs project run or ajs project build has loaded the modules, it removes every listed folder that nothing keeps. A folder stays while any of these holds:

  • The lease of a live run names it. Every run that loads modules from the cache, start, ajs module test and the embedded runtime included, writes the folders it uses to .antelope/cache/.leases/<host>-<namespace>-<pid>-<uuid>.json before it reads them, and deletes that file when its process exits. <namespace> is the number of the run's pid namespace on Linux, and 0 on a system where the core cannot read it.
  • The project's .antelope/build/build.json points at it.
  • A build pin points at it. build pins its build.json in .antelope/cache/.builds/, so the build of a project that shares the cache through an absolute cacheFolder keeps its folders when another project prunes. A pin whose build.json is gone is deleted. When the cache refuses the pin, on a full disk or a read-only cache, build warns and finishes without it.

A folder that manifest.json does not list is never removed. Neither is a listed name that points outside the cache or into a folder whose name starts with a dot, such as .leases.

Downloads

Two runs that need the same module version never download it at the same time. A run loads each npm or git module version in this order:

  1. It takes the download lock of that version, .antelope/cache/.downloads/<version>.lock. While another run downloads the same version, it waits.
  2. It reads manifest.json again.
  3. When the manifest lists the version and its folder is complete, it uses that folder. An npm module's folder is complete when it holds its package.json and, for each dependency that file declares, node_modules/<dependency>/package.json. A dependency that optionalDependencies also lists is not required, since npm skips it on a platform it does not support. A git clone that the manifest lists, that has a node_modules folder and that lacks one of those files has its install commands run again after the fetch, unless another live run uses it. A clone without node_modules, such as a Yarn Plug'n'Play install, is not checked. A frozen or recorded module also needs the folder to be installed from its lock.
  4. Otherwise, when the live run of another process uses a folder of that version that the manifest lists, it keeps that folder as it is and logs which run uses it. This holds for ajs project modules update and for a source with ignoreCache too: update records the lock of that folder and reports the module as kept. A version is listed only once its install has returned, so a folder that a stopped or killed install left is never kept: the run fails naming the run that holds it, and once that run is gone, the next run downloads the version again. A git clone that another run uses is neither fetched nor switched. It is kept only when it has the requested commit checked out: the commit its commit names (a SHA or a tag), else the remote tip of the branch, else of the default branch, as the clone last fetched it. A run that starts meanwhile therefore uses the commit that clone already has; the next run after the other one exits fetches again.
  5. Otherwise it downloads the version and replaces the folder. Only then does the manifest drop the old entry. An npm module is installed without its own install scripts, such as prepare or postinstall, which serve its development: they are removed from the package.json of the cached folder. The install scripts of its dependencies run as the package manager decides.
  6. It lists the version in manifest.json, then releases the lock.

So ajs project modules update and ajs project build succeed while ajs project dev runs from the same cache, and a second run that starts while the first one installs a module waits, then uses what the first one installed. Every run that reads a cache folder, start from build.json included, also waits while another run holds the download lock of that version, so it never reads a folder another run is installing. A run fails only when another live run uses a folder that is incomplete, such as one an older core left half-written, when --frozen-modules finds that folder installed from another lockfile than its lock, or when that git clone has another commit checked out. A download also fails when the registry serves a tarball of another version than the one requested. The error names that run and its lease file. Stop that run, or wait for it to exit, then run the command again.

Each run saves manifest.json under .antelope/cache/.manifest.lock. It reads the file again, applies only its own changes, and renames a new file over it, so that the entries other runs add at the same time all stay. A save can list a version again after a prune moved its folder away. The next run that needs that version finds the folder missing and downloads it again.

Cache locks

The cache has three kinds of lock, each a folder with an owner.json:

  • .prune.lock. One process at a time removes folders. A prune skips while another holds it. A run that starts meanwhile waits for it, then downloads again a module whose folder it removed. start cannot download: it leases the folders its build.json names, then reads build.json again. When another build rewrote it meanwhile with other module folders in the same cache, start leases those folders and starts that build. A build.json that cannot be read at that moment leaves start with the build it read first. build writes build.json to a temporary file and renames it over the previous one, so a run that reads it meanwhile reads the whole previous build or the whole new one.
  • .manifest.lock. Each save of manifest.json holds it for the time of one read and one rename.
  • .downloads/<version>.lock. One run at a time downloads a module version.

A process takes a lock by writing its owner to a file beside the lock's folder, creating the folder, then moving that file into it as owner.json. A full disk therefore fails before any lock folder exists. While it holds the lock, it rewrites owner.json about every 10 seconds. A waiting run stops waiting in four cases:

  • The lock is gone.
  • The lock's owner ran on the same host and in the same pid namespace, and has exited.
  • The lock has no owner.json and its folder is more than 60 seconds old, as when a process stopped between creating the folder and writing its owner. The waiting run removes the lock at once and goes on.
  • owner.json has not changed for 60 seconds. The waiting run then removes the lock and goes on.

Each run counts the 60 seconds of silence on its own clock. It dates a folder without owner.json against a file it writes beside the folder and removes, so that both dates come from the clock of the cache's file system. Clocks that disagree between machines therefore do not matter. On a cache the run may not write, a folder without owner.json waits the 60 seconds of silence. A process takes over a lock only from an owner of the same host and pid namespace that has exited, or after those 60 seconds. owner.json holds a token for each hold of the lock. Before each folder it removes, before it renames manifest.json, and before each step of a download that writes a module folder (its replacement, each git or install command, the listing of its version), the holder checks that owner.json still holds its token. When another process has taken the lock over, it stops, and leaves that process's lock in place. A download that stops so fails, naming the module; run the command again.

Turning the prune off

Set ANTELOPEJS_NO_CACHE_PRUNE to 1 or true to keep every folder. Runs still write their leases, so a later prune keeps what they use.

terminal
ANTELOPEJS_NO_CACHE_PRUNE=1 ajs project build

Runs on other hosts and in containers

A run counts as live while its process exists. The core can only check a process of its own host and pid namespace. A run on another host, or in another pid namespace such as a container, is foreign, and its lease counts as live until that run deletes it.

While the lease of a foreign run is present, a prune removes nothing. This covers a cache that several hosts share over NFS, where a host may not yet see a lease or a lock that another host wrote. A run that crashed on another host, or a container run killed with SIGKILL, leaves its lease behind. Each prune then warns, naming every foreign lease, and removes nothing until those leases are gone. A download keeps a complete folder that such a lease names.

Remaining cases

These cases remain:

  • When another process takes the pid of a run that has exited, its lease counts until that process exits too. The folders it names are removed later, not lost.
  • A process that reads a cache folder outside a run holds no lease, such as a script that requires a cached module or the agent-skills interface resolver. A prune can remove the folder it reads.
  • A run that cannot write its lease runs without one and does not prune. On a cache it may not write, such as a start in a read-only image, it logs both at debug level. On any other error, such as a full disk, it logs both as warnings. A process of another user that may write the cache can remove the folders that run reads.
  • A run that cannot create a download lock, as on a read-only cache, loads the module without it.
  • A run that removes a lock after 60 seconds, silent or without owner.json, can race a process that takes the same lock at that instant. Both processes then hold it. The one whose token was replaced stops at its next check. A download checks before each of its steps, so a command that had started when the lock was taken over, such as an install, still runs to its end.
  • A process that cannot move its owner into the lock folder it created removes that folder, unless owner.json exists and holds another token, cannot be read, or does not parse as an owner. When a waiter removed that folder after 60 seconds and another process created it again meanwhile, without its owner yet, the folder of that other process is removed, and its take of the lock fails.