From 6ffb70cb39dd1fb075635095521f95c716ef6645 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 13 Jul 2026 04:25:03 +0100 Subject: [PATCH] Replay CultMesh subscriptions per physical session --- src/GameCult.Mesh/CultMeshClient.cs | 10 ++++++++-- src/GameCult.Mesh/CultMeshSessions.cs | 8 +++++++- src/GameCult.Mesh/docs/reliability-migration-plan.md | 11 +++++++---- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/GameCult.Mesh/CultMeshClient.cs b/src/GameCult.Mesh/CultMeshClient.cs index ddc55a38..ed712521 100644 --- a/src/GameCult.Mesh/CultMeshClient.cs +++ b/src/GameCult.Mesh/CultMeshClient.cs @@ -303,6 +303,7 @@ namespace GameCult.Mesh private readonly TaskCompletionSource _ready = new(TaskCreationOptions.RunContinuationsAsynchronously); private readonly object _subscribeLock = new(); private Task? _subscribeTask; + private long _subscribedGeneration = -1; private IDisposable? _stateWatch; private bool _disposed; @@ -337,7 +338,9 @@ namespace GameCult.Mesh { lock (_subscribeLock) { - if (_subscribeTask is { IsCompleted: false }) return _subscribeTask; + var generation = _session.PhysicalGeneration; + if (_subscribedGeneration == generation && _subscribeTask != null) return _subscribeTask; + _subscribedGeneration = generation; return _subscribeTask = SubscribeAsync(); } } @@ -380,6 +383,7 @@ namespace GameCult.Mesh private readonly TaskCompletionSource _ready = new(TaskCreationOptions.RunContinuationsAsynchronously); private readonly object _subscribeLock = new(); private Task? _subscribeTask; + private long _subscribedGeneration = -1; private IDisposable? _stateWatch; private bool _disposed; @@ -412,7 +416,9 @@ namespace GameCult.Mesh { lock (_subscribeLock) { - if (_subscribeTask is { IsCompleted: false }) return _subscribeTask; + var generation = _session.PhysicalGeneration; + if (_subscribedGeneration == generation && _subscribeTask != null) return _subscribeTask; + _subscribedGeneration = generation; return _subscribeTask = SubscribeAsync(); } } diff --git a/src/GameCult.Mesh/CultMeshSessions.cs b/src/GameCult.Mesh/CultMeshSessions.cs index 806f0405..e654fddc 100644 --- a/src/GameCult.Mesh/CultMeshSessions.cs +++ b/src/GameCult.Mesh/CultMeshSessions.cs @@ -163,6 +163,7 @@ namespace GameCult.Mesh private readonly Subject _states = new(); private readonly ConcurrentBag _subscriptions = new(); private readonly ManagedSchemaChannel _channel; + private long _physicalGeneration; private bool _disposed; internal CultMeshSession(CultMeshEndpointId endpointId, CultMeshProtocolId protocol, ICultNetSchemaClient channel, CultMeshSessionState state) { @@ -175,6 +176,7 @@ namespace GameCult.Mesh public CultMeshProtocolId Protocol { get; } internal ICultNetSchemaClient Channel => _channel; public CultMeshSessionState State { get; private set; } + internal long PhysicalGeneration => Interlocked.Read(ref _physicalGeneration); public Observable WatchState() => _states; public ICultNetSchemaClient OpenSchemaClient() => new SessionSchemaClient(this); public void SendCultNet(T message) where T : ICultNetSchemaMessage => Channel.SendCultNet(message); @@ -187,7 +189,11 @@ namespace GameCult.Mesh return subscription; } internal void Transition(CultMeshSessionState state) { State = state; _states.OnNext(state); } - internal void ReplacePhysicalChannel(ICultNetSchemaClient channel) => _channel.Replace(channel); + internal void ReplacePhysicalChannel(ICultNetSchemaClient channel) + { + _channel.Replace(channel); + Interlocked.Increment(ref _physicalGeneration); + } public void Dispose() { if (_disposed) return; diff --git a/src/GameCult.Mesh/docs/reliability-migration-plan.md b/src/GameCult.Mesh/docs/reliability-migration-plan.md index 83bc49df..d6e6c9cd 100644 --- a/src/GameCult.Mesh/docs/reliability-migration-plan.md +++ b/src/GameCult.Mesh/docs/reliability-migration-plan.md @@ -424,9 +424,12 @@ and client disposal. online physical incarnation, cache-backed typed handles, and readiness after the first successful snapshot from any incarnation. -**Derived state:** physical client identity, attempt count, cached snapshots, -and online/reconnecting/offline projections. No individual request owns the -logical handle. +**Derived state:** physical client identity and generation, attempt count, +cached snapshots, and online/reconnecting/offline projections. The session +increments the generation only after replacing its physical channel. A binding +may coalesce duplicate subscription attempts within that generation, but a +pending request from an older generation cannot suppress replay. No individual +request owns the logical handle. **Forbidden writers:** bindings and schema clients cannot choose endpoints, replace channels, reconnect, or declare session state. An abandoned initial @@ -435,7 +438,7 @@ readiness. **Shared paths:** initial open and reconnect both use session-manager path resolution. Document and collection activation both replay the same binding -intent when the logical session returns online. +intent once per physical generation when the logical session returns online. **Cut line:** the initial subscribe no longer runs before reconnect observation, and a per-binding semaphore no longer lets an unanswered physical request hold