Merge cap. lifecycle debugging

This commit is contained in:
Christian Köllner 2020-02-16 18:00:22 +01:00
commit 3e96fd0fd5
4 changed files with 39 additions and 5 deletions

View File

@ -25,7 +25,11 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'"> <PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE;DEBUG</DefineConstants>
</PropertyGroup>
<PropertyGroup>
<DefineConstants>DebugCapabilityLifecycle</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -18,7 +18,10 @@
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

@ -36,7 +36,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.Tasks; using System.Threading.Tasks;
namespace Capnp.Rpc namespace Capnp.Rpc
@ -25,6 +26,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);
@ -71,12 +80,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)
{ {
@ -85,6 +102,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;