diff --git a/src/controller/python/chip/ChipReplStartup.py b/src/controller/python/chip/ChipReplStartup.py index 861d07ca8a7c9b..efdf8ea4cb3b6b 100644 --- a/src/controller/python/chip/ChipReplStartup.py +++ b/src/controller/python/chip/ChipReplStartup.py @@ -12,6 +12,7 @@ import argparse import builtins import chip.FabricAdmin +import atexit _fabricAdmins = None @@ -98,6 +99,12 @@ def ReplInit(debug): logging.getLogger().setLevel(logging.WARN) +def StackShutdown(): + chip.FabricAdmin.FabricAdmin.ShutdownAll() + ChipDeviceCtrl.ChipDeviceController.ShutdownAll() + builtins.chipStack.Shutdown() + + def matterhelp(classOrObj=None): if (classOrObj is None): inspect(builtins.devCtrl, methods=True, help=True, private=False) @@ -135,5 +142,7 @@ def mattersetdebug(enableDebugMode: bool = True): builtins.devCtrl = devCtrl +atexit.register(StackShutdown) + console.print( '\n\n[blue]Default CHIP Device Controller has been initialized to manage [bold red]fabricAdmins[0][blue], and is available as [bold red]devCtrl') diff --git a/src/controller/python/chip/ChipStack.py b/src/controller/python/chip/ChipStack.py index 47c5ef43ce14c8..666d1192f7a9f8 100644 --- a/src/controller/python/chip/ChipStack.py +++ b/src/controller/python/chip/ChipStack.py @@ -318,6 +318,9 @@ def setLogFunct(self, logFunct): self._ChipStackLib.pychip_Stack_SetLogFunct(logFunct) def Shutdown(self): + # Make sure PersistentStorage is destructed before chipStack + # to avoid accessing builtins.chipStack after destruction. + self._persistentStorage = None self.Call(lambda: self._ChipStackLib.pychip_Stack_Shutdown()) self.networkLock = None self.completeEvent = None