control-channel: the Awake test moves into Session, where the beat is
Round-2 QA finding: the channel reset beatInputEventCount on its own clock, out of phase with every connection's heartbeat instant, so a session could send Heartbeat (20) where Windows sends Awake (21) — and Awake is what drives the peer's Common.HumanBeingDetected() (Receiver.cs:245-248), the default-on BlockScreenSaver mechanism. Windows's beat is the send: SendAwakeBeat() compares lastRealInputEventCount against Event.RealInputEventCount and re-records it in the same call (Common.cs:542-559). So the comparison belongs at the send. Session.tick(now:userActive:) becomes Session.tick(now:inputEventCount:), taking the raw process-wide counter (Event.cs:51-55, InputHook.cs:223, 345) and keeping its own beatInputEventCount — Common.lastRealInputEventCount, Common.cs:543 — recorded on both branches (:558). ControlChannel loses lastBeatAt and beatInputEventCount entirely; noteUserActivity() only increments the counter and tick() hands the same value to every connection. No userActive-shaped name is left in Sources. Sources/MWBCore/Session.swift is session-matrix's file, edited for this fix with the lead's authorisation, as is that lane's Handshakes section. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
1 parent
23eddb42b9
commit
c03b8cdbcf
5 files changed
+158
-40
No files matched your search
@@ -6,7 +6,7 @@
|
||||
// Ported from App/Class/SocketStuff.cs MainTCPRoutine (1261-1565) and
|
||||
// TcpReceiveData (557-610), App/Core/Receiver.cs PreProcess (40-82),
|
||||
// App/Core/Common.cs SendPackage/SendHeartBeat (499-513), SendAwakeBeat
|
||||
// (545-558), SendByeBye (612-616) and TestSend (822-847),
|
||||
// (542-559), SendByeBye (612-616) and TestSend (822-847),
|
||||
// App/Form/frmScreen.cs the 100 ms timer (285-288, 555-575).
|
||||
|
||||
import Foundation
|
||||
@@ -124,6 +124,14 @@ public final class Session {
|
||||
private var nextHeartbeat: TimeInterval = .greatestFiniteMagnitude
|
||||
private var finished = false
|
||||
|
||||
/// The real-input counter as it stood when *this* session last beat —
|
||||
/// `Common.lastRealInputEventCount` (`Common.cs:543`), re-recorded on the way
|
||||
/// out of every beat (`:558`). It starts at zero because the Windows field
|
||||
/// does, so the first beat after the process has seen any input at all is an
|
||||
/// `Awake`; a spurious `Awake` only wakes a peer's screen, while a missed one
|
||||
/// lets it lock under the user's hands.
|
||||
private var beatInputEventCount: UInt64 = 0
|
||||
|
||||
/// The connection's status.
|
||||
public private(set) var status: SocketStatus = .na
|
||||
/// The peer's name, known once the handshake completes.
|
||||
@@ -212,13 +220,24 @@ public final class Session {
|
||||
pending.append(.status(new))
|
||||
}
|
||||
|
||||
/// Heartbeats. `userActive` is whether real input happened since the last
|
||||
/// tick, which is what turns a `Heartbeat` into an `Awake`
|
||||
/// (`Common.cs:545-552`).
|
||||
public func tick(now: TimeInterval, userActive: Bool) {
|
||||
/// Heartbeats. `inputEventCount` is the raw process-wide real-input counter,
|
||||
/// which is `Event.RealInputEventCount` on Windows (`Event.cs:51-55`, bumped
|
||||
/// by the hooks at `InputHook.cs:223, 345`); the owner passes the same value
|
||||
/// to every session on a tick and never interprets it.
|
||||
///
|
||||
/// The beat is `SendAwakeBeat` (`Common.cs:545-559`): `Awake` when
|
||||
/// `BlockScreenSaver` is on *and* the counter moved since this session's own
|
||||
/// previous beat, `Heartbeat` otherwise, and the counter is recorded either
|
||||
/// way. Keeping the comparison here rather than in the owner is what makes it
|
||||
/// a since-*this*-connection's-last-beat test — sessions beat on their own
|
||||
/// schedules, so a shared boundary in the owner would let a connection report
|
||||
/// `Heartbeat` for input that postdates its own previous beat.
|
||||
public func tick(now: TimeInterval, inputEventCount: UInt64) {
|
||||
guard status == .connected, !finished, now >= nextHeartbeat else { return }
|
||||
nextHeartbeat = now + heartbeatInterval
|
||||
let type: PackageType = config.blockScreenSaver && userActive ? .awake : .heartbeat
|
||||
let moved = inputEventCount != beatInputEventCount
|
||||
beatInputEventCount = inputEventCount // Common.cs:558
|
||||
let type: PackageType = config.blockScreenSaver && moved ? .awake : .heartbeat
|
||||
originate(broadcast(type))
|
||||
}
|
||||
|
||||
|
||||
@@ -57,8 +57,6 @@ public final class ControlChannel: @unchecked Sendable {
|
||||
private var blocked: Set<String> = []
|
||||
private var dialDeadline: TimeInterval = 0
|
||||
private var inputEventCount: UInt64 = 0
|
||||
private var beatInputEventCount: UInt64 = 0
|
||||
private var lastBeatAt: TimeInterval = 0
|
||||
private var controlledPeerID: UInt32?
|
||||
private var lastPathStatus: NWPath.Status?
|
||||
private var connectionCount = 0
|
||||
@@ -194,10 +192,13 @@ public final class ControlChannel: @unchecked Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
/// Tells the channel the user just did something, which is what turns the
|
||||
/// next `Heartbeat` into an `Awake` (`Common.cs:545-558`). This is our
|
||||
/// `Event.RealInputEventCount++` (`InputHook.cs:223, 345`): the count is
|
||||
/// what matters, not when it happened.
|
||||
/// Tells the channel the user just did something, which is what turns a
|
||||
/// connection's next `Heartbeat` into an `Awake` (`Common.cs:545-559`).
|
||||
///
|
||||
/// This is exactly our `Event.RealInputEventCount++` (`Event.cs:51-55`,
|
||||
/// `InputHook.cs:223, 345`) and nothing more: the channel only counts, and
|
||||
/// each `Session` decides `Awake` against the count it saw at its own
|
||||
/// previous beat.
|
||||
public func noteUserActivity() {
|
||||
queue.async { [self] in inputEventCount &+= 1 }
|
||||
}
|
||||
@@ -447,25 +448,21 @@ public final class ControlChannel: @unchecked Sendable {
|
||||
source.resume()
|
||||
}
|
||||
|
||||
/// `Awake` or `Heartbeat` is decided by whether the real-input counter moved
|
||||
/// since the *previous* heartbeat, not by a window around `now`:
|
||||
/// `SendAwakeBeat` sends `Awake` when `lastRealInputEventCount !=
|
||||
/// Event.RealInputEventCount` and records the counter again on its way out
|
||||
/// (`Common.cs:545-558`). The two counters here are that pair, and the beat
|
||||
/// they are recorded on is the one global heartbeat Windows's 100 ms timer
|
||||
/// runs for every socket at once (`frmScreen.cs:565-575`).
|
||||
/// The tick hands every connection the raw real-input counter and decides
|
||||
/// nothing about it. `SendAwakeBeat`'s `lastRealInputEventCount !=
|
||||
/// Event.RealInputEventCount` test is a comparison against the counter *as of
|
||||
/// the previous beat*, and the beat is the send (`Common.cs:545-559`,
|
||||
/// `frmScreen.cs:565-575`), so the comparison belongs where the send is
|
||||
/// scheduled: inside each `Session`, which owns its own heartbeat instant.
|
||||
/// A boundary kept here instead would be out of phase with every connection's
|
||||
/// own beat and could swallow the input a peer needs to stay awake.
|
||||
private func tick() {
|
||||
guard running else { return }
|
||||
let now = monotonicNow()
|
||||
let interval = config.blockScreenSaver ? Session.fastHeartbeat : Session.slowHeartbeat
|
||||
let userActive = inputEventCount != beatInputEventCount
|
||||
if now - lastBeatAt >= interval {
|
||||
lastBeatAt = now
|
||||
beatInputEventCount = inputEventCount
|
||||
}
|
||||
let count = inputEventCount
|
||||
for connection in connections {
|
||||
connection.isControlled = connection.peerID != nil && connection.peerID == controlledPeerID
|
||||
connection.tick(now: now, userActive: userActive)
|
||||
connection.tick(now: now, inputEventCount: count)
|
||||
}
|
||||
updateClients(now: now)
|
||||
}
|
||||
|
||||
@@ -315,10 +315,11 @@ final class PeerConnection: @unchecked Sendable {
|
||||
// MARK: - Time
|
||||
|
||||
/// Heartbeats, and the C2 rule that a controlled peer which has gone quiet
|
||||
/// for 30 s while we are feeding it input is gone.
|
||||
func tick(now: TimeInterval, userActive: Bool) {
|
||||
/// for 30 s while we are feeding it input is gone. `inputEventCount` is the
|
||||
/// channel's raw real-input counter, passed straight through to the session.
|
||||
func tick(now: TimeInterval, inputEventCount: UInt64) {
|
||||
guard !finished, closing == nil else { return }
|
||||
session.tick(now: now, userActive: userActive)
|
||||
session.tick(now: now, inputEventCount: inputEventCount)
|
||||
pump()
|
||||
|
||||
guard status == .connected, isControlled, lastForward > lastReceive else { return }
|
||||
|
||||
Reference in new issue
Block a user