Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e602694

Browse files
committed
Register a single classmethod to stop threads for all HistoryManager instances
1 parent add29df commit e602694

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

IPython/core/history.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,6 @@ def __init__(
703703
self.hist_file = ":memory:"
704704
else:
705705
self.using_thread = True
706-
if hasattr(os, "register_at_fork"):
707-
os.register_at_fork(before=self._stop_thread)
708706
self._instances.add(self)
709707
assert len(HistoryManager._instances) <= HistoryManager._max_inst, (
710708
len(HistoryManager._instances),
@@ -715,11 +713,13 @@ def __del__(self) -> None:
715713
if self.save_thread is not None:
716714
self.save_thread.stop()
717715

718-
def _stop_thread(self) -> None:
716+
@classmethod
717+
def _stop_thread(cls) -> None:
719718
# Used before forking so the thread isn't running at fork
720-
if self.save_thread is not None:
721-
self.save_thread.stop()
722-
self.save_thread = None
719+
for inst in cls._instances:
720+
if inst.save_thread is not None:
721+
inst.save_thread.stop()
722+
inst.save_thread = None
723723

724724
def _restart_thread_if_stopped(self) -> None:
725725
# Start the thread again after it was stopped for forking
@@ -1081,6 +1081,10 @@ def writeout_cache(self, conn: Optional[sqlite3.Connection] = None) -> None:
10811081
self.db_output_cache = []
10821082

10831083

1084+
if hasattr(os, "register_at_fork"):
1085+
os.register_at_fork(before=HistoryManager._stop_thread)
1086+
1087+
10841088
from collections.abc import Callable, Iterator
10851089
from weakref import ReferenceType
10861090

0 commit comments

Comments
 (0)