Skip to content

Commit

Permalink
Properly terminate linux example apps (followup from #24979) (#25053)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple authored and pull[bot] committed Jul 18, 2023
1 parent 94e07bd commit b69d807
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 47 deletions.
43 changes: 0 additions & 43 deletions examples/all-clusters-app/linux/main-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include <new>
#include <platform/DiagnosticDataProvider.h>
#include <platform/PlatformManager.h>
#include <signal.h>
#include <system/SystemPacketBuffer.h>
#include <transport/SessionManager.h>
#include <transport/raw/PeerAddress.h>
Expand Down Expand Up @@ -61,44 +60,6 @@ NamedPipeCommands sChipNamedPipeCommands;
AllClustersCommandDelegate sAllClustersCommandDelegate;
chip::app::Clusters::WindowCovering::WindowCoveringManager sWindowCoveringManager;

// TODO(#20664) REPL test will fail if signal SIGINT is not caught, temporarily keep following logic.

// when the shell is enabled, don't intercept signals since it prevents the user from
// using expected commands like CTRL-C to quit the application. (see issue #17845)
// We should stop using signals for those faults, and move to a different notification
// means, like a pipe. (see issue #19114)
#if !defined(ENABLE_CHIP_SHELL)
void OnRebootSignalHandler(int signum)
{
ChipLogDetail(DeviceLayer, "Caught signal %d", signum);

// The BootReason attribute SHALL indicate the reason for the Node’s most recent boot, the real usecase
// for this attribute is embedded system. In Linux simulation, we use different signals to tell the current
// running process to terminate with different reasons.
BootReasonType bootReason = BootReasonType::kUnspecified;
switch (signum)
{
case SIGINT:
bootReason = BootReasonType::kSoftwareReset;
break;
default:
IgnoreUnusedVariable(bootReason);
ChipLogError(NotSpecified, "Unhandled signal: Should never happens");
chipDie();
break;
}

Server::GetInstance().DispatchShutDownAndStopEventLoop();
}

void SetupSignalHandlers()
{
// sigaction is not used here because Tsan interceptors seems to
// never dispatch the signals on darwin.
signal(SIGINT, OnRebootSignalHandler);
}
#endif // !defined(ENABLE_CHIP_SHELL)

} // namespace

bool emberAfBasicClusterMfgSpecificPingCallback(chip::app::CommandHandler * commandObj)
Expand Down Expand Up @@ -187,10 +148,6 @@ Clusters::NetworkCommissioning::Instance sEthernetNetworkCommissioningInstance(k

void ApplicationInit()
{
#if !defined(ENABLE_CHIP_SHELL)
SetupSignalHandlers();
#endif // !defined(ENABLE_CHIP_SHELL)

(void) kNetworkCommissioningEndpointMain;
// Enable secondary endpoint only when we need it, this should be applied to all platforms.
emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false);
Expand Down
5 changes: 3 additions & 2 deletions examples/lighting-app/linux/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <Options.h> // examples/platform/linux/Options.h
#include <app/server/OnboardingCodesUtil.h>
#include <app/server/Server.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/PlatformManager.h>
Expand Down Expand Up @@ -373,12 +374,12 @@ void UiLoop()
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
{
chip::DeviceLayer::PlatformMgr().StopEventLoopTask();
chip::Server::GetInstance().DispatchShutDownAndStopEventLoop();
}
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE &&
event.window.windowID == SDL_GetWindowID(window))
{
chip::DeviceLayer::PlatformMgr().StopEventLoopTask();
chip::Server::GetInstance().DispatchShutDownAndStopEventLoop();
}
}

Expand Down
14 changes: 12 additions & 2 deletions examples/platform/linux/AppMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,18 @@ void Cleanup()
// TODO(16968): Lifecycle management of storage-using components like GroupDataProvider, etc
}

// TODO(#20664) REPL test will fail if signal SIGINT is not caught, temporarily keep following logic.

// when the shell is enabled, don't intercept signals since it prevents the user from
// using expected commands like CTRL-C to quit the application. (see issue #17845)
// We should stop using signals for those faults, and move to a different notification
// means, like a pipe. (see issue #19114)
#if !defined(ENABLE_CHIP_SHELL)
void StopSignalHandler(int signal)
{
DeviceLayer::PlatformMgr().StopEventLoopTask();
Server::GetInstance().DispatchShutDownAndStopEventLoop();
}
#endif // !defined(ENABLE_CHIP_SHELL)

} // namespace

Expand Down Expand Up @@ -394,8 +402,10 @@ void ChipLinuxAppMainLoop()

ApplicationInit();

signal(SIGTERM, StopSignalHandler);
#if !defined(ENABLE_CHIP_SHELL)
signal(SIGINT, StopSignalHandler);
signal(SIGTERM, StopSignalHandler);
#endif // !defined(ENABLE_CHIP_SHELL)
DeviceLayer::PlatformMgr().RunEventLoop();

#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
Expand Down

0 comments on commit b69d807

Please sign in to comment.