From 2210080197ec5bda563ef7ac4194a638e9e0af10 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Thu, 19 May 2022 16:47:03 +0200 Subject: [PATCH] Shutdown Python REPL correctly (#18437) * Python REPL: Shutdown CHIP stack properly * Make sure Storage is deallocated before ChipStack is Shutdown Otherwise, we later fail to access the builtins.chipStack: AttributeError: module 'builtins' has no attribute 'chipStack' * Add comment why we need to explictily set _persistentStorage to None --- src/controller/python/chip/ChipReplStartup.py | 9 +++++++++ src/controller/python/chip/ChipStack.py | 3 +++ 2 files changed, 12 insertions(+) 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