Skip to content

Using AnswerAsync() with Task.Run() causes the mediasession to be #833

@half2me

Description

@half2me

Describe the issue
We tried to wrap call.AnswerAsync() inside of Task.Run() and it resulted in an issue where the MediaSession on a ICall was often null instead of the expected value.

Code Snippet
This code works as expected

ICommunicationsClient client;
client.Calls().OnIncoming += CallsOnIncoming;
client.Calls().OnUpdated += CallsOnUpdated;

private void CallsOnIncoming(ICallCollection sender, CollectionEventArgs<ICall> args)
{
  foreach (var call in args.AddedResources)
  {
    if (callShouldBeAccepted()) // business logic to decide if we answer or reject
    {
      // Here we answer the call:
      ILocalMediaSession session = CreateMediaSession(/*...*/);
      call.AnswerAsync(session, /*...*/);
    } else {
      // Here we reject it
      call.RejectAsync(RejectReason.Forbidden);
    }
  }
}

private void CallsOnUpdated(ICallCollection sender, CollectionEventArgs<ICall> args)
{
  foreach (var call in args.AddedResources)
  {
    // rejected calls also show up here for some reason, so we filter those:
    if (call.MediaSession is null)
    {
      // rejected calls don't have a MediaSession
      _logger.LogInformation("Skipping call init (no MediaSession)");
      continue;
    }
    // otherwise, handle the call as usual...
    // <call handling logic here>
  }
}

Adding Task.Run() breaks things

Task.Run(async () => await call.AnswerAsync(session, /*...*/));

Now the MediaSession is often null for calls that we answered. And we see this in the logs:

Skipping call init (no MediaSession)
Skipping call init (no MediaSession)
Skipping call init (no MediaSession)

In our testing, it happens approximately 15-30% of calls, but this varies. My guess is an issue due to the task being scheduled on a different thread.

Expected behavior
It should behave the same way it did before using Task.Run()

Graph SDK (please complete the following information):

  • Version 1.31.0.225 (but the issue is the same with older versions)

Call ID
Provide the list call ids that encountered this issue. Include the time in UTC/GMT when these call have occurred.

Logs
If required, please add logs from the SDK. (Please remove any PII from the logs before uploading)

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions