Skip to content

Commit 2210080

Browse files
agnerspull[bot]
authored andcommitted
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
1 parent b01bb81 commit 2210080

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/controller/python/chip/ChipReplStartup.py

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import argparse
1313
import builtins
1414
import chip.FabricAdmin
15+
import atexit
1516

1617
_fabricAdmins = None
1718

@@ -98,6 +99,12 @@ def ReplInit(debug):
9899
logging.getLogger().setLevel(logging.WARN)
99100

100101

102+
def StackShutdown():
103+
chip.FabricAdmin.FabricAdmin.ShutdownAll()
104+
ChipDeviceCtrl.ChipDeviceController.ShutdownAll()
105+
builtins.chipStack.Shutdown()
106+
107+
101108
def matterhelp(classOrObj=None):
102109
if (classOrObj is None):
103110
inspect(builtins.devCtrl, methods=True, help=True, private=False)
@@ -135,5 +142,7 @@ def mattersetdebug(enableDebugMode: bool = True):
135142

136143
builtins.devCtrl = devCtrl
137144

145+
atexit.register(StackShutdown)
146+
138147
console.print(
139148
'\n\n[blue]Default CHIP Device Controller has been initialized to manage [bold red]fabricAdmins[0][blue], and is available as [bold red]devCtrl')

src/controller/python/chip/ChipStack.py

+3
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ def setLogFunct(self, logFunct):
318318
self._ChipStackLib.pychip_Stack_SetLogFunct(logFunct)
319319

320320
def Shutdown(self):
321+
# Make sure PersistentStorage is destructed before chipStack
322+
# to avoid accessing builtins.chipStack after destruction.
323+
self._persistentStorage = None
321324
self.Call(lambda: self._ChipStackLib.pychip_Stack_Shutdown())
322325
self.networkLock = None
323326
self.completeEvent = None

0 commit comments

Comments
 (0)