Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"Uno.Sdk": "6.5.31"
},
"sdk": {
"version": "10.0.202",
"version": "10.0.103",
"rollForward": "latestPatch",
"allowPrerelease": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public sealed class InMemoryAcpConnectionSessionRegistry : IAcpConnectionSession
{
private readonly object _gate = new();
private readonly Dictionary<string, AcpConnectionSession> _sessionsByProfile = new(StringComparer.Ordinal);
private readonly Dictionary<IChatService, string> _profileIdByService = new();

/// <inheritdoc />
public event Action<string, bool>? ProfileConnectionChanged;
Expand All @@ -70,13 +71,10 @@ public bool TryGetProfileId(IChatService service, out string profileId)
ArgumentNullException.ThrowIfNull(service);
lock (_gate)
{
foreach (var pair in _sessionsByProfile)
if (_profileIdByService.TryGetValue(service, out var foundProfileId))
{
if (ReferenceEquals(pair.Value.Service, service))
{
profileId = pair.Key;
return true;
}
profileId = foundProfileId;
return true;
}
}

Expand All @@ -89,10 +87,16 @@ public void Upsert(AcpConnectionSession session)
ArgumentNullException.ThrowIfNull(session);
lock (_gate)
{
if (_sessionsByProfile.TryGetValue(session.ProfileId, out var existingSession) && !ReferenceEquals(existingSession.Service, session.Service))
{
_profileIdByService.Remove(existingSession.Service);
}

_sessionsByProfile[session.ProfileId] = session with
{
LastUsedUtc = session.LastUsedUtc == default ? DateTime.UtcNow : session.LastUsedUtc
};
_profileIdByService[session.Service] = session.ProfileId;
}

// Raise outside the lock to avoid potential deadlocks from re-entrant subscribers.
Expand All @@ -104,7 +108,15 @@ public bool RemoveByProfile(string profileId)
bool removed;
lock (_gate)
{
removed = _sessionsByProfile.Remove(profileId);
if (_sessionsByProfile.TryGetValue(profileId, out var session))
{
_profileIdByService.Remove(session.Service);
removed = _sessionsByProfile.Remove(profileId);
}
else
{
removed = false;
}
}

if (removed)
Expand All @@ -123,15 +135,12 @@ public bool RemoveByService(IChatService service, out string profileId)

lock (_gate)
{
foreach (var pair in _sessionsByProfile)
if (_profileIdByService.TryGetValue(service, out var foundProfileId))
{
if (ReferenceEquals(pair.Value.Service, service))
{
profileId = pair.Key;
_sessionsByProfile.Remove(pair.Key);
removed = true;
break;
}
profileId = foundProfileId;
_profileIdByService.Remove(service);
_sessionsByProfile.Remove(profileId);
removed = true;
}
}

Expand Down Expand Up @@ -164,7 +173,11 @@ public IReadOnlyList<AcpConnectionSession> RemoveWhere(Func<AcpConnectionSession

foreach (var key in keysToRemove)
{
_sessionsByProfile.Remove(key);
if (_sessionsByProfile.TryGetValue(key, out var session))
{
_profileIdByService.Remove(session.Service);
_sessionsByProfile.Remove(key);
}
}
}

Expand Down