You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Spurned by failures discovered in #20178, I did an investigation into the Python threading model. As I understood how it worked, it became readily apparent that it has a fairly broken threading model:
The main Matter thread got spun up implicitly anytime any Python module attempted to get a handle to the underlying ChipDeviceCtrl.so to invoke a C++ function. This happened in chip.native.GetLibraryHandle() - that would call the C++ pychip_native_init, which then did the pthread creation. This made it highly in-deterministic and un-coordinated as to when the Matter thread would actually spool up relative to other core stack initialization that had to happen first.
Specifically, the failure seen in #20178 occurred because ChipReplStartup.py would first attempt to setup logging by calling chip.logging.RedirectToPythonLogging(), which attempted to get a handle to the shared library, which caused the Matter thread to spin-up.
It's only after that that we'd initialize ChipStack, which would actually do the core job of initializing the stack (which included initializing DeviceControllerFactory. It did not do this on the Matter thread, and called the accompanying C++ function directly, triggering the failure seen in #20178.
The actual creation of the Matter thread happened in chip/native/StackInit.cpp::pychip_native_init. This however, did not use the PlatformMgr APIs. Instead, it directly instantiated a pthread and pumped the event loop from there. This evaded the checks in IsChipStackLockedByCurrentThread because mMainLoopStarted would never get set to true forever, effectively making it look like the Matter thread never started. Assert that controllers are inited and shut down with the Matter lock held. #20178 switching to using mHasValidChipTask now correctly triggers this, because mHasValidChipTask is set to true inside _RunEventLoop, and not EventLoopTaskMain (which the Python C++ code never used).
There were 3 different 'stack init' APIs in Python - the one in StackInit.cpp, and two other ones in ChipDeviceController-ScriptBinding.cpp. The latter two fragmented the logic of stack initialization.
The text was updated successfully, but these errors were encountered:
Spurned by failures discovered in #20178, I did an investigation into the Python threading model. As I understood how it worked, it became readily apparent that it has a fairly broken threading model:
ChipDeviceCtrl.so
to invoke a C++ function. This happened inchip.native.GetLibraryHandle()
- that would call the C++pychip_native_init
, which then did the pthread creation. This made it highly in-deterministic and un-coordinated as to when the Matter thread would actually spool up relative to other core stack initialization that had to happen first.Specifically, the failure seen in #20178 occurred because
ChipReplStartup.py
would first attempt to setup logging by callingchip.logging.RedirectToPythonLogging()
, which attempted to get a handle to the shared library, which caused the Matter thread to spin-up.It's only after that that we'd initialize
ChipStack
, which would actually do the core job of initializing the stack (which included initializingDeviceControllerFactory
. It did not do this on the Matter thread, and called the accompanying C++ function directly, triggering the failure seen in #20178.The actual creation of the Matter thread happened in
chip/native/StackInit.cpp::pychip_native_init
. This however, did not use thePlatformMgr
APIs. Instead, it directly instantiated a pthread and pumped the event loop from there. This evaded the checks inIsChipStackLockedByCurrentThread
becausemMainLoopStarted
would never get set to true forever, effectively making it look like the Matter thread never started. Assert that controllers are inited and shut down with the Matter lock held. #20178 switching to usingmHasValidChipTask
now correctly triggers this, becausemHasValidChipTask
is set to true inside_RunEventLoop
, and notEventLoopTaskMain
(which the Python C++ code never used).There were 3 different 'stack init' APIs in Python - the one in
StackInit.cpp
, and two other ones inChipDeviceController-ScriptBinding.cpp
. The latter two fragmented the logic of stack initialization.The text was updated successfully, but these errors were encountered: