Skip to content

Commit 326251b

Browse files
committed
Track the number of live multiplexers, and report it in the error dump if non-trivial
1 parent bc086f3 commit 326251b

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/StackExchange.Redis/ConnectionMultiplexer.Debug.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@ namespace StackExchange.Redis;
44

55
public partial class ConnectionMultiplexer
66
{
7-
private static int _collectedWithoutDispose;
7+
private static int _collectedWithoutDispose, s_MuxerDestroyCount, s_MuxerCreateCount;
88
internal static int CollectedWithoutDispose => Volatile.Read(ref _collectedWithoutDispose);
99

10+
internal static int GetLiveObjectCount(out int created, out int destroyed, out int nonDisposed)
11+
{
12+
// read destroy first, to prevent negative numbers in race conditions
13+
destroyed = Volatile.Read(ref s_MuxerDestroyCount);
14+
created = Volatile.Read(ref s_MuxerCreateCount);
15+
nonDisposed = Volatile.Read(ref _collectedWithoutDispose);
16+
return created - destroyed;
17+
}
18+
1019
/// <summary>
1120
/// Invoked by the garbage collector.
1221
/// </summary>
1322
~ConnectionMultiplexer()
1423
{
24+
Interlocked.Increment(ref s_MuxerDestroyCount);
1525
Interlocked.Increment(ref _collectedWithoutDispose);
1626
}
1727

src/StackExchange.Redis/ConnectionMultiplexer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ static ConnectionMultiplexer()
128128

129129
private ConnectionMultiplexer(ConfigurationOptions configuration, ServerType? serverType = null, EndPointCollection? endpoints = null)
130130
{
131+
Interlocked.Increment(ref s_MuxerCreateCount);
132+
131133
RawConfig = configuration ?? throw new ArgumentNullException(nameof(configuration));
132134
EndPoints = endpoints ?? RawConfig.EndPoints.Clone();
133135
EndPoints.SetDefaultPorts(serverType, ssl: RawConfig.Ssl);
@@ -2258,6 +2260,7 @@ ConfigurationChangedChannel is byte[] channel
22582260
public void Dispose()
22592261
{
22602262
GC.SuppressFinalize(this);
2263+
Interlocked.Increment(ref s_MuxerDestroyCount);
22612264
Close(!_isDisposed);
22622265
sentinelConnection?.Dispose();
22632266
var oldTimer = Interlocked.Exchange(ref sentinelPrimaryReconnectTimer, null);
@@ -2270,6 +2273,7 @@ public void Dispose()
22702273
public async ValueTask DisposeAsync()
22712274
{
22722275
GC.SuppressFinalize(this);
2276+
Interlocked.Increment(ref s_MuxerDestroyCount);
22732277
await CloseAsync(!_isDisposed).ForAwait();
22742278
if (sentinelConnection is ConnectionMultiplexer sentinel)
22752279
{

src/StackExchange.Redis/ExceptionFactory.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@ private static void AddCommonDetail(
347347
Add(data, sb, "Last-Result-Bytes", "last-in", bs.Connection.BytesLastResult.ToString());
348348
Add(data, sb, "Inbound-Buffer-Bytes", "cur-in", bs.Connection.BytesInBuffer.ToString());
349349

350+
var liveMuxers = ConnectionMultiplexer.GetLiveObjectCount(out var created, out var destroyed, out var nonDisposed);
351+
if (created > 1)
352+
{
353+
Add(data, sb, "Live-Multiplexers", "lm", $"{liveMuxers}/{created}/{destroyed}/{nonDisposed}");
354+
}
355+
350356
Add(data, sb, "Sync-Ops", "sync-ops", multiplexer.syncOps.ToString());
351357
Add(data, sb, "Async-Ops", "async-ops", multiplexer.asyncOps.ToString());
352358

0 commit comments

Comments
 (0)