Skip to content

Commit 0d68788

Browse files
authored
Stop main event loop when shell prompt exits (project-chip#36433)
* Stop main event loop when shell prompt exits * Do not call platform manager shutdown twice * Prevent double free in case Shutdown is called twice
1 parent 18c0788 commit 0d68788

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

examples/fabric-sync/shell/ShellCommands.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ void RegisterCommands()
216216

217217
// Register the root `device` command with the top-level shell.
218218
Engine::Root().RegisterCommands(&sDeviceComand, 1);
219-
return;
220219
}
221220

222221
} // namespace Shell

examples/platform/linux/AppMain.cpp

+22-13
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,19 @@ void EventHandler(const DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
301301
}
302302
}
303303

304+
void StopMainEventLoop()
305+
{
306+
if (gMainLoopImplementation != nullptr)
307+
{
308+
gMainLoopImplementation->SignalSafeStopMainLoop();
309+
}
310+
else
311+
{
312+
Server::GetInstance().GenerateShutDownEvent();
313+
PlatformMgr().ScheduleWork([](intptr_t) { PlatformMgr().StopEventLoopTask(); });
314+
}
315+
}
316+
304317
void Cleanup()
305318
{
306319
#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED
@@ -317,17 +330,9 @@ void Cleanup()
317330
// We should stop using signals for those faults, and move to a different notification
318331
// means, like a pipe. (see issue #19114)
319332
#if !defined(ENABLE_CHIP_SHELL)
320-
void StopSignalHandler(int signal)
333+
void StopSignalHandler(int /* signal */)
321334
{
322-
if (gMainLoopImplementation != nullptr)
323-
{
324-
gMainLoopImplementation->SignalSafeStopMainLoop();
325-
}
326-
else
327-
{
328-
Server::GetInstance().GenerateShutDownEvent();
329-
PlatformMgr().ScheduleWork([](intptr_t) { PlatformMgr().StopEventLoopTask(); });
330-
}
335+
StopMainEventLoop();
331336
}
332337
#endif // !defined(ENABLE_CHIP_SHELL)
333338

@@ -530,7 +535,10 @@ void ChipLinuxAppMainLoop(AppMainLoopImplementation * impl)
530535

531536
#if defined(ENABLE_CHIP_SHELL)
532537
Engine::Root().Init();
533-
std::thread shellThread([]() { Engine::Root().RunMainLoop(); });
538+
std::thread shellThread([]() {
539+
Engine::Root().RunMainLoop();
540+
StopMainEventLoop();
541+
});
534542
Shell::RegisterCommissioneeCommands();
535543
#endif
536544
initParams.operationalServicePort = CHIP_PORT;
@@ -692,14 +700,15 @@ void ChipLinuxAppMainLoop(AppMainLoopImplementation * impl)
692700
Server::GetInstance().Shutdown();
693701

694702
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
703+
// Commissioner shutdown call shuts down entire stack, including the platform manager.
695704
ShutdownCommissioner();
705+
#else
706+
DeviceLayer::PlatformMgr().Shutdown();
696707
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
697708

698709
#if ENABLE_TRACING
699710
tracing_setup.StopTracing();
700711
#endif
701712

702-
DeviceLayer::PlatformMgr().Shutdown();
703-
704713
Cleanup();
705714
}

src/controller/CHIPDeviceControllerSystemState.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class DeviceControllerSystemState
201201
//
202202
// The stack will shut down when all references are released.
203203
//
204-
// NB: The system state is owned by the factory; Relase() will not free it
204+
// NB: The system state is owned by the factory; Release() will not free it
205205
// but will free its members (Shutdown()).
206206
//
207207
// Returns true if the system state was shut down in response to this call.

src/platform/Linux/PlatformManagerImpl.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,13 @@ void PlatformManagerImpl::_Shutdown()
274274
Internal::GenericPlatformManagerImpl_POSIX<PlatformManagerImpl>::_Shutdown();
275275

276276
#if CHIP_DEVICE_CONFIG_WITH_GLIB_MAIN_LOOP
277-
g_main_loop_quit(mGLibMainLoop);
278-
g_thread_join(mGLibMainLoopThread);
279-
g_main_loop_unref(mGLibMainLoop);
277+
if (mGLibMainLoop != nullptr)
278+
{
279+
g_main_loop_quit(mGLibMainLoop);
280+
g_thread_join(mGLibMainLoopThread);
281+
g_main_loop_unref(mGLibMainLoop);
282+
mGLibMainLoop = nullptr;
283+
}
280284
#endif
281285
}
282286

0 commit comments

Comments
 (0)