Skip to content

Commit

Permalink
Shutdown Python REPL correctly (#18437)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
agners authored and pull[bot] committed Oct 13, 2023
1 parent b01bb81 commit 2210080
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/controller/python/chip/ChipReplStartup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import argparse
import builtins
import chip.FabricAdmin
import atexit

_fabricAdmins = None

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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')
3 changes: 3 additions & 0 deletions src/controller/python/chip/ChipStack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2210080

Please sign in to comment.