Start a watch
- axectl
- gRPC
CREATE src/main.rs. Errors print to stderr as axectl: watch: <message>. An error that ends the watch makes axectl exit with 1; other errors are reported and watching continues. Ctrl-C stops the watch.NOT_FOUND, a file with INVALID_ARGUMENT, and a path outside the root with PERMISSION_DENIED.
The Agent Sandbox equivalent is
watcher.v1.FileWatcherService/WatchDir with path and recursive. It sends FileEvents with a type of CREATE, WRITE, REMOVE, RENAME, CHMOD or ERROR. It has no started event, and its errors do not say whether they end the watch.Event kinds
Event paths are relative to the sandbox root, not to the watched directory.
A session in a watched directory looks like this:
CREATE of its path, even when it replaced an existing file.
Events arrive in order, in small batches: the native API gathers events for 50 ms and the Agent Sandbox API for 250 ms after the first one. A batch holds about 4,096 events at most. During a flood axed stops reading until the batch is sent, so the backlog waits in the kernel’s queue, and if that fills you get the overflow error below.
Recursive watches
Withrecursive, axed watches the directory and every subdirectory below it. It does not follow symlinks. When a directory is created inside the tree, axed starts watching it; when one is removed or moved away, axed stops. A new directory is watched before its event is reported, so changes made in it afterwards are seen. Its existing contents, such as a tree moved in, are covered in the background.
A file created inside a new directory in the instant before axed starts watching that directory is not reported. If you need every file, list a new directory when you see its CREATE.
If some subdirectories cannot be watched, usually because the kernel’s inotify watch limit (fs.inotify.max_user_watches) is reached, or because their path from the sandbox root is longer than 4,096 bytes (PATH_MAX), you get one non-terminal error that counts them and names the first few. axectl watch prints it as:
Overflow and rescan
The kernel queues events for each watch. If a burst fills the queue, the kernel drops events and you get a non-terminal error:Files.List) and compare it with what you knew.
When a watch ends
An error event’sterminal field says whether it ends the watch:
- Terminal. The watched directory itself was removed or renamed. This is the last event.
- Not terminal. Queue overflow and incomplete recursive coverage. The watch continues.
INTERNAL status.
The shared watch limit
axed allows at most 64 concurrent watches, shared by the native API and the Agent Sandbox API. A watch past the limit fails withRESOURCE_EXHAUSTED:
RESOURCE_EXHAUSTED rather than NOT_FOUND. A refused watch never creates an inotify instance.
Each watch holds its own inotify instance, so a burst in one client’s tree cannot overflow another watch’s queue. The kernel limits how many inotify instances one user may hold (fs.inotify.max_user_instances), and that limit is shared with every other program in the sandbox running as the same user. The cap of 64 bounds how much of that limit axed can take.
Close watches you no longer need. Watching needs inotify; on a kernel without it, watch calls fail and everything else works.
