Skip to content

Fix crashes during document loading and improve error handling#12

Merged
JaviLendi merged 3 commits into
masterfrom
UI-Improvements
May 21, 2026
Merged

Fix crashes during document loading and improve error handling#12
JaviLendi merged 3 commits into
masterfrom
UI-Improvements

Conversation

@JaviLendi

Copy link
Copy Markdown
Owner

This pull request addresses stability issues in the document loading workflow, specifically targeting crashes related to window and tab management during rapid document operations. The changes add additional safety checks and clarify the rationale for asynchronous settings saving.

Crash prevention and stability improvements:

  • Added checks in LoadDocumentFinish to ensure that the MainWindow is still valid and not in the process of closing before proceeding with further operations, preventing potential crashes during rapid document or tab transitions. [1] [2]

Documentation and code clarity:

  • Improved the comment above SaveSettingsVoid to clarify that asynchronous settings saving is necessary to avoid crashes caused by dangling pointers to tab controls during rapid DDE (Dynamic Data Exchange) opens and hooks.

kjk and others added 3 commits May 20, 2026 17:30
The EXCEPTION_ACCESS_VIOLATION in SetSidebarVisibility+0x48
(ReplaceDocumentInCurrentTab) was a use-after-free on MainWindow*.

For command-line (or startup) loads we use the sync path:
  CreateAndShowMainWindow (window visible) ->
  CreateControllerForEngineOrFile (can take tens of seconds for
    large/complex EPUBs on slow CPUs) ->
  LoadDocumentFinish / Replace... / SetSidebarVisibility

If the user closes the (frozen) window, WM_CLOSE is queued.
During tab insertion, RedrawAll, Show/UpdateWindow etc. in the
finish path the close can be processed synchronously via
DestroyWindow, which calls DeleteMainWindow and tears down the
win/tabs/DisplayModel while the load code is still on the stack.

Added IsMainWindowValid(win) && !win->isBeingClosed guards at the
entry to ReplaceDocumentInCurrentTab, after tab creation, before/after
the Replace call, and immediately before SetSidebarVisibility.
This follows the existing pattern in LoadModelIntoTab and the
async load path (LoadDocumentAsyncFinish).

The original report was crash 86.227.233.173 (EPUB, 327 pages,
~38s load, Win10 on Pentium N4200).

No ailog.txt to commit.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Defer the SaveSettings() call that happens at the end of
LoadDocumentFinish() by posting it via uitask::Post. This prevents
SaveSettings() (which walks all tabs and calls tab->ctrl->GetDisplayState)
from running while other documents are still being loaded or closed
via DDE command storm.

As a defensive measure, snapshot the list of tabs before iterating
in SaveSettings(), since the call can now happen asynchronously
relative to tab open/close operations.

This addresses crashes such as:
https://www.sumatrapdfreader.org/crash/2026-05-20/89bcb2fbc000001.txt

The AV occurred in UpdateTabFileDisplayStateForTab (line 567) with
a garbage tab->ctrl pointer (e.g. FFFFFFFF000000A1) under heavy load
combined with 3rd-party window hooks (MarkAny Document SAFER +
GridWndHookLM64.dll).

Also ran clang-format.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Applied fixes from sumatrapdfreader:master commits 032a973 and fcf280b:
- fix crash when user closes window during slow initial document load
  with IsMainWindowValid() guards at key points in ReplaceDocumentInCurrentTab
- Fix crash with dangling tab->ctrl during rapid document loading
  by deferring SaveSettings() via uitask::Post() for async-safe operation

Integrated UI-Improvements improvements:
- ReplaceDocumentInCurrentTab now returns bool for error signaling
- Proper cleanup of args->ctrl on early-return paths to prevent leaks
- Addressed Copilot review feedback on resource safety and re-entrancy
Copilot AI review requested due to automatic review settings May 21, 2026 11:22
@JaviLendi
JaviLendi merged commit 333db89 into master May 21, 2026
3 of 4 checks passed
@JaviLendi
JaviLendi removed the request for review from Copilot May 21, 2026 11:42
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