fix(M7): review fixes — pidfile O_EXCL, publish race, backoff reset 🐛
Code-review pass on M6.5+M7: - pidfile: claim via O_CREATE|O_EXCL and arbitrate live-vs-stale, so two `devbox start` invocations can't both win the guard and run concurrently - hub: serialize publish (publishMu) so a case-clash check + create is atomic; reject share names ending in '.' (Windows strips it → silent collision) - daemon: reset SSE backoff only after a genuinely healthy stream (2m), not the 30s ceiling, so a hub that recycles connections doesn't pin a 30s event lag - document that restore/deploy's delete pass is ignore/guard-filtered on purpose (never delete a user's gitignored runtime files or local secrets) - test: pidfile claim/refuse/stale-reclaim arbitration
This commit is contained in:
parent
6e3ddee1e6
commit
5bfccdb297
8 changed files with 98 additions and 12 deletions
|
|
@ -475,9 +475,6 @@ func startCmd() *cobra.Command {
|
|||
if len(cfg.Mounts) == 0 {
|
||||
return fmt.Errorf("no mounts — run: devbox mount <share> <dir>")
|
||||
}
|
||||
if pid, ok := runningPid(dir); ok {
|
||||
return fmt.Errorf("daemon already running (pid %d) — run: devbox stop", pid)
|
||||
}
|
||||
if err := writePid(dir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -577,11 +574,34 @@ func stopCmd() *cobra.Command {
|
|||
|
||||
func pidPath(dir string) string { return filepath.Join(dir, "daemon.pid") }
|
||||
|
||||
// writePid atomically claims the pidfile via O_EXCL so two `devbox start`
|
||||
// invocations can't both think they're the only daemon. If the file already
|
||||
// exists it arbitrates: a live pid means refuse; a stale pid (crashed daemon)
|
||||
// is removed and the claim retried.
|
||||
func writePid(dir string) error {
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(pidPath(dir), []byte(strconv.Itoa(os.Getpid())), 0o600)
|
||||
for {
|
||||
f, err := os.OpenFile(pidPath(dir), os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)
|
||||
if err == nil {
|
||||
_, werr := f.WriteString(strconv.Itoa(os.Getpid()))
|
||||
if cerr := f.Close(); werr == nil {
|
||||
werr = cerr
|
||||
}
|
||||
return werr
|
||||
}
|
||||
if !os.IsExist(err) {
|
||||
return err
|
||||
}
|
||||
if pid, ok := runningPid(dir); ok {
|
||||
return fmt.Errorf("daemon already running (pid %d) — run: devbox stop", pid)
|
||||
}
|
||||
if rerr := os.Remove(pidPath(dir)); rerr != nil && !os.IsNotExist(rerr) {
|
||||
return rerr
|
||||
}
|
||||
// Stale pidfile removed; loop retries the exclusive create.
|
||||
}
|
||||
}
|
||||
|
||||
func removePid(dir string) { _ = os.Remove(pidPath(dir)) }
|
||||
|
|
|
|||
48
cmd/devbox/main_test.go
Normal file
48
cmd/devbox/main_test.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestWritePidArbitration covers the O_EXCL claim, the live-daemon refusal, and
|
||||
// stale-pidfile recovery.
|
||||
func TestWritePidArbitration(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
|
||||
// First claim succeeds and records our (live) pid.
|
||||
if err := writePid(dir); err != nil {
|
||||
t.Fatalf("first writePid: %v", err)
|
||||
}
|
||||
if pid, ok := runningPid(dir); !ok || pid != os.Getpid() {
|
||||
t.Fatalf("runningPid = (%d,%v), want (%d,true)", pid, ok, os.Getpid())
|
||||
}
|
||||
|
||||
// A second claim must refuse: the pid in the file (us) is alive.
|
||||
if err := writePid(dir); err == nil || !strings.Contains(err.Error(), "already running") {
|
||||
t.Fatalf("second writePid = %v, want 'already running'", err)
|
||||
}
|
||||
|
||||
// A stale pidfile (a pid that isn't running) is reclaimed, not refused.
|
||||
dead := 0x7fffffff // implausibly-high pid; Signal(0) returns ESRCH
|
||||
if err := os.WriteFile(pidPath(dir), []byte(strconv.Itoa(dead)), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := runningPid(dir); ok {
|
||||
t.Fatal("runningPid reported a dead pid as alive")
|
||||
}
|
||||
if err := writePid(dir); err != nil {
|
||||
t.Fatalf("reclaiming a stale pidfile: %v", err)
|
||||
}
|
||||
if pid, _ := runningPid(dir); pid != os.Getpid() {
|
||||
t.Fatalf("after reclaim pid = %d, want %d", pid, os.Getpid())
|
||||
}
|
||||
|
||||
// removePid clears it.
|
||||
removePid(dir)
|
||||
if _, ok := runningPid(dir); ok {
|
||||
t.Fatal("pidfile still live after removePid")
|
||||
}
|
||||
}
|
||||
|
|
@ -5,10 +5,14 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
// Reconnect backoff bounds: start at 1s, double to a 30s ceiling.
|
||||
// Reconnect backoff bounds: start at 1s, double to a 30s ceiling. A stream that
|
||||
// stayed up at least healthyStream is treated as a transient drop (reset to base),
|
||||
// not a hard failure — so a hub that recycles SSE connections every ~30s doesn't
|
||||
// leave the daemon stuck at the ceiling with a 30s event lag.
|
||||
const (
|
||||
backoffBase = 1 * time.Second
|
||||
backoffMax = 30 * time.Second
|
||||
healthyStream = 2 * time.Minute
|
||||
)
|
||||
|
||||
// nextBackoff returns the next clean (un-jittered) backoff: base, then doubling
|
||||
|
|
|
|||
|
|
@ -138,7 +138,7 @@ func (d *Daemon) runMount(ctx context.Context, m config.Mount) {
|
|||
}
|
||||
// A stream that lived a while is a transient drop, not a hard failure:
|
||||
// reset so the retry is prompt instead of inheriting a grown delay.
|
||||
if time.Since(start) > backoffMax {
|
||||
if time.Since(start) > healthyStream {
|
||||
backoff = 0
|
||||
}
|
||||
backoff = nextBackoff(backoff)
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import (
|
|||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.shoemoney.ai/shoemoney/devbox/internal/chunk"
|
||||
|
|
@ -28,6 +29,7 @@ type Server struct {
|
|||
db *meta.DB
|
||||
store blobstore.Store
|
||||
broker *broker
|
||||
publishMu sync.Mutex // serializes publish so the case-clash check + create is atomic
|
||||
}
|
||||
|
||||
// NewServer builds a hub server over the given metadata store and blob store.
|
||||
|
|
@ -127,6 +129,10 @@ func (s *Server) handlePublish(w http.ResponseWriter, r *http.Request, deviceID
|
|||
if !decode(w, r, &req) {
|
||||
return
|
||||
}
|
||||
// Serialize: the case-clash check below reads all names then creates one, so
|
||||
// two concurrent publishes of "Foo"/"foo" must not both pass the check.
|
||||
s.publishMu.Lock()
|
||||
defer s.publishMu.Unlock()
|
||||
existing, err := s.db.ShareNames()
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||
|
|
|
|||
|
|
@ -32,6 +32,8 @@ func ValidateShareName(name string, existing []string) error {
|
|||
return fmt.Errorf("share name %q is reserved", name)
|
||||
case strings.HasPrefix(name, ".") || strings.HasPrefix(name, "-"):
|
||||
return fmt.Errorf("share name %q must not start with '.' or '-'", name)
|
||||
case strings.HasSuffix(name, "."):
|
||||
return fmt.Errorf("share name %q must not end with '.' (Windows strips it)", name)
|
||||
}
|
||||
for _, r := range name {
|
||||
switch {
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ func TestValidateShareName(t *testing.T) {
|
|||
{"a\\b", false}, // backslash
|
||||
{"..", false}, // traversal
|
||||
{".hidden", false}, // leading dot
|
||||
{"trail.", false}, // trailing dot (Windows strips it)
|
||||
{"-flag", false}, // leading dash
|
||||
{"sp ace", false}, // interior space
|
||||
{"emoji🎉", false}, // non-ASCII
|
||||
|
|
|
|||
|
|
@ -210,7 +210,12 @@ func applySnapshot(c *transport.Client, root, subpath, snapshot, onlyPath string
|
|||
return 0, err
|
||||
}
|
||||
}
|
||||
// Delete anything present locally but absent from the snapshot.
|
||||
// Delete tracked files present locally but absent from the snapshot. We diff
|
||||
// against manifest.Build (ignore- + secret-guard-filtered) ON PURPOSE: a
|
||||
// restore/deploy must NOT delete a user's .devignore'd runtime files (caches,
|
||||
// logs) or local secrets — those were never in any snapshot, so "match the
|
||||
// snapshot" leaves them untouched. Tracked extras (e.g. a file added in a newer
|
||||
// snapshot) are still removed. ponytail: never lose a byte we didn't sync.
|
||||
local, _, err := manifest.Build(root, ig, guard)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
|
|
|||
Loading…
Reference in a new issue