Repository navigation
Fix crash when a drawing surface can't be created because the graphics device was lost - #16655
Open
Gordon MacMaster (gmacmaster) wants to merge 1 commit into
Conversation
* Don't terminate when a drawing surface can't be created (device lost) CompContext::CreateDrawingSurfaceBrush (both type redirects) is noexcept but calls CompositionGraphicsDevice::CreateDrawingSurface, which throws when the graphics device is lost or being reset: a display change, a remote desktop session, a driver reset. The exception cannot leave a noexcept function, so std::terminate aborts the app. Seen in production on two Windows POS registers (react-native-windows 0.84.0): ReactNativeIsland::ScaleFactor -> Arrange -> layout -> BorderPrimitive (and react-native-svg's SvgView::Invalidate) -> CreateDrawingSurfaceBrush -> throw_hresult -> abort. - CreateDrawingSurfaceBrush catches winrt::hresult_error and returns null. - AutoDrawDrawingSurface accepts a null surface (no render target), which also covers external callers such as react-native-svg. - Every in-tree caller skips drawing on null: BorderPrimitive, ImageComponentView (and DrawImage), ReactNativeIsland loading bar, ScrollView arrows, WindowsTextInput, Unimplemented view (still runs the base layout update), DebuggerUIIsland, UriImageManager. Lazily created surfaces stay null, so the next update creates them again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01K4oHd4uE4aKmzxmFNqHHe5 * Retry images and keep borders marked after a lost drawing surface Review of the device-lost fix: - ImageComponentView: once the final image response has arrived, nothing calls ensureDrawingSurface() again (no layout hook, and nothing invokes OnRenderingDeviceLost), so a failed surface left the image blank until another response or a theme change. It now retries on a UI timer (Timer::Create, so a custom timer factory is honoured): 250 ms, 500 ms, ... up to 10 attempts, one timer at a time. A successful surface or prepareForRecycle cancels it and resets the count; the Tick handler holds the view weakly and stops the timer if the view is gone. - BorderPrimitive: finalize() cleared m_needsUpdate before drawing, so a layer that got no surface was treated as drawn and never redrawn until props, layout or theme changed. SetBorderLayerPropertiesCommon now returns false only for a failed surface (nothing to draw is still success); SetBorderLayerProperties and DrawAllBorderLayers pass it up, TryUpdateSpecialBorderLayers reports it through a new out-parameter, and finalize() keeps m_needsUpdate set so the next finalize draws it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01K4oHd4uE4aKmzxmFNqHHe5 --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Gordon MacMaster (gmacmaster)
requested a review
from a team
as a code owner
November 24, 2026 02:27
Contributor
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Surface-creation failures can still crash the playground’s custom-image handler through an unchecked null render target, while incomplete retry and device-recreation logic can leave images and borders blank after recovery.
5 open issues
What changed in this PR
This PR hardens React Native Windows drawing-surface creation against graphics-device loss, replacing termination with null results and adding recovery handling for borders and images.
Changes:
- Catch drawing-surface creation failures and guard drawing code against null surfaces.
- Preserve pending border updates and add bounded, timer-based image retries.
- Add a prerelease changelog entry.
| File | Description |
|---|---|
vnext/Microsoft.ReactNative/Fabric/Composition/UriImageManager.cpp |
Return null when SVG surface creation fails. |
vnext/Microsoft.ReactNative/Fabric/Composition/UnimplementedNativeViewComponentView.cpp |
Skip label drawing while preserving base layout updates. |
vnext/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputComponentView.cpp |
Skip setup when the drawing surface is unavailable. |
vnext/Microsoft.ReactNative/Fabric/Composition/ScrollViewComponentView.cpp |
Guard scrollbar-arrow surface creation. |
vnext/Microsoft.ReactNative/Fabric/Composition/ReactNativeIsland.cpp |
Guard loading-bar surface creation. |
vnext/Microsoft.ReactNative/Fabric/Composition/ImageComponentView.h |
Declare retry helpers, timer, and attempt counter. |
vnext/Microsoft.ReactNative/Fabric/Composition/ImageComponentView.cpp |
Add bounded image retries, cleanup, and null guards. |
vnext/Microsoft.ReactNative/Fabric/Composition/DebuggerUIIsland.cpp |
Skip redraw when surface creation fails. |
vnext/Microsoft.ReactNative/Fabric/Composition/CompositionContextHelper.cpp |
Catch surface-creation exceptions in both composition implementations. |
vnext/Microsoft.ReactNative/Fabric/Composition/BorderPrimitive.h |
Add surface-failure reporting to border updates. |
vnext/Microsoft.ReactNative/Fabric/Composition/BorderPrimitive.cpp |
Propagate failures and retain pending border updates. |
vnext/Microsoft.ReactNative.Cxx/AutoDraw.h |
Accept null surfaces without starting a draw. |
change/react-native-windows-drawing-surface-device-lost.json |
Record the crash fix for release notes. |
Review effort: Balanced
Comment on lines
+19
to
+20
| if (!drawingSurface) { | ||
| return; |
| if (drawingSurfaceFailed) { | ||
| // A layer got no drawing surface (graphics device lost). Keep the border marked so the next | ||
| // finalize draws it again, instead of leaving that edge missing until props, layout or theme change. | ||
| m_needsUpdate = true; |
| static constexpr uint8_t kMaxDrawingSurfaceRetries = 10; | ||
|
|
||
| void ImageComponentView::scheduleDrawingSurfaceRetry() noexcept { | ||
| if (m_drawingSurfaceRetryTimer || m_drawingSurfaceRetryAttempts >= kMaxDrawingSurfaceRetries) { |
| if (!m_drawingSurface) { | ||
| // The graphics device is lost. Nothing else re-runs this for an image once its response has | ||
| // arrived (no layout hook, and OnRenderingDeviceLost is never called), so retry on a timer. | ||
| scheduleDrawingSurfaceRetry(); |
Comment on lines
+103
to
+104
| if (!drawingBrush) { | ||
| return nullptr; |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Type of Change
Why
CompContext::CreateDrawingSurfaceBrush(both the Windows and Microsoft type redirects) isnoexcept, but it callsCompositionGraphicsDevice::CreateDrawingSurface, which throws when the graphics device is lost or being reset (display change, remote desktop session, driver reset). The exception can't leave anoexceptfunction, sostd::terminateaborts the app.We hit this in production on two Windows POS registers running react-native-windows 0.84.0. The crashing stacks were:
and the same path through react-native-svg's
SvgView::Invalidate.What
Stop the crash
CreateDrawingSurfaceBrush(both overloads) catcheswinrt::hresult_errorand returnsnullptrinstead of terminating.AutoDrawDrawingSurface(AutoDraw.h) accepts a null surface and leaves the render target null, soGetRenderTarget()/IsValid()report nothing to draw. This also protects external callers such as react-native-svg.BorderPrimitive,ImageComponentView(andDrawImage), theReactNativeIslandloading bar,ScrollViewarrows,WindowsTextInput,UnimplementedNativeViewComponentView(still runs the base layout update),DebuggerUIIsland, andUriImageManager. Surfaces that are created lazily stay null, so the next update creates them again.Recover after the device comes back
BorderPrimitive:finalize()clearedm_needsUpdatebefore drawing, so a layer that got no surface counted as drawn and stayed missing until props, layout or theme changed.SetBorderLayerPropertiesCommonnow returnsfalseonly when the surface couldn't be created (nothing to draw still counts as success).SetBorderLayerPropertiesandDrawAllBorderLayerspass that result up,TryUpdateSpecialBorderLayersreports it through a new out-parameter, andfinalize()keepsm_needsUpdateset so the nextfinalizeredraws the border.ImageComponentView: once the final image response has arrived, nothing callsensureDrawingSurface()again, so a failed surface left the image blank. It now retries on a UI timer created withTimer::Create(so a custom timer factory is respected). The delay grows by 250 ms per attempt (250 ms, 500 ms, ...) for up to 10 attempts, with one timer at a time. A successful surface orprepareForRecyclecancels the timer and resets the count, and theTickhandler holds the view weakly and stops the timer if the view is gone.Screenshots
N/A
Changelog
Should this change be included in the release notes: yes
Don't terminate the app when a drawing surface can't be created because the graphics device was lost or reset.
CreateDrawingSurfaceBrushnow returns null, callers skip drawing, and borders and images redraw once the device is available again.Microsoft Reviewers: Open in CodeFlow