From f23708651508ef0d43ea2fc42159da2762468983 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6llner?= Date: Wed, 4 Sep 2019 17:59:59 +0200 Subject: [PATCH] Do not throw from Finializer --- Capnp.Net.Runtime/Rpc/Proxy.cs | 19 ++++++++++++++++++- .../Rpc/RefCountingCapability.cs | 2 ++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Capnp.Net.Runtime/Rpc/Proxy.cs b/Capnp.Net.Runtime/Rpc/Proxy.cs index bd341ef..c0516c6 100644 --- a/Capnp.Net.Runtime/Rpc/Proxy.cs +++ b/Capnp.Net.Runtime/Rpc/Proxy.cs @@ -136,7 +136,24 @@ namespace Capnp.Rpc { if (!_disposedValue) { - ConsumedCap?.Release(); + if (disposing) + { + ConsumedCap?.Release(); + } + else + { + // When called from the Finalizer, we must not throw. + // But when reference counting goes wrong, ConsumedCapability.Release() will throw an InvalidOperationException. + // The only option here is to suppress that exception. + try + { + ConsumedCap?.Release(); + } + catch + { + } + } + _disposedValue = true; } } diff --git a/Capnp.Net.Runtime/Rpc/RefCountingCapability.cs b/Capnp.Net.Runtime/Rpc/RefCountingCapability.cs index aa05db0..16fb7fc 100644 --- a/Capnp.Net.Runtime/Rpc/RefCountingCapability.cs +++ b/Capnp.Net.Runtime/Rpc/RefCountingCapability.cs @@ -70,6 +70,8 @@ namespace Capnp.Rpc { if (++_refCount <= 1) { + --_refCount; + throw new ObjectDisposedException(nameof(ConsumedCapability)); } }