Skip to content

Add ConfigureAwait(false) to all library await calls #400

@rido-min

Description

@rido-min

Summary

The library projects under Libraries/ are missing .ConfigureAwait(false) on virtually all await calls (~150+ call sites). As library code that may be consumed in contexts with a SynchronizationContext, this is a best practice to prevent unnecessary context capture and potential deadlocks.

Affected projects

Project Approximate await sites
Microsoft.Teams.Api 28
Microsoft.Teams.Apps 50+
Microsoft.Teams.Common 9+
Microsoft.Teams.AI 20+
Microsoft.Teams.AI.Models.OpenAI 10+
Microsoft.Teams.Plugins.* 20+
Microsoft.Teams.Extensions.* 5+

Why this matters

  • Library code should not capture the caller's synchronization context
  • Without ConfigureAwait(false), awaiting a library method from a context with a SynchronizationContext (e.g., ASP.NET on .NET Framework, WPF, WinForms) and then blocking (.Result or .Wait()) causes a deadlock
  • Even without deadlocks, unnecessary context switching adds overhead

Suggested fix

Add .ConfigureAwait(false) to every await in library code. This can be enforced with an analyzer like ConfigureAwaitChecker or the built-in CA2007 rule.

Example:

// Before
var res = await _http.SendAsync(req, cancellationToken);

// After
var res = await _http.SendAsync(req, cancellationToken).ConfigureAwait(false);

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