##// END OF EJS Templates
IPython/core/history.py: Handle RuntimeError on Thread.start (#14318)...
M Bussonnier -
r28625:0a3cf8f6 merge
parent child Browse files
Show More
@@ -6,15 +6,12 b''
6
6
7 import atexit
7 import atexit
8 import datetime
8 import datetime
9 from pathlib import Path
10 import re
9 import re
11 import sqlite3
10 import sqlite3
12 import threading
11 import threading
12 from pathlib import Path
13
13
14 from traitlets.config.configurable import LoggingConfigurable
15 from decorator import decorator
14 from decorator import decorator
16 from IPython.utils.decorators import undoc
17 from IPython.paths import locate_profile
18 from traitlets import (
15 from traitlets import (
19 Any,
16 Any,
20 Bool,
17 Bool,
@@ -22,12 +19,16 b' from traitlets import ('
22 Instance,
19 Instance,
23 Integer,
20 Integer,
24 List,
21 List,
22 TraitError,
25 Unicode,
23 Unicode,
26 Union,
24 Union,
27 TraitError,
28 default,
25 default,
29 observe,
26 observe,
30 )
27 )
28 from traitlets.config.configurable import LoggingConfigurable
29
30 from IPython.paths import locate_profile
31 from IPython.utils.decorators import undoc
31
32
32 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
33 # Classes and functions
34 # Classes and functions
@@ -554,7 +555,14 b' class HistoryManager(HistoryAccessor):'
554
555
555 if self.enabled and self.hist_file != ':memory:':
556 if self.enabled and self.hist_file != ':memory:':
556 self.save_thread = HistorySavingThread(self)
557 self.save_thread = HistorySavingThread(self)
557 self.save_thread.start()
558 try:
559 self.save_thread.start()
560 except RuntimeError:
561 self.log.error(
562 "Failed to start history saving thread. History will not be saved.",
563 exc_info=True,
564 )
565 self.hist_file = ":memory:"
558
566
559 def _get_hist_file_name(self, profile=None):
567 def _get_hist_file_name(self, profile=None):
560 """Get default history file name based on the Shell's profile.
568 """Get default history file name based on the Shell's profile.
@@ -880,10 +888,10 b' class HistorySavingThread(threading.Thread):'
880 super(HistorySavingThread, self).__init__(name="IPythonHistorySavingThread")
888 super(HistorySavingThread, self).__init__(name="IPythonHistorySavingThread")
881 self.history_manager = history_manager
889 self.history_manager = history_manager
882 self.enabled = history_manager.enabled
890 self.enabled = history_manager.enabled
883 atexit.register(self.stop)
884
891
885 @only_when_enabled
892 @only_when_enabled
886 def run(self):
893 def run(self):
894 atexit.register(self.stop)
887 # We need a separate db connection per thread:
895 # We need a separate db connection per thread:
888 try:
896 try:
889 self.db = sqlite3.connect(
897 self.db = sqlite3.connect(
@@ -900,6 +908,8 b' class HistorySavingThread(threading.Thread):'
900 except Exception as e:
908 except Exception as e:
901 print(("The history saving thread hit an unexpected error (%s)."
909 print(("The history saving thread hit an unexpected error (%s)."
902 "History will not be written to the database.") % repr(e))
910 "History will not be written to the database.") % repr(e))
911 finally:
912 atexit.unregister(self.stop)
903
913
904 def stop(self):
914 def stop(self):
905 """This can be called from the main thread to safely stop this thread.
915 """This can be called from the main thread to safely stop this thread.
General Comments 0
You need to be logged in to leave comments. Login now