Skip to content

Fixed deadlock on macOS & added filtering of redundant layout requests#1913

Open
janiselfert wants to merge 3 commits into
DavBfr:masterfrom
coneno:master
Open

Fixed deadlock on macOS & added filtering of redundant layout requests#1913
janiselfert wants to merge 3 commits into
DavBfr:masterfrom
coneno:master

Conversation

@janiselfert

@janiselfert janiselfert commented Feb 9, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR fixes a critical deadlock issue in macOS printing that prevented the print dialog from working correctly in dynamic layout mode, prevents redundant onLayout calls and fixes problems with multiple windows.

Addressed issues:

1865
1875

Problem

The original implementation used DispatchSemaphore.wait() to block the main thread while waiting for the PDF document from Flutter. However, this caused a deadlock on newer Flutter versions which use merged threads (see here).

Solution

Replaced the semaphore-based blocking with RunLoop polling:

  • Uses RunLoop.run(mode:before:) to keep the main thread responsive
  • Allows Flutter Platform Channel to deliver the PDF document while "waiting"
  • Includes a 30-second timeout as a safety measure
  • The main thread processes events in 0.1s intervals until the document arrives

Additional Changes:

Layout Parameter Caching:

  • macOS calls knowsPageRange() multiple times during the print dialog lifecycle with identical parameters
  • Added caching of layout parameters to prevent redundant onLayout() calls

Shared PrintJobs between plugin instances:

  • Each window creates a new PrintingPlugin instance, overwriting the singleton instance variable.
  • Scenario: Jobs registered in Window 1 were stored in old instance, but setDocument() looked in new instance (Window 2) → job not found → 30s timeout
  • Changed from instance-specific jobs dictionary to static sharedJobs list shared across all instances with NSLock to prevent errors caused by concurrent dictionary edits from different instances.
  • Made jobID start at a random int, so overlaps between Plugin instances are unlikely
  • This should not affect single instance scenarios, but fixes problems with multiple instances.

@janiselfert janiselfert marked this pull request as draft February 10, 2026 17:43
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.

1 participant