Skip to content

Commit

Permalink
Fix proxies to accept multiple Dispose calls
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Aug 25, 2020
1 parent d7f63d6 commit 64c9625
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/StreamJsonRpc.Tests/DisposableProxyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ public async Task IDisposableInNotificationArgumentIsRejected()
}

[Fact]
public async Task DisposableReturnValue_DisposeThrowsOnSecondCall()
public async Task DisposableReturnValue_DisposeSwallowsSecondCall()
{
IDisposable? proxyDisposable = await this.client.GetDisposableAsync();
Assumes.NotNull(proxyDisposable);
proxyDisposable.Dispose();
Assert.Throws<ObjectDisposedException>(proxyDisposable.Dispose);
proxyDisposable.Dispose();
}

[Fact]
Expand Down
8 changes: 8 additions & 0 deletions src/StreamJsonRpc.Tests/JsonRpcProxyGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ public void IsDisposedReturnsTrueWhenJsonRpcIsDisposed()
Assert.True(disposableClient.IsDisposed);
}

[Fact]
public void DisposeTwiceDoesNotThrow()
{
var disposableClient = (IDisposable)this.clientRpc;
disposableClient.Dispose();
disposableClient.Dispose();
}

[Fact]
public async Task TaskReturningRpcMethodsThrowAfterDisposal()
{
Expand Down
8 changes: 7 additions & 1 deletion src/StreamJsonRpc/ProxyGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,13 @@ private static void ImplementDisposeMethod(TypeBuilder proxyTypeBuilder, FieldBu
Type.EmptyTypes);
ILGenerator il = methodBuilder.GetILGenerator();

EmitThrowIfDisposed(proxyTypeBuilder, il, disposedField);
// if (this.disposed) { return; }
Label notDisposedLabel = il.DefineLabel();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld, disposedField);
il.Emit(OpCodes.Brfalse_S, notDisposedLabel);
il.Emit(OpCodes.Ret);
il.MarkLabel(notDisposedLabel);

// this.disposed = true;
il.Emit(OpCodes.Ldarg_0);
Expand Down

0 comments on commit 64c9625

Please sign in to comment.