diff --git a/internal/provider/codex_daemon.go b/internal/provider/codex_daemon.go index 45f469758..7d5b70504 100644 --- a/internal/provider/codex_daemon.go +++ b/internal/provider/codex_daemon.go @@ -362,7 +362,7 @@ var codexRestarting sync.Mutex // be found or timed, or a restart already under way, is left as it is. func KeepCodexDaemonCurrent(ctx context.Context, home string, changed time.Time) CodexDaemonCheck { var c CodexDaemonCheck - if changed.IsZero() { + if changed.IsZero() || !pidFileBehind(home, changed) { return c } ps, err := listProcesses(ctx) @@ -390,6 +390,20 @@ func KeepCodexDaemonCurrent(ctx context.Context, home string, changed time.Time) return c } +// pidFileBehind reports whether a pid file of home's was written before +// changed: only a daemon one names can be found behind (daemonStarted), so +// with none the processes aren't listed at all. Listing them is a +// PowerShell run on Windows, which every look did every codexDaemonEvery +// for as long as magpie ran (#2341). +func pidFileBehind(home string, changed time.Time) bool { + for _, pid := range daemonPIDs(home) { + if since, ok := daemonStarted(home, pid); ok && since.Before(changed.Add(-time.Second)) { + return true + } + } + return false +} + // daemonStarted is when the daemon pid started: the time Codex wrote its // pid file, which it does once, as the daemon comes up, on every OS. ok is // false when no pid file of home's names pid. diff --git a/internal/provider/codex_daemon_test.go b/internal/provider/codex_daemon_test.go index 3be3da8bc..511dac3c4 100644 --- a/internal/provider/codex_daemon_test.go +++ b/internal/provider/codex_daemon_test.go @@ -385,6 +385,37 @@ func TestKeepCodexDaemonCurrent(t *testing.T) { } } +// The processes are listed only when a pid file says a daemon started +// before the change: listing them is a PowerShell run on Windows, which +// the look every 15s made for as long as magpie ran (#2341). +func TestKeepCodexDaemonCurrentListsOnlyWhenBehind(t *testing.T) { + pid := fakeDaemon(t) + fakeCodex(t, 0) + home, _ := os.UserHomeDir() + home = filepath.Join(home, ".codex") + lists := 0 + listProcesses = func(context.Context) ([]proc.Process, error) { + lists++ + return nil, nil + } + ctx := context.Background() + *pid = 50 + // no pid file: no daemon to be behind + KeepCodexDaemonCurrent(ctx, home, time.Now()) + // one written after the change: it has the list + started := time.Now().Add(-time.Hour).Truncate(time.Second) + writeDaemonPID(t, home, "50", started) + KeepCodexDaemonCurrent(ctx, home, started.Add(-time.Minute)) + if lists != 0 { + t.Fatalf("processes listed %d times with no daemon behind", lists) + } + // one written before the change: listed, to see it runs + KeepCodexDaemonCurrent(ctx, home, time.Now()) + if lists != 1 { + t.Fatalf("processes listed %d times for a daemon behind, want 1", lists) + } +} + // A failed restart is said, not taken for one done. func TestKeepCodexDaemonCurrentFails(t *testing.T) { pid := fakeDaemon(t)