session-matrix: PacketSequencer is thread-safe (app-engine request 1)
Cross-lane edit, authorised by the lead. Contracts C1 gives the process one sequencer and two callers: the Engine actor stamps application packets, each `Session` stamps its own handshake, heartbeat, `Hi` and `ByeBye` on the channel's queue (`Session.swift:402-403`). `value &+ 1` with no synchronisation can lose an update and hand two packets the same `Id`, which the peer's replay ring drops (`Receiver.cs:40-82`). The counter now lives under an `NSLock` — Foundation only, so MWBCore's layering holds — and the class is `@unchecked Sendable`, which is the guarantee Windows gets from `Interlocked.Increment` (`Common.cs:916`). `ReplayFilter` is unchanged: one queue touches it. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
1 parent
02df633a01
commit
8125cb1ede
2 files changed
+30
-8
No files matched your search
@@ -11,10 +11,19 @@ import Foundation
|
||||
///
|
||||
/// Windows keeps a single `Package.PackageID` (`Package.cs:22`) incremented by
|
||||
/// `Interlocked.Increment` for every broadcast (`Common.cs:916`) and persists it
|
||||
/// as the `PackageID` setting, so ids keep rising across restarts. Every
|
||||
/// `Session` in the app shares one instance; it is not `Sendable`, so the app
|
||||
/// owns it on the one queue that owns the sessions.
|
||||
public final class PacketSequencer {
|
||||
/// as the `PackageID` setting, so ids keep rising across restarts.
|
||||
///
|
||||
/// **Thread-safe, and it has to be.** Contracts C1 gives the process one
|
||||
/// sequencer and two callers: the app stamps every application packet on the
|
||||
/// Engine actor, while each `Session` stamps its own handshake, heartbeat, `Hi`
|
||||
/// and `ByeBye` on the channel's queue (`Session.swift:402-403`). An
|
||||
/// unsynchronised `value &+ 1` can lose an update there and hand two packets the
|
||||
/// same `Id`, which the peer's replay ring reads as a duplicate and drops
|
||||
/// (`Receiver.cs:40-82`). The lock is held for one add, so it is never
|
||||
/// contended for long. Windows gets the same guarantee from
|
||||
/// `Interlocked.Increment` (`Common.cs:916`).
|
||||
public final class PacketSequencer: @unchecked Sendable {
|
||||
private let lock = NSLock()
|
||||
private var value: Int32
|
||||
|
||||
/// A sequencer resuming from the persisted `PackageID` setting.
|
||||
@@ -25,12 +34,18 @@ public final class PacketSequencer {
|
||||
/// The next id: pre-incremented like `Interlocked.Increment`
|
||||
/// (`Common.cs:916`), wrapping on overflow as C#'s unchecked `int` does.
|
||||
public func next() -> Int32 {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
value = value &+ 1
|
||||
return value
|
||||
}
|
||||
|
||||
/// The last id handed out, for the app to persist as `PackageID`.
|
||||
public var current: Int32 { value }
|
||||
public var current: Int32 {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
/// The replay window: the last 50 packet ids, in a ring, with the four
|
||||
|
||||
Reference in new issue
Block a user