fix(daemon): make idle disconnect updates atomic - #98
Conversation
|
The race condition here only exists in the nanosecond-scale gap between two adjacent statements, so it is almost impossible to observe in real situation. But consolidating the two states that change together into a single invariant will avoid the same kind of bug again, so it is still worthwhile. Once the code comments are updated to match the code, it can be merged. |
After folding the IPC connection count and activity timestamp into a single mutex-protected IpcActivityState, the idle task no longer references the connection count directly; should_exit_for_idle already returns false while a connection is open or within the idle window. Update the comment to describe the post-refactor control flow so it matches the code.
9191f43 to
8352c3c
Compare
NianJiuZst
left a comment
There was a problem hiding this comment.
Thanks for the review. Per your note, the idle-task comment now matches the post-refactor control flow: should_exit_for_idle already enforces the zero-connection + idle-window invariant under the single IpcActivityState lock, so the remaining if block only holds the daemon alive while any browser is paired or any session is live (design §3.2, M4/M5 registries).
New head: 8352c3c (rebased onto current main@6b78211).
Verified locally after the rebase:
cargo test -p bsk --locked --lib -- ipc_close ipc_activity— both new unit tests pass.cargo clippy -p bsk --all-targets --locked -- -D warnings— clean.cargo test -p bsk --test idle_exit --locked -- --test-threads=1— the integration tests need an actual daemon process binding a UDS in/var/folders/...; on this machine that bind is rejected by the sandbox (Operation not permitted (os error 1)), so I could not re-run them locally. The PR did not touch those tests; they should still be green on your CI.
No logic change, only the comment refresh.
Summary
Why
The close callback previously decremented the atomic connection count before updating the separately locked activity timestamp. The idle task could observe the old expired timestamp between those operations, then see zero connections and shut the daemon down immediately after the last client disconnected instead of starting a fresh idle interval.
An idle check now observes a coherent snapshot: it sees either the still-open connection or the new close timestamp.
Tests
cargo test -p bsk --lockedcargo clippy -p bsk --all-targets --locked -- -D warningscargo test -p bsk --test idle_exit --locked -- --test-threads=1