@@ -467,26 +467,55 @@ def close(self):
467467 if self .is_closed ():
468468 return
469469
470+ # the following code places try/catch around possibly failing
471+ # operations for safety and to guard against implementation
472+ # difference in the QT bindings.
473+
470474 self .__log_debug ("Closing event loop..." )
475+ # Catch exceptions for safety between bindings.
476+ poller = getattr (self , "_ProactorEventLoop__event_poller" , None )
477+ if poller is not None :
478+ try :
479+ poller .stop ()
480+ except Exception :
481+ pass
471482 if self .__default_executor is not None :
472483 self .__default_executor .shutdown ()
473484
485+ # Disconnect thread-safe signaller and schedule deletion of helper QObjects
474486 if self .__call_soon_signal :
475- self .__call_soon_signal .disconnect ()
476-
477- super ().close ()
487+ try :
488+ self .__call_soon_signal .disconnect ()
489+ except Exception :
490+ pass
491+ if self .__call_soon_signaller is not None :
492+ self .__call_soon_signaller .deleteLater ()
478493
479- self ._timer .stop ()
480- self .__app = None
494+ # Stop timers first to avoid late invocations during teardown
495+ try :
496+ self ._timer .stop ()
497+ except Exception :
498+ pass
499+ self ._timer .deleteLater ()
481500
501+ # Disable and disconnect any remaining notifiers before closing
482502 for notifier in itertools .chain (
483503 self ._read_notifiers .values (), self ._write_notifiers .values ()
484504 ):
485505 notifier .setEnabled (False )
486- notifier .activated ["int" ].disconnect ()
506+ try :
507+ notifier .activated ["int" ].disconnect ()
508+ except Exception :
509+ pass
510+ notifier .deleteLater ()
511+
512+ self ._read_notifiers .clear ()
513+ self ._write_notifiers .clear ()
487514
488- self ._read_notifiers = None
489- self ._write_notifiers = None
515+ super ().close ()
516+
517+ # Finally, clear app reference
518+ self .__app = None
490519
491520 def call_later (self , delay , callback , * args , context = None ):
492521 """Register callback to be invoked after a certain delay."""
0 commit comments