Skip to content

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
microsoft:mainfrom
Virtual-Fulfillment-Technologies-Inc:vendora/fix-drawing-surface-noexcept
Open

Gordon MacMaster (gmacmaster) wants to merge 1 commit into
microsoft:mainfrom
Virtual-Fulfillment-Technologies-Inc:vendora/fix-drawing-surface-noexcept

Conversation

@gmacmaster

@gmacmaster Gordon MacMaster (gmacmaster) commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Why

CompContext::CreateDrawingSurfaceBrush (both the Windows and Microsoft type redirects) is noexcept, but it calls CompositionGraphicsDevice::CreateDrawingSurface, which throws when the graphics device is lost or being reset (display change, remote desktop session, driver reset). The exception can't leave a noexcept function, so std::terminate aborts the app.

We hit this in production on two Windows POS registers running react-native-windows 0.84.0. The crashing stacks were:

ReactNativeIsland::ScaleFactor -> Arrange -> layout -> BorderPrimitive
  -> CreateDrawingSurfaceBrush -> throw_hresult -> abort

and the same path through react-native-svg's SvgView::Invalidate.

What

Stop the crash

  • CreateDrawingSurfaceBrush (both overloads) catches winrt::hresult_error and returns nullptr instead of terminating.
  • AutoDrawDrawingSurface (AutoDraw.h) accepts a null surface and leaves the render target null, so GetRenderTarget() / IsValid() report nothing to draw. This also protects external callers such as react-native-svg.
  • Every in-tree caller skips drawing when the surface is null: BorderPrimitive, ImageComponentView (and DrawImage), the ReactNativeIsland loading bar, ScrollView arrows, WindowsTextInput, UnimplementedNativeViewComponentView (still runs the base layout update), DebuggerUIIsland, and UriImageManager. Surfaces that are created lazily stay null, so the next update creates them again.

Recover after the device comes back

  • BorderPrimitive: finalize() cleared m_needsUpdate before drawing, so a layer that got no surface counted as drawn and stayed missing until props, layout or theme changed. SetBorderLayerPropertiesCommon now returns false only when the surface couldn't be created (nothing to draw still counts as success). SetBorderLayerProperties and DrawAllBorderLayers pass that result up, TryUpdateSpecialBorderLayers reports it through a new out-parameter, and finalize() keeps m_needsUpdate set so the next finalize redraws the border.
  • ImageComponentView: once the final image response has arrived, nothing calls ensureDrawingSurface() again, so a failed surface left the image blank. It now retries on a UI timer created with Timer::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 or prepareForRecycle cancels the timer and resets the count, and the Tick handler 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. CreateDrawingSurfaceBrush now returns null, callers skip drawing, and borders and images redraw once the device is available again.

Microsoft Reviewers: Open in CodeFlow

* 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>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 02:27
@azure-pipelines

Copy link
Copy Markdown
Contributor
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants