question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create Replica never returns if RunAsync is endless

See original GitHub issue

In 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:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
loekdcommented, Apr 6, 2019

I’ve taken a similar route and changed the call to RunAsync into an invokation without an await within CreateAsync. The task is awaited when the service is closed or deleted. The CancellationToken passed to RunAsync 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.

0reactions
VaclavKcommented, Apr 8, 2019

yeah the fix looks real good

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found