microsoftgraph / microsoftgraph/microsoft-graph-comms-samples

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

Open
#833 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
No language data
Stars
253
Forks
275
Avg merge
1d 5h
Merged PRs (30d)
1

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the interaction between CallsOnIncoming, AnswerAsync(), Task.Run(), and CallsOnUpdated, recording when MediaSession is null. Trace the call and media-session lifecycle across these entry points and compare execution with and without Task.Run(); done means answered calls consistently expose a MediaSession while rejected calls remain safely filtered.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
audio-video-rtc
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.