Skip to content

[2.0 Servicing] Fix UniversalBGTask crash and gracefully handle CoCreateInstance failures - #6568

Merged
Godly T.Alias (godlytalias) merged 5 commits into
release/2.0-stablefrom
user/godlytalias/fix-universalbgtask-crash-on-release-2.0-stable
Jun 30, 2026
Merged

[2.0 Servicing] Fix UniversalBGTask crash and gracefully handle CoCreateInstance failures#6568
Godly T.Alias (godlytalias) merged 5 commits into
release/2.0-stablefrom
user/godlytalias/fix-universalbgtask-crash-on-release-2.0-stable

Conversation

@godlytalias

Copy link
Copy Markdown
Contributor

UniversalBGTask::Task::Run could crash backgroundTaskHost.exe in two scenarios:

  1. When LocalSettings has no entry for the task's TaskId, ApplicationDataContainerSettings::Lookup returns S_OK with a null IInspectable. The subsequent unbox_valuewinrt::guid then throws hresult_no_interface, and because the broker treats the failure as transient it re-fires the trigger forever.

  2. When CoCreateInstance fails (e.g. the COM class isn't registered against the package), the wrapped THROW_IF_FAILED produced no diagnostic context.

Fix

  • If LocalSettings.Lookup returns null, log via wil and Unregister the orphaned OS task so the broker stops re-firing it.
  • If CoCreateInstance fails, log the HR with the CLSID and TaskId and return cleanly. The registration is left in place so the broker can retry on the next trigger in case the failure is transient.

Servicing

  • Contained behind WINAPPSDK_CHANGEID_62755661 (WinAppSDK_2_2_2); the original (throwing) behavior is preserved in the else branch for apps pinned to an earlier patch level.
  • RuntimeCompatibilityChange: UniversalBGTask_RunCrash = 62755661.
  • ADO servicing bug: 62755661.

Cherry picked from #6507

…ures (#6507)

UniversalBGTask::Task::Run could crash backgroundTaskHost.exe in two
scenarios:

1. When LocalSettings has no entry for the task's TaskId,
   ApplicationDataContainerSettings::Lookup returns S_OK with a null
   IInspectable. The subsequent unbox_value<guid> then throws
   hresult_no_interface, and because the broker treats the failure as
   transient it re-fires the trigger forever.

2. When CoCreateInstance fails (e.g. the COM class isn't registered
   against the package), the wrapped THROW_IF_FAILED produced no
   diagnostic context.

Fix:

- If LocalSettings.Lookup returns null, log via wil and unregister the
  orphaned OS task so the broker stops re-firing it.
- If CoCreateInstance fails, log the HR with the CLSID and TaskId and
  return cleanly. The registration is left in place so the broker can
  retry on the next trigger in case the failure is transient.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…fix (bug 62755661)

Wrap the crash-hardening behavior behind WINAPPSDK_CHANGEID_62755661 (WinAppSDK_2_2_2) and add the RuntimeCompatibilityChange.UniversalBGTask_RunCrash enum entry so apps can opt out via RuntimeCompatibilityOptions.
@godlytalias
Godly T.Alias (godlytalias) marked this pull request as ready for review June 18, 2026 08:05
@godlytalias

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@agniuks

agniuks commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment thread dev/WindowsAppRuntime_UniversalBGTaskDLL/Task.cpp Outdated
Co-authored-by: agniuks <41223743+agniuks@users.noreply.github.com>
@godlytalias

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

…rsalBGTaskDLL

Task.cpp now includes <FrameworkUdk/Containment.h> for the containment fix,
but WindowsAppRuntime_UniversalBGTaskDLL.vcxproj did not import the
Microsoft.FrameworkUdk targets, so the FrameworkUdk include directory was
missing from the compiler command line, causing:

  Task.cpp(10,10): error C1083: Cannot open include file:
  'FrameworkUdk/Containment.h': No such file or directory

Import Microsoft.FrameworkUdk.targets (which adds the include path and the
Microsoft.Internal.FrameworkUdk.lib link dependency) plus the matching
package-restore Error check, mirroring the existing pattern used by
Microsoft.Windows.ApplicationModel.Resources.vcxproj and other Containment
consumers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@godlytalias

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@godlytalias
Godly T.Alias (godlytalias) merged commit b5f2d0b into release/2.0-stable Jun 30, 2026
46 checks passed
@godlytalias
Godly T.Alias (godlytalias) deleted the user/godlytalias/fix-universalbgtask-crash-on-release-2.0-stable branch June 30, 2026 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants