Disable main window close during open/save dialog


If I press the main window’s close button while a file chooser dialog is open, the main window stays open (as expected). But as soon as I close the file chooser, the entire app disappears immediately, which is a dangerous behavior.

How can I prevent this?