Преглед изворни кода

fix #243: thread keep alive after session closed

wangweimin пре 3 година
родитељ
комит
e8ae3b2bd7
1 измењених фајлова са 12 додато и 1 уклоњено
  1. 12 1
      pywebio/io_ctrl.py

+ 12 - 1
pywebio/io_ctrl.py

@@ -79,6 +79,13 @@ class Output:
         self.custom_enter = None
         self.custom_exit = None
 
+        # Try to make sure current session exist.
+        # If we leave the session interaction in `Output.__del__`,
+        # the Exception raised from there will be ignored by python interpreter,
+        # thus we can't end some session in some cases.
+        # See also: https://github.com/pywebio/PyWebIO/issues/243
+        get_current_session()
+
     def enable_context_manager(self, container_selector=None, container_dom_id=None, custom_enter=None,
                                custom_exit=None):
         self.enabled_context_manager = True
@@ -157,7 +164,11 @@ class Output:
     def __del__(self):
         """返回值没有被变量接收时的操作:直接输出消息"""
         if not self.processed:
-            self.send()
+            # avoid `Exception ignored in xxx` error log
+            try:
+                self.send()
+            except Exception:
+                pass
 
 
 class OutputList(UserList):