Skip to content

Cleaner closing#89

Open
krish-vj wants to merge 1 commit into
FOSSEE:masterfrom
krish-vj:master
Open

Cleaner closing#89
krish-vj wants to merge 1 commit into
FOSSEE:masterfrom
krish-vj:master

Conversation

@krish-vj

Copy link
Copy Markdown
Contributor

PR Description: Fix Simulator Window Transition Hangs, C++ Object Deletion Crashes, and Windows Console Unicode Errors

Summary

This PR addresses several critical stability issues occurring when transitioning from the simulator/flowsheet workspace back to the landing page, as well as console encoding crashes on Windows platforms.

Identified Issues & Root Causes

1. "Not Responding" / Abrupt Freezes on Window Transition

  • Root Cause: When closing the MainApp (flowsheet) window, the landing page previously called deleteLater() on the MainApp instance. Because MainApp owns complex sub-components, dock widgets, simulation threads, and scene items, calling deleteLater() schedules asynchronous C++ destruction. This destruction races with the Qt event loop. When the landing page restores, repaints, and handles focus, it triggers queries or events on objects whose C++ counterparts have already been deleted, resulting in Windows "Not Responding" hangs or abrupt segmentation faults.

2. Cursor Stack Pollution & Corrupted Interaction

  • Root Cause: Override cursors (like WaitCursor or PointingHandCursor) set during simulation execution or drag-and-drop operations were not fully drained from Qt's cursor stack upon window closure. The polluted cursor stack caused mouse-tracking and hover events to fail on the restored Landing Page, making it seem locked or unresponsive.

3. Unicode Console Encoding Crashes on Windows

  • Root Cause: Several print() statements in Container.py and undo_manager.py used the unicode right-arrow character . On Windows machines running standard non-UTF-8 console codepages (e.g. CP1252), this caused uncaught UnicodeEncodeError exceptions, causing background routines to crash.

Solutions & Implementation Details

1. Safe Window Stashing (No deleteLater())

  • Modified Landing_page.py to hide the closed MainApp window and stash it in a module-level list _stashed_windows instead of deleting it. Keeping the window alive but hidden completely bypasses C++ destruction issues and races, with negligible memory impact.
  • Swapped out unstable closeEvent monkey-patching wrapper with a standard PyQt closed signal connection.

2. Robust Thread & Cursor Cleanup

  • Implemented a clean closeEvent in mainApp.py that:
    • Safely stops any running simulation thread (self.thrd).
    • Drains the QApplication override cursor stack.
    • Emits the closed signal and accepts the event.
  • Added a cursor draining loop in the landing page's restore_landing_page method to ensure a clean cursor stack state before showing the landing page.
  • Added try-except RuntimeError guards in dock widget cleanup to handle instances where underlying C++ objects may have been freed elsewhere.

3. ASCII Arrow Replacements for Console Prints

  • Replaced all unicode characters with ASCII -> in Container.py and undo_manager.py.
  • Stripped BOM headers from modified files to prevent encoding errors on older Python interpreters.

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