Create Replica never returns if RunAsync is endless
See original GitHub issueIn your Tests project, you have the following Test:
[TestMethod]
public async Task TestServiceState_InMemoryState_PromoteActiveSecondary()
{
var replicaSet = new MockStatefulServiceReplicaSet<MyStatefulService>(CreateStatefulService, CreateStateManagerReplica);
await replicaSet.AddReplicaAsync(ReplicaRole.Primary, 1);
await replicaSet.AddReplicaAsync(ReplicaRole.ActiveSecondary, 2);
await replicaSet.AddReplicaAsync(ReplicaRole.ActiveSecondary, 3);
const string stateName = "test";
var payload = new Payload(StatePayload);
//insert data
await replicaSet.Primary.ServiceInstance.InsertAsync(stateName, payload);
//promote one of the secondaries to primary
await replicaSet.PromoteActiveSecondaryToPrimaryAsync(2);
//get data
var payloads = (await replicaSet.Primary.ServiceInstance.GetPayloadsAsync()).ToList();
//data should match what was inserted against the primary
Assert.IsTrue(payloads.Count == 1);
Assert.IsTrue(payloads[0].Content == payload.Content);
//the primary should not have any in-memory state
var payloadsFromOldPrimary = await replicaSet[1].ServiceInstance.GetPayloadsAsync();
Assert.IsTrue(!payloadsFromOldPrimary.Any());
}
The problem is this test is not working, if service is executing endless loop in RunAsync, let’s say for background task.
Some details:
AddReplicaAsync will wait for CreateAsync:
public async Task AddReplicaAsync(ReplicaRole role, long? replicaId = null, int activationDelayMs = 0)
{
var serviceContext = MockStatefulServiceContextFactory.Create(CodePackageActivationContext, ServiceTypeName, ServiceUri, Guid.NewGuid(), replicaId ?? _random.Next());
var stateManager = _stateManagerFactory(serviceContext, _reliableStates);
var replica = new MockStatefulServiceReplica<TStatefulService>(_serviceFactory, serviceContext, stateManager);
await replica.CreateAsync(role);
_replicas.Add(replica);
}
and then CreateAsync will wait for RunAsync which will never return
public async Task CreateAsync(ReplicaRole role)
{
await OpenAsync(ReplicaOpenMode.New);
if (role == ReplicaRole.Primary)
{
await OpenServiceReplicaListeners();
await ChangeRoleAsync(role);
await RunAsync();
}
else
await ChangeRoleAsync(role);
}
My suggestion is to wait for RunAnync to be completed in CreateAsync. Thank you.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
HttpClient.GetAsync(...) never returns when using await/ ...
This only occurs in certain circumstances when using the new async/await language functionality and Tasks API - the code always seems to work...
Read more >README.md - ServiceFabric.Mocks
Fix for issue 82 'Create Replica never returns if RunAsync is endless.' Reported by sportfort. RunAsync is now not awaited until the replica...
Read more >Difference between await method() and await Task.Run ...
It didn't, task.run returned. Aside from the synchronization context stuff you mentioned those calls are the same if you await task.run.
Read more >C# Console program end before async function finished
I have simple program where i am calling my main function Start() from timer after every 10 minutes. i have seen that my...
Read more >Asynchronous actions and polling
Asynchronous playbook tasks always return changed. If the task is using a module that requires the user to annotate changes with changed_when ,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve taken a similar route and changed the call to
RunAsync
into an invokation without an await withinCreateAsync
. The task is awaited when the service is closed or deleted. TheCancellationToken
passed toRunAsync
is cancelled, just as it would be in the real runtime. Added a unit test to prove the functionality. @sportfort , @Patrick-Spiegel , @VaclavK thanks for the assistance in this issue! Please let me know if this is fixed in v3.4.10.yeah the fix looks real good