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
|
||||
|
||||
@@ -128,7 +128,7 @@ does I/O, reads a clock or logs.
|
||||
|
||||
```swift
|
||||
// Sequencing.swift — the two process-wide counters (§1.4)
|
||||
public final class PacketSequencer { // not Sendable: app-wide, one queue
|
||||
public final class PacketSequencer: @unchecked Sendable { // thread-safe: NSLock
|
||||
public init(start: Int32 = 0) // resumes the PackageID setting
|
||||
public func next() -> Int32 // pre-increment, wrapping
|
||||
public var current: Int32 { get } // persist this back
|
||||
@@ -316,8 +316,15 @@ Rules the consuming lanes must honour:
|
||||
and after the replay ring (CLAUDE.md invariant 2). `.close` is always the
|
||||
last event a session emits; close the socket and drop the session.
|
||||
- **One `PacketSequencer` and one `ReplayFilter` per process**, shared by
|
||||
every `Session`, as on Windows. Neither is `Sendable`; nor are `Session`
|
||||
and `Switcher`. Everything in this lane lives on one queue.
|
||||
every `Session`, as on Windows. `PacketSequencer` is **thread-safe and
|
||||
`Sendable`** — it holds its counter under an `NSLock` — because contracts
|
||||
C1 gives it two callers by design: the Engine actor stamps application
|
||||
packets while a `Session` stamps its own handshake, heartbeat, `Hi` and
|
||||
`ByeBye` on the channel's queue, and a lost update there would hand two
|
||||
packets the same `Id` (amended 2026-09-16 at the app-engine lane's
|
||||
request, with the lead's authorisation). `ReplayFilter` is **not**
|
||||
`Sendable`; nor are `Session` and `Switcher` — those are touched on one
|
||||
queue only.
|
||||
- **`Switcher` never does I/O.** It returns `[SwitchAction]`; the app
|
||||
performs them in order. A `.send` packet is already sequenced and
|
||||
addressed, so it goes to every connection through `Session.forward(_:)`
|
||||
|
||||
Reference in new issue
Block a user