Skip to content

Commit e36992f

Browse files
fix(gateway): isolate the SessionsUpdated snapshot from live tracked state
GetSessionListInternal copies the _sessions array but its elements are the same SessionInfo instances the map holds, which UpdateTrackedSession mutates in place under _sessionsLock. SessionsUpdated subscribers read the snapshot with no lock, so they can observe CurrentActivity/LastSeen/Status changing underneath them — a data race on shared mutable state. Hand out a defensive copy (SessionInfo.Clone via MemberwiseClone) so a delivered snapshot is isolated from later in-place mutation. Adds SessionSnapshotIsolationTests (red before the fix: a captured snapshot's CurrentActivity is overwritten by a later update for the same key; green after).
1 parent 9fb1960 commit e36992f

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

‎src/OpenClaw.Shared/Models.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ private static bool TryGetBool(JsonElement parent, string property) =>
253253

254254
public class SessionInfo
255255
{
256+
/// <summary>Defensive copy so a snapshot handed to a SessionsUpdated subscriber does not
257+
/// alias the live tracked instance (whose fields are mutated in place under _sessionsLock).</summary>
258+
public SessionInfo Clone() => (SessionInfo)MemberwiseClone();
259+
256260
public string Key { get; set; } = "";
257261
public bool IsMain { get; set; }
258262
public string Status { get; set; } = "unknown";

‎src/OpenClaw.Shared/OpenClawGatewayClient.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3397,6 +3397,9 @@ private SessionInfo[] GetSessionListInternal()
33973397
// Avoids the intermediate List<T> that new List(collection).ToArray() would produce.
33983398
var arr = new SessionInfo[_sessions.Count];
33993399
_sessions.Values.CopyTo(arr, 0);
3400+
// Defensive copy: subscribers read the snapshot with no lock, and the tracked
3401+
// instances keep being mutated in place under _sessionsLock — hand out clones.
3402+
for (var i = 0; i < arr.Length; i++) arr[i] = arr[i].Clone();
34003403
Array.Sort(arr, static (a, b) =>
34013404
{
34023405
// Main session first, then by last seen
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.IO;
3+
using System.Reflection;
4+
using OpenClaw.Shared;
5+
using Xunit;
6+
using Xunit.Abstractions;
7+
8+
namespace OpenClaw.Shared.Tests;
9+
10+
/// <summary>
11+
/// Regression: the SessionInfo[] snapshot raised through SessionsUpdated (and returned by
12+
/// GetSessionList) must be isolated from the live tracked state. GetSessionListInternal
13+
/// copies the array but its elements were the very SessionInfo instances held in _sessions,
14+
/// which UpdateTrackedSession mutates in place under _sessionsLock — so a subscriber reading
15+
/// a snapshot with no lock could observe fields (CurrentActivity/LastSeen/Status) changing
16+
/// underneath it. Before the fix this test is red: a captured snapshot's CurrentActivity is
17+
/// overwritten by a later update.
18+
/// </summary>
19+
public sealed class SessionSnapshotIsolationTests : IDisposable
20+
{
21+
private readonly ITestOutputHelper _output;
22+
private readonly string _identityDir;
23+
24+
public SessionSnapshotIsolationTests(ITestOutputHelper output)
25+
{
26+
_output = output;
27+
_identityDir = Path.Combine(Path.GetTempPath(), "session-snap-" + Guid.NewGuid().ToString("N"));
28+
Directory.CreateDirectory(_identityDir);
29+
}
30+
31+
public void Dispose()
32+
{
33+
try { Directory.Delete(_identityDir, recursive: true); } catch { /* best effort */ }
34+
}
35+
36+
[Fact]
37+
public void SessionSnapshot_IsNotAliasedToLiveTrackedState()
38+
{
39+
using var client = new OpenClawGatewayClient(
40+
"ws://127.0.0.1:1", "test-token", new TestLogger(), identityPath: _identityDir);
41+
42+
var update = typeof(OpenClawGatewayClient).GetMethod(
43+
"UpdateTrackedSession", BindingFlags.NonPublic | BindingFlags.Instance)!;
44+
45+
update.Invoke(client, new object[] { "agent:main:main", true, "compiling" });
46+
47+
var snapshot = client.GetSessionList();
48+
var captured = Assert.Single(snapshot);
49+
Assert.Equal("compiling", captured.CurrentActivity);
50+
51+
// A later update for the same session must not reach back through the earlier snapshot.
52+
update.Invoke(client, new object[] { "agent:main:main", true, "running-tool" });
53+
54+
_output.WriteLine($"captured.CurrentActivity after later update = '{captured.CurrentActivity}'");
55+
Assert.Equal("compiling", captured.CurrentActivity);
56+
}
57+
}

0 commit comments

Comments
 (0)