diff --git a/change/react-native-windows-4672fa37-ea7f-4ffd-82cd-40e7d8b41de5.json b/change/react-native-windows-4672fa37-ea7f-4ffd-82cd-40e7d8b41de5.json new file mode 100644 index 00000000000..3b1c6d61ae5 --- /dev/null +++ b/change/react-native-windows-4672fa37-ea7f-4ffd-82cd-40e7d8b41de5.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Fix a crash calling CallInvoker during shutdown", + "packageName": "react-native-windows", + "email": "30809111+acoates-ms@users.noreply.github.com", + "dependentChangeType": "patch" +} diff --git a/vnext/Microsoft.ReactNative.Cxx/TurboModuleProvider.cpp b/vnext/Microsoft.ReactNative.Cxx/TurboModuleProvider.cpp index bfeb719adaa..7b15527ab45 100644 --- a/vnext/Microsoft.ReactNative.Cxx/TurboModuleProvider.cpp +++ b/vnext/Microsoft.ReactNative.Cxx/TurboModuleProvider.cpp @@ -13,17 +13,23 @@ struct AbiCallInvoker final : facebook::react::CallInvoker { AbiCallInvoker(IReactContext const &context) : m_context(context) {} void invokeAsync(facebook::react::CallFunc &&func) noexcept override { - m_context.CallInvoker().InvokeAsync( - [context = m_context, func = std::move(func)](const winrt::Windows::Foundation::IInspectable &runtimeHandle) { - func(GetOrCreateContextRuntime(context, runtimeHandle)); - }); + auto callInvoker = m_context.CallInvoker(); + if (callInvoker) { + callInvoker.InvokeAsync( + [context = m_context, func = std::move(func)](const winrt::Windows::Foundation::IInspectable &runtimeHandle) { + func(GetOrCreateContextRuntime(context, runtimeHandle)); + }); + } } void invokeSync(facebook::react::CallFunc &&func) override { - m_context.CallInvoker().InvokeSync( - [context = m_context, func = std::move(func)](const winrt::Windows::Foundation::IInspectable &runtimeHandle) { - func(GetOrCreateContextRuntime(context, runtimeHandle)); - }); + auto callInvoker = m_context.CallInvoker(); + if (callInvoker) { + callInvoker.InvokeSync( + [context = m_context, func = std::move(func)](const winrt::Windows::Foundation::IInspectable &runtimeHandle) { + func(GetOrCreateContextRuntime(context, runtimeHandle)); + }); + } } private: