Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - Change DelegateInstrumentation set continuations behaviour to copy Calltarget #5049

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,11 @@ public Func0Wrapper(Delegate? target, TCallbacks callbacks)
{
try
{
if (SetContinuation is { } setContinuation)
{
returnValue = setContinuation(Callbacks, sender, exception, state, returnValue);
}

returnValue = Callbacks.OnDelegateEnd(sender, returnValue, exception, state);
}
catch (Exception innerException)
Expand All @@ -832,11 +837,6 @@ public Func0Wrapper(Delegate? target, TCallbacks callbacks)
}
}

if (SetContinuation is { } setContinuation)
{
returnValue = setContinuation(Callbacks, sender, exception, state, returnValue);
}

return returnValue;
}
}
Expand Down Expand Up @@ -894,6 +894,11 @@ public Func1Wrapper(Delegate? target, TCallbacks callbacks)
{
try
{
if (SetContinuation is { } setContinuation)
{
returnValue = setContinuation(Callbacks, sender, exception, state, returnValue);
}

returnValue = Callbacks.OnDelegateEnd(sender, returnValue, exception, state);
}
catch (Exception innerException)
Expand All @@ -902,11 +907,6 @@ public Func1Wrapper(Delegate? target, TCallbacks callbacks)
}
}

if (SetContinuation is { } setContinuation)
{
returnValue = setContinuation(Callbacks, sender, exception, state, returnValue);
}

return returnValue;
}
}
Expand Down Expand Up @@ -965,6 +965,11 @@ public Func2Wrapper(Delegate? target, TCallbacks callbacks)
{
try
{
if (SetContinuation is { } setContinuation)
{
returnValue = setContinuation(Callbacks, sender, exception, state, returnValue);
}

returnValue = Callbacks.OnDelegateEnd(sender, returnValue, exception, state);
}
catch (Exception innerException)
Expand All @@ -973,11 +978,6 @@ public Func2Wrapper(Delegate? target, TCallbacks callbacks)
}
}

if (SetContinuation is { } setContinuation)
{
returnValue = setContinuation(Callbacks, sender, exception, state, returnValue);
}

return returnValue;
}
}
Expand Down Expand Up @@ -1037,6 +1037,11 @@ public Func3Wrapper(Delegate? target, TCallbacks callbacks)
{
try
{
if (SetContinuation is { } setContinuation)
{
returnValue = setContinuation(Callbacks, sender, exception, state, returnValue);
}

returnValue = Callbacks.OnDelegateEnd(sender, returnValue, exception, state);
}
catch (Exception innerException)
Expand All @@ -1045,11 +1050,6 @@ public Func3Wrapper(Delegate? target, TCallbacks callbacks)
}
}

if (SetContinuation is { } setContinuation)
{
returnValue = setContinuation(Callbacks, sender, exception, state, returnValue);
}

return returnValue;
}
}
Expand Down Expand Up @@ -1110,6 +1110,11 @@ public Func4Wrapper(Delegate? target, TCallbacks callbacks)
{
try
{
if (SetContinuation is { } setContinuation)
{
returnValue = setContinuation(Callbacks, sender, exception, state, returnValue);
}

returnValue = Callbacks.OnDelegateEnd(sender, returnValue, exception, state);
}
catch (Exception innerException)
Expand All @@ -1118,11 +1123,6 @@ public Func4Wrapper(Delegate? target, TCallbacks callbacks)
}
}

if (SetContinuation is { } setContinuation)
{
returnValue = setContinuation(Callbacks, sender, exception, state, returnValue);
}

return returnValue;
}
}
Expand Down Expand Up @@ -1184,6 +1184,11 @@ public Func5Wrapper(Delegate? target, TCallbacks callbacks)
{
try
{
if (SetContinuation is { } setContinuation)
{
returnValue = setContinuation(Callbacks, sender, exception, state, returnValue);
}

returnValue = Callbacks.OnDelegateEnd(sender, returnValue, exception, state);
}
catch (Exception innerException)
Expand All @@ -1192,11 +1197,6 @@ public Func5Wrapper(Delegate? target, TCallbacks callbacks)
}
}

if (SetContinuation is { } setContinuation)
{
returnValue = setContinuation(Callbacks, sender, exception, state, returnValue);
}

return returnValue;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,126 @@ public async Task Async1Test()
value.Should().Be(4);
}

[Fact]
public async Task Async1WithAsyncExceptionTest()
{
var result = new StrongBox<int>(0);
var callbacks = new Async1Callbacks();
Func<string, Task<int>> func = async (arg1) =>
{
callbacks.Count.Value++;
// force an exception
int x = 0, y = 0, z = 0;
z = x / y;
await Task.Yield();
return 42;
};
func = func.Instrument(callbacks);
await Assert.ThrowsAsync<DivideByZeroException>(
async () =>
{
result = new StrongBox<int>(await func("Arg01").ConfigureAwait(false));
}).ConfigureAwait(false);

callbacks.Count.Value.Should().Be(4);
Copy link
Collaborator

@zacharycmontoya zacharycmontoya Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So just to make sure I'm understanding this part of the unit test, the result is 4 because we're calling Count.Value++ in the following sequence right?

  • OnDelegateBegin
  • original func delegate
  • ⚠️ Exception happens ⚠️
  • OnDelegateEndAsync
  • OnDelegateEnd

Edit: Changed to list OnDelegateEndAsync then OnDelegateEnd

Copy link
Member Author

@tonyredondo tonyredondo Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the execution order will depend if the target delegate has finished or not. If when we are setting the continuation the task has been completed we call the continuation directly. If not, we set the continuation so the execution can occur after the OnDelegateEnd. In the case of an exception because the initial Task is completed (faulted) we run the EndAsync before End.


var value = 0;
CustomFunc<string, Task<int>> func2 = async (arg1) =>
{
Interlocked.Increment(ref value).Should().Be(2);
// force an exception
int x = 0, y = 0, z = 0;
z = x / y;
await Task.Yield();
return 42;
};
func2 = DelegateInstrumentation.Wrap(func2, new DelegateFunc1Callbacks(
(target, arg1) =>
{
arg1.Should().Be("Arg01");
Interlocked.Increment(ref value).Should().Be(1);
return null;
},
(target, returnValue, exception, state) =>
{
Interlocked.Increment(ref value).Should().Be(4);
return returnValue;
},
onDelegateAsyncEnd: async (sender, returnValue, exception, state) =>
{
Interlocked.Increment(ref value).Should().Be(3);
await Task.Delay(100).ConfigureAwait(false);
return ((int)returnValue) + 1;
}));

await Assert.ThrowsAsync<DivideByZeroException>(
async () =>
{
result = new StrongBox<int>(await func2("Arg01").ConfigureAwait(false));
}).ConfigureAwait(false);

value.Should().Be(4);
}

[Fact]
public async Task Async1WithExceptionTest()
{
var result = new StrongBox<int>(0);
var callbacks = new Async1Callbacks();
Func<string, Task<int>> func = (arg1) =>
{
callbacks.Count.Value++;
// force an exception
int x = 0, y = 0, z = 0;
z = x / y;
return Task.FromResult(42);
};
func = func.Instrument(callbacks);
await Assert.ThrowsAsync<DivideByZeroException>(
async () =>
{
result = new StrongBox<int>(await func("Arg01").ConfigureAwait(false));
}).ConfigureAwait(false);

callbacks.Count.Value.Should().Be(4);

var value = 0;
CustomFunc<string, Task<int>> func2 = (arg1) =>
{
Interlocked.Increment(ref value).Should().Be(2);
// force an exception
int x = 0, y = 0, z = 0;
z = x / y;
return Task.FromResult(42);
};
func2 = DelegateInstrumentation.Wrap(func2, new DelegateFunc1Callbacks(
(target, arg1) =>
{
arg1.Should().Be("Arg01");
Interlocked.Increment(ref value).Should().Be(1);
return null;
},
(target, returnValue, exception, state) =>
{
Interlocked.Increment(ref value).Should().Be(4);
return returnValue;
},
onDelegateAsyncEnd: async (sender, returnValue, exception, state) =>
{
Interlocked.Increment(ref value).Should().Be(3);
await Task.Delay(100).ConfigureAwait(false);
return ((int)returnValue) + 1;
}));

await Assert.ThrowsAsync<DivideByZeroException>(
async () =>
{
result = new StrongBox<int>(await func2("Arg01").ConfigureAwait(false));
}).ConfigureAwait(false);

value.Should().Be(4);
}

public readonly struct Async1Callbacks : IBegin1Callbacks, IReturnCallback, IReturnAsyncCallback
{
public Async1Callbacks()
Expand Down
Loading