Skip to content

Commit

Permalink
Fixed #234
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed May 14, 2024
1 parent a39ab3a commit 5ec53e6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,16 @@ void IAsyncStateMachine.MoveNext()
}

// finalize state machine
if (StateId == IAsyncStateMachine<TState>.FinalState)
if (StateId is IAsyncStateMachine<TState>.FinalState)
{
if (exception is null)
builder.SetResult();
if (exception?.SourceException is { } e)
{
builder.SetException(e);
}
else
builder.SetException(exception.SourceException);

// perform cleanup after resuming of all suspended tasks
guardedRegionsCounter = 0;
exception = null;
State = default;
{
builder.SetResult();
}
}
}

Expand Down Expand Up @@ -376,18 +375,16 @@ void IAsyncStateMachine.MoveNext()
}

// finalize state machine
if (StateId == IAsyncStateMachine<TState>.FinalState)
if (StateId is IAsyncStateMachine<TState>.FinalState)
{
if (exception is null)
builder.SetResult(result);
if (exception?.SourceException is { } e)
{
builder.SetException(e);
}
else
builder.SetException(exception.SourceException);

// perform cleanup after resuming of all suspended tasks
guardedRegionsCounter = 0;
exception = null;
result = default;
State = default;
{
builder.SetResult(result);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,16 @@ void IAsyncStateMachine.MoveNext()
}

// finalize state machine
if (StateId == IAsyncStateMachine<TState>.FinalState)
if (StateId is IAsyncStateMachine<TState>.FinalState)
{
if (exception is null)
builder.SetResult();
if (exception?.SourceException is { } e)
{
builder.SetException(e);
}
else
builder.SetException(exception.SourceException);
{
builder.SetResult();
}

// perform cleanup after resuming of all suspended tasks
guardedRegionsCounter = 0;
Expand Down Expand Up @@ -376,12 +380,16 @@ void IAsyncStateMachine.MoveNext()
}

// finalize state machine
if (StateId == IAsyncStateMachine<TState>.FinalState)
if (StateId is IAsyncStateMachine<TState>.FinalState)
{
if (exception is null)
builder.SetResult(result);
if (exception?.SourceException is { } e)
{
builder.SetException(e);
}
else
builder.SetException(exception.SourceException);
{
builder.SetResult(result);
}

// perform cleanup after resuming of all suspended tasks
guardedRegionsCounter = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,12 @@ internal MemberExpression Build(Type stateType)
Type stateMachineType;
if (returnType == typeof(void))
{
stateMachineType = usePooling ? typeof(AsyncStateMachine<>) : typeof(PoolingAsyncStateMachine<>);
stateMachineType = usePooling ? typeof(PoolingAsyncStateMachine<>) : typeof(AsyncStateMachine<>);
stateMachineType = stateMachineType.MakeGenericType(stateType);
}
else
{
stateMachineType = usePooling ? typeof(AsyncStateMachine<,>) : typeof(PoolingAsyncStateMachine<,>);
stateMachineType = usePooling ? typeof(PoolingAsyncStateMachine<,>) : typeof(AsyncStateMachine<,>);
stateMachineType = stateMachineType.MakeGenericType(stateType, returnType);
}

Expand Down
18 changes: 18 additions & 0 deletions src/DotNext.Tests/Metaprogramming/LambdaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,22 @@ public static async Task ParameterClosure()

Equal("hello, world", await lambda.Compile().Invoke(", world"));
}

[Fact]
public static async Task RegressionIssue234()
{
var asyncMethod = new Func<Task>(DoAsync).Method;
var lambda = AsyncLambda<Func<ValueTask<int>>>(usePooling: true, (ctx, result) =>
{
Await(Expression.Call(asyncMethod));
Assign(result, 42.Const());
}).Compile();

Equal(42, await lambda.Invoke());
Equal(42, await lambda.Invoke());
Equal(42, await lambda.Invoke());
Equal(42, await lambda.Invoke());

static Task DoAsync() => Task.Delay(1);
}
}

0 comments on commit 5ec53e6

Please sign in to comment.