Skip to content

A keep-awake session bound to a pid outlives that process #52

Description

@ostapondo

plonk awake on --pid 4321 and plonk awake while <command> hold the Mac awake for exactly as long as a process lives. The check is AwakeManager.isRunning:

private static func isRunning(_ pid: Int) -> Bool {
    errno = 0
    return kill(pid_t(pid), 0) == 0 || errno == EPERM
}

That asks whether something with that number is alive, not whether it is still the process the session was bound to. The poll runs every 5 seconds. If the build finishes and macOS hands the number to something else before the next tick, the poll keeps answering yes and the assertion is never dropped. The Mac stays awake on a session nobody remembers starting, and the menu keeps saying "on until process 4321 exits" about a process that is long gone.

pids wrap around 99999, so this needs a busy machine and a long session. Rare, not impossible, and the failure mode is a laptop that stops sleeping until someone notices.

What to do

  • Read the process start time when the session is bound: sysctl with KERN_PROC_PID into a kinfo_proc, then kp_proc.p_starttime.
  • Store it alongside boundPID and compare it on every poll. A different start time means a different process, so end the session.
  • Keep the decision out of the syscall, something like stillBound(pid:startedAt:now:) taking the value already looked up, so it is a plain unit test rather than something that has to spawn a process.

Done when

swift build and ./scripts/test.sh pass, a pid-bound session still ends when its process exits (check by hand with plonk awake while sleep 5), and there is a test for "same pid, different start time, session ends".

Starting from cold

The loop this needs runs on a plain checkout, with no signing certificate and
no Xcode:

git clone https://github.com/ostapondo/plonk && cd plonk
(cd App && swift build) && ./scripts/test.sh

Comment here to claim it, so nobody writes it twice. If it would be your first
pull request anywhere, say so — you get a slower review, not a worse one.
CONTRIBUTING.md
has the commit and pull request conventions, and
AGENTS.md is the
engineering guide underneath them: the repo layout, the five places a new
module touches, and the mistakes that have already cost someone an hour.

Or point an agent at it

AGENTS.md is written for one, so this repo is unusually easy to hand over:

Read AGENTS.md in this repo first. Then do the issue: make a pid-bound keep-awake session in App/Sources/plonk/AwakeManager.swift end when the process it was bound to ends, rather than when the pid stops resolving. Record the process start time at bind time and compare it on every poll. Keep the syscall and the decision apart, so the decision is a pure function covered in App/Tests/plonkTests/. Change nothing else about how sessions start or end. Verify with swift build in App/ and ./scripts/test.sh from the root, and check by hand that plonk awake while sleep 5 still releases, saying so in the pull request.

Read what it produces before you send it. The pull request is yours, and the
review will be with you.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions