Additional diagnostic information for troubleshooting capability lifecycles

This commit is contained in:
Christian Köllner 2019-12-30 18:47:33 +01:00
parent abe9921ec5
commit 846cbaac6b
4 changed files with 39 additions and 5 deletions

View File

@ -24,7 +24,11 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'"> <PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE;DEBUG;DebugCapabilityLifecycle</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|netstandard2.0|AnyCPU'">
<DefineConstants>TRACE;DEBUG;DebugCapabilityLifecycle</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -22,7 +22,10 @@ namespace Capnp.Rpc
internal abstract void Unfreeze(); internal abstract void Unfreeze();
internal abstract void AddRef(); internal abstract void AddRef();
internal abstract void Release(); internal abstract void Release(
[System.Runtime.CompilerServices.CallerMemberName] string methodName = "",
[System.Runtime.CompilerServices.CallerFilePath] string filePath = "",
[System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0);
#if DebugFinalizers #if DebugFinalizers
public string CreatorMemberName { get; set; } public string CreatorMemberName { get; set; }

View File

@ -37,7 +37,10 @@ namespace Capnp.Rpc
ProvidedCap.Claim(); ProvidedCap.Claim();
} }
internal override void Release() internal override void Release(
[System.Runtime.CompilerServices.CallerMemberName] string methodName = "",
[System.Runtime.CompilerServices.CallerFilePath] string filePath = "",
[System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0)
{ {
ProvidedCap.Relinquish(); ProvidedCap.Relinquish();
} }

View File

@ -1,4 +1,5 @@
using System; using Microsoft.Extensions.Logging;
using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -26,6 +27,14 @@ namespace Capnp.Rpc
// Value 0 has the special meaning of being in state C. // Value 0 has the special meaning of being in state C.
long _refCount = 1; long _refCount = 1;
#if DebugCapabilityLifecycle
ILogger Logger { get; } = Logging.CreateLogger<RefCountingCapability>();
string _releasingMethodName;
string _releasingFilePath;
int _releasingLineNumber;
#endif
~RefCountingCapability() ~RefCountingCapability()
{ {
Dispose(false); Dispose(false);
@ -72,12 +81,20 @@ namespace Capnp.Rpc
{ {
--_refCount; --_refCount;
#if DebugCapabilityLifecycle
Logger.LogError($"Attempted to add reference to capability which was already released. " +
$"Releasing entity: {_releasingFilePath}, line {_releasingLineNumber}, method {_releasingMethodName}" +
$"Current stack trace: {Environment.StackTrace}");
#endif
throw new ObjectDisposedException(ToString(), "Attempted to add reference to capability which was already released"); throw new ObjectDisposedException(ToString(), "Attempted to add reference to capability which was already released");
} }
} }
} }
internal sealed override void Release() internal sealed override void Release(
[System.Runtime.CompilerServices.CallerMemberName] string methodName = "",
[System.Runtime.CompilerServices.CallerFilePath] string filePath = "",
[System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0)
{ {
lock (_reentrancyBlocker) lock (_reentrancyBlocker)
{ {
@ -86,6 +103,13 @@ namespace Capnp.Rpc
case 1: // initial state, actually ref. count 0 case 1: // initial state, actually ref. count 0
case 2: // actually ref. count 1 case 2: // actually ref. count 1
_refCount = 0; _refCount = 0;
#if DebugCapabilityLifecycle
_releasingMethodName = methodName;
_releasingFilePath = filePath;
_releasingLineNumber = lineNumber;
#endif
Dispose(true); Dispose(true);
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
break; break;