JuliusBrussee/caveman · error

native runtime remove stale socket

Error message

native runtime remove stale socket: %w

What it means

When the socket path already exists, ServeUnix probes it: if a connect succeeds the socket is live (different error), otherwise the leftover file is stale and is removed. If os.Remove of that stale socket fails, the error is wrapped as "native runtime remove stale socket". This prevents orphaned socket files from blocking startup.

Solutions

  1. Manually delete the stale socket file: rm <socket path>
  2. Ensure the containing directory is writable by the current user
  3. Run the runtime as the same user that created the leftover socket

Example fix

// before (fails on stale socket owned by other user)
ServeUnix(ctx, "/home/olduser/.caveman/rt.sock", rt)
// after
sudo rm /home/olduser/.caveman/rt.sock
ServeUnix(ctx, "/home/newuser/.caveman/rt.sock", rt)
Defensive patterns

Strategy: validation

Validate before calling

if st, err := os.Stat(socketPath); err == nil {
	if err := os.Remove(socketPath); err != nil {
		return fmt.Errorf("cannot clear stale socket %s: %w", socketPath, err)
	}
}

Try / catch

if err := nativeruntime.ServeUnix(ctx, path, rt); err != nil {
	if strings.Contains(err.Error(), "remove stale socket") {
		os.Remove(path) // or log with elevated instructions
	}
	return err
}

Prevention

When it happens

Trigger: os.Remove(path) fails on a leftover socket file: it is owned by another user, the containing directory is not writable, or the path became immutable.

Common situations: Previous runtime crashed leaving a socket, and restart happens under a different user or with a read-only directory; manual files placed at the socket path.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/feb0b6aa1674c235. Report an issue: GitHub.

Appendix: source

Thrown at proxy/internal/nativeruntime/server_unix.go:49

// the coding agent or the provider proxy.
func ServeUnix(ctx context.Context, path string, runtime *Runtime) error {
	if runtime == nil || runtime.store == nil {
		return errors.New("native runtime: store is required")
	}
	if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
		return fmt.Errorf("native runtime mkdir: %w", err)
	}
	if err := os.Chmod(filepath.Dir(path), 0o700); err != nil {
		return fmt.Errorf("native runtime chmod dir: %w", err)
	}
	if _, err := os.Stat(path); err == nil {
		conn, dialErr := net.DialTimeout("unix", path, 50*time.Millisecond)
		if dialErr == nil {
			_ = conn.Close()
			return errors.New("native runtime: socket already active")
		}
		if err := os.Remove(path); err != nil {
			return fmt.Errorf("native runtime remove stale socket: %w", err)
		}
	} else if !os.IsNotExist(err) {
		return fmt.Errorf("native runtime inspect socket: %w", err)
	}
	listener, err := net.Listen("unix", path)
	if err != nil {
		return fmt.Errorf("native runtime listen: %w", err)
	}
	defer listener.Close()
	defer os.Remove(path)
	if err := os.Chmod(path, 0o600); err != nil {
		return fmt.Errorf("native runtime chmod socket: %w", err)
	}
	go func() {
		<-ctx.Done()
		_ = listener.Close()
	}()
	for {

View on GitHub (pinned to 3ee70a1026)