Skip to content

Commit

Permalink
Merge pull request #481 from SteamRE/fix/480-deadlock
Browse files Browse the repository at this point in the history
Invoke AsyncJob continuations asynchronously
  • Loading branch information
yaakov-h authored Oct 25, 2017
2 parents 2787d46 + dd1ecb9 commit 9677726
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions SteamKit2/SteamKit2/Types/JobID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public sealed class AsyncJob<T> : AsyncJob
public AsyncJob( SteamClient client, JobID jobId )
: base( client, jobId )
{
tcs = new TaskCompletionSource<T>();
tcs = new TaskCompletionSource<T>( TaskCreationOptions.RunContinuationsAsynchronously );
}


Expand Down Expand Up @@ -287,7 +287,7 @@ public sealed class ResultSet
public AsyncJobMultiple( SteamClient client, JobID jobId, Predicate<T> finishCondition )
: base( client, jobId )
{
tcs = new TaskCompletionSource<ResultSet>();
tcs = new TaskCompletionSource<ResultSet>( TaskCreationOptions.RunContinuationsAsynchronously );

this.finishCondition = finishCondition;
}
Expand Down
47 changes: 47 additions & 0 deletions SteamKit2/Tests/AsyncJobFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using SteamKit2;
using Xunit;
Expand Down Expand Up @@ -360,5 +361,51 @@ public async Task AsyncJobMultipleThrowsFailureExceptionOnFailure()

await Assert.ThrowsAsync( typeof( AsyncJobFailedException ), async () => await asyncTask );
}

[Fact]
public async Task AsyncJobContinuesAsynchronously()
{
SteamClient client = new SteamClient();

var asyncJob = new AsyncJob<Callback>( client, 123 );
var asyncTask = asyncJob.ToTask();

var continuationThreadID = -1;
var continuation = asyncTask.ContinueWith( t =>
{
continuationThreadID = Thread.CurrentThread.ManagedThreadId;
}, TaskContinuationOptions.ExecuteSynchronously );

var completionThreadID = Thread.CurrentThread.ManagedThreadId;
asyncJob.AddResult( new Callback { JobID = 123 } );

await continuation;

Assert.NotEqual( -1, continuationThreadID );
Assert.NotEqual( completionThreadID, continuationThreadID );
}

[Fact]
public async Task AsyncJobMultipleContinuesAsynchronously()
{
SteamClient client = new SteamClient();

var asyncJob = new AsyncJobMultiple<Callback>( client, 123, call => true );
var asyncTask = asyncJob.ToTask();

var continuationThreadID = -1;
var continuation = asyncTask.ContinueWith( t =>
{
continuationThreadID = Thread.CurrentThread.ManagedThreadId;
}, TaskContinuationOptions.ExecuteSynchronously );

var completionThreadID = Thread.CurrentThread.ManagedThreadId;
asyncJob.AddResult( new Callback { JobID = 123 } );

await continuation;

Assert.NotEqual( -1, continuationThreadID );
Assert.NotEqual( completionThreadID, continuationThreadID );
}
}
}

0 comments on commit 9677726

Please sign in to comment.