Files
w3cjandClaude Opus 5.5 2de3e31a6c fix(vm): devvm ports shows a status line as it restarts a running VM's forwarder, and every lock's wait is tested for its label
Lock waits:
- Tests record the label of each lock's wait that had none, the lock held
  as by another devvm and the wait ended by a cancel, a deadline failing
  one that shows nothing rather than hanging: git_lock_waiting
  (TestGitLockWaits; .git protection's Serial is gitSerial, so a test can
  reach it), packages_waiting and rebuilds_waiting (TestImageLocksWait),
  ubuntu_pin_waiting (TestPinOriginWaits), custom_secrets_waiting
  (TestYoursLockWaits) and forwarder_waiting (TestForwardsLockWaits).
- A test build's locks panic on a nil platform.Waiting
  (platform.CheckWaiting, which the root's run calls in every test build
  beside state.CheckLockOrder), so the CLI tests and e2e catch a nil wait
  the structure test can't see, such as a zero agentlogin.File's; a
  msg.Waiting given a nil status shows nothing and stays review only.
  msg.Waiting with no status gives a wait that shows nothing, never nil;
  platform.NoWait is the Waiting of a lock that never waits (the build
  port's record, which passed nil), and the status-lines rule holds it to
  statusSites as it does nil (TestCheckWaiting, TestWaiting, the rule's
  NoWait case).
- forbidigo refuses syscall.Flock and unix.Flock outside internal/platform,
  internal/proxy/rotate.go, the stubs and tests (testdata/lint, both).
- TestLockFileWaiting runs in a synctest bubble and checks the wait shows
  while held, not before (its counters under a mutex, which the race
  detector needs, as only the file's lock orders them).

Status lines:
- TestCommandStatusLines runs the setters too: devvm ports LIST and ports
  add, size --cpus, network open, allowlist, transparent off and on, learn
  and allow, git-protect off and on, ssh-agent off. It found devvm ports on
  a running VM starting its forwarder again (ssh and a command in the VM)
  with no status line; that step now shows ports_restarting.

The folder named twice:
- vm_folder_unreadable's why was the open's error, the folder's path in
  it; folderUnreadable gives the system's reason alone when the error is
  about the folder itself (TestUnreadableFolder checks the whole message).
- platform.OpenNoLinks and StatFolder return a link on the path as an
  fs.PathError about the folder too (ErrLinkOnPath still matches), so that
  case names it once as well (TestStatFolderUnique).

Docs: Contributing's CLI page (status lines) and structure page, the
codebase-standards guards; CHANGELOG (devvm ports' status line).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-19 17:04:29 -07:00

172 lines
5.0 KiB
Go

//go:build unix
package platform
import (
"context"
"errors"
"os"
"syscall"
"time"
)
// lockTick is how often LockFile tries again for a lock another process
// holds.
const lockTick = 50 * time.Millisecond
// LockFile holds an exclusive lock on path (made if missing, mode 600)
// until unlock is called or the process ends, waiting for another process
// that holds it until ctx ends, while waiting (Waiting) says so. Its
// descriptor isn't inherited (close on exec), so a process started
// meanwhile doesn't keep it. The holder may remove the file before it lets
// go: one that got the lock on the removed file tries again on path.
func LockFile(ctx context.Context, path string, waiting Waiting) (unlock func(), err error) {
return lockFile(ctx, path, syscall.LOCK_EX, waiting)
}
// LockFileShared holds a shared lock on path, as LockFile does: any
// number of processes hold it at once, and it waits only while one holds
// it exclusively (LockFile, TryLockFile).
func LockFileShared(ctx context.Context, path string, waiting Waiting) (unlock func(), err error) {
return lockFile(ctx, path, syscall.LOCK_SH, waiting)
}
// TryLockFile is LockFile without waiting: ok is false while another
// holds the lock, shared or exclusive.
func TryLockFile(path string) (unlock func(), ok bool, err error) {
for {
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0o600) //nolint:gosec // devvm's own lock file
if err != nil {
return nil, false, err
}
got, err := flock(f, syscall.LOCK_EX)
switch {
case err != nil:
_ = f.Close()
return nil, false, err
case !got:
_ = f.Close()
return nil, false, nil
case !current(f, path):
_ = f.Close()
continue
}
return func() { _ = f.Close() }, true, nil
}
}
// LockHeld reports whether another process holds path's lock
// exclusively; false when there's no such file.
func LockHeld(path string) (bool, error) {
f, err := os.OpenFile(path, os.O_RDWR, 0) //nolint:gosec // devvm's own lock file
if errors.Is(err, os.ErrNotExist) {
return false, nil
} else if err != nil {
return false, err
}
defer func() { _ = f.Close() }()
got, err := flock(f, syscall.LOCK_SH)
return !got, err
}
// lockFile takes the lock how says (LOCK_EX or LOCK_SH) on path, trying
// again lockTick apart until ctx ends; waiting, if any, is called once it
// finds the lock held, and what it returned once the wait ends.
func lockFile(ctx context.Context, path string, how int, waiting Waiting) (unlock func(), err error) {
checkWaiting(waiting)
var end func()
defer func() {
if end != nil {
end()
}
}()
for {
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0o600) //nolint:gosec // devvm's own lock file
if err != nil {
return nil, err
}
if err := waitLock(ctx, f, how, &waiting, &end); err != nil {
_ = f.Close()
return nil, err
}
if current(f, path) {
return func() { _ = f.Close() }, nil
}
_ = f.Close()
}
}
// LockDir holds an exclusive lock on the folder dir itself, as LockFile
// holds one on a file, until unlock is called or the process ends,
// waiting for another process that holds it until ctx ends, while waiting
// (Waiting) says so. It never makes the folder: one that isn't there is
// os.ErrNotExist. Its descriptor isn't inherited (close on exec).
func LockDir(ctx context.Context, dir string, waiting Waiting) (unlock func(), err error) {
checkWaiting(waiting)
var end func()
defer func() {
if end != nil {
end()
}
}()
f, err := os.Open(dir) //nolint:gosec // a folder, locked, never read
if err != nil {
return nil, err
}
if err := waitLock(ctx, f, syscall.LOCK_EX, &waiting, &end); err != nil {
_ = f.Close()
return nil, err
}
return func() { _ = f.Close() }, nil
}
// waitLock takes the lock how says on f, trying again lockTick apart
// until ctx ends. The first time it finds the lock held, it calls
// *waiting, if any, sets it nil and keeps what it returned in *end, for
// its caller to call once its wait is over.
func waitLock(ctx context.Context, f *os.File, how int, waiting *Waiting, end *func()) error {
for {
got, err := flock(f, how)
if err != nil || got {
return err
}
if *waiting != nil {
*end = (*waiting)()
*waiting = nil
}
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(lockTick):
}
}
}
// flock tries the lock how says on f once, without waiting: got is false
// while another holds it.
func flock(f *os.File, how int) (got bool, err error) {
for {
err := syscall.Flock(int(f.Fd()), how|syscall.LOCK_NB)
switch {
case err == nil:
return true, nil
case errors.Is(err, syscall.EINTR):
continue
case errors.Is(err, syscall.EWOULDBLOCK):
return false, nil
}
return false, err
}
}
// current reports whether f is still the file at path: the last holder
// may have removed it, or put another there.
func current(f *os.File, path string) bool {
a, err := f.Stat()
if err != nil {
return false
}
b, err := os.Stat(path) //nolint:gosec // devvm's own lock file, which f has open
return err == nil && os.SameFile(a, b)
}