Skip to content

Commit 8d2d6cc

Browse files
authored
Merge pull request #595 from rdkcentral/development/R2-shutdown-fix
[SHUTDOWN] Be more verbose on shuting down in case of an failure!
2 parents 1f8d96e + 226d1fe commit 8d2d6cc

File tree

1 file changed

+116
-42
lines changed

1 file changed

+116
-42
lines changed

Source/WPEFramework/PluginHost.cpp

Lines changed: 116 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace WPEFramework {
3030
static PluginHost::Config* _config = nullptr;
3131
static PluginHost::Server* _dispatcher = nullptr;
3232
static bool _background = false;
33+
static bool _atExitActive = true;
3334

3435
namespace PluginHost {
3536

@@ -125,88 +126,126 @@ namespace PluginHost {
125126

126127
class ExitHandler : public Core::Thread {
127128
private:
129+
ExitHandler() = delete;
128130
ExitHandler(const ExitHandler&) = delete;
129131
ExitHandler& operator=(const ExitHandler&) = delete;
130132

131-
public:
132-
ExitHandler()
133+
ExitHandler(PluginHost::Server* destructor)
133134
: Core::Thread(Core::Thread::DefaultStackSize(), nullptr)
135+
, _destructor(destructor)
134136
{
137+
ASSERT(_destructor != nullptr);
135138
}
136-
virtual ~ExitHandler()
139+
140+
public:
141+
~ExitHandler() override
137142
{
138143
Stop();
139144
Wait(Core::Thread::STOPPED, Core::infinite);
140145
}
141146

142-
static void Construct() {
147+
static void StartShutdown() {
143148
_adminLock.Lock();
144-
if (_instance == nullptr) {
145-
_instance = new WPEFramework::PluginHost::ExitHandler();
149+
if ((_dispatcher != nullptr) && (_instance == nullptr)) {
150+
_instance = new WPEFramework::PluginHost::ExitHandler(_dispatcher);
151+
_dispatcher = nullptr;
146152
_instance->Run();
147153
}
148154
_adminLock.Unlock();
149155
}
150156
static void Destruct() {
157+
151158
_adminLock.Lock();
152-
if (_instance != nullptr) {
153-
delete _instance; //It will wait till the worker execution completed
159+
160+
if (_instance == nullptr) {
161+
162+
PluginHost::Server* destructor = _dispatcher;
163+
164+
_dispatcher = nullptr;
165+
166+
_adminLock.Unlock();
167+
168+
if (destructor != nullptr) {
169+
CloseDown (destructor);
170+
}
171+
}
172+
else {
173+
ExitHandler* destructor = _instance;
154174
_instance = nullptr;
155-
} else {
156-
CloseDown();
175+
176+
_adminLock.Unlock();
177+
178+
delete destructor; //It will wait till the worker execution completed
157179
}
158-
_adminLock.Unlock();
159180
}
160181

161182
private:
162183
virtual uint32_t Worker() override
163184
{
164-
CloseDown();
185+
CloseDown(_destructor);
165186
Block();
166187
return (Core::infinite);
167188
}
168-
static void CloseDown()
189+
static void CloseDown(PluginHost::Server* destructor)
169190
{
170-
TRACE_L1("Entering @Exit. Cleaning up process: %d.", Core::ProcessInfo().Id());
171-
172-
if (_dispatcher != nullptr) {
173-
PluginHost::Server* destructor = _dispatcher;
174-
destructor->Close();
175-
_dispatcher = nullptr;
176-
delete destructor;
177-
178-
delete _config;
179-
_config = nullptr;
180-
181191
#ifndef __WINDOWS__
182-
if (_background) {
183-
syslog(LOG_NOTICE, EXPAND_AND_QUOTE(APPLICATION_NAME) " Daemon closed down.");
184-
} else
192+
if (_background) {
193+
syslog(LOG_NOTICE, EXPAND_AND_QUOTE(APPLICATION_NAME) " Daemon closing down.");
194+
} else
185195
#endif
186-
{
187-
fprintf(stdout, EXPAND_AND_QUOTE(APPLICATION_NAME) " closed down.\n");
188-
}
196+
{
197+
fprintf(stdout, EXPAND_AND_QUOTE(APPLICATION_NAME) " closing down.\n");
198+
fflush(stderr);
199+
}
200+
201+
destructor->Close();
202+
delete destructor;
203+
delete _config;
204+
_config = nullptr;
205+
189206

190207
#ifndef __WINDOWS__
191-
closelog();
208+
closelog();
192209
#endif
193210

194-
// Do not forget to close the Tracing stuff...
195-
Trace::TraceUnit::Instance().Close();
211+
// Do not forget to close the Tracing stuff...
212+
Trace::TraceUnit::Instance().Close();
196213

197214
#ifdef __CORE_WARNING_REPORTING__
198-
WarningReporting::WarningReportingUnit::Instance().Close();
215+
WarningReporting::WarningReportingUnit::Instance().Close();
199216
#endif
200217

201-
// Now clear all singeltons we created.
202-
Core::Singleton::Dispose();
218+
#ifndef __WINDOWS__
219+
if (_background) {
220+
syslog(LOG_NOTICE, EXPAND_AND_QUOTE(APPLICATION_NAME) " closing all created singletons.");
221+
} else
222+
#endif
223+
{
224+
fprintf(stdout, EXPAND_AND_QUOTE(APPLICATION_NAME) " closing all created singletons.\n");
225+
fflush(stderr);
203226
}
204227

205-
TRACE_L1("Leaving @Exit. Cleaning up process: %d.", Core::ProcessInfo().Id());
228+
229+
// Now clear all singeltons we created.
230+
Core::Singleton::Dispose();
231+
232+
#ifndef __WINDOWS__
233+
if (_background) {
234+
syslog(LOG_NOTICE, EXPAND_AND_QUOTE(APPLICATION_NAME) " completely stopped.");
235+
} else
236+
#endif
237+
{
238+
fprintf(stdout, EXPAND_AND_QUOTE(APPLICATION_NAME) " completely stopped.\n");
239+
fflush(stderr);
240+
}
241+
_atExitActive = false;
206242
}
207-
private:
208-
static ExitHandler* _instance;
209-
static Core::CriticalSection _adminLock;
243+
244+
private:
245+
PluginHost::Server* _destructor;
246+
247+
static ExitHandler* _instance;
248+
static Core::CriticalSection _adminLock;
210249
};
211250

212251
ExitHandler* ExitHandler::_instance = nullptr;
@@ -225,7 +264,15 @@ namespace PluginHost {
225264
}
226265

227266
if ((signo == SIGTERM) || (signo == SIGQUIT)) {
228-
ExitHandler::Construct();
267+
268+
if (_background) {
269+
syslog(LOG_NOTICE, EXPAND_AND_QUOTE(APPLICATION_NAME) " shutting down due to a SIGTERM or SIGQUIT signal. Regular shutdown\n");
270+
} else {
271+
fprintf(stderr, EXPAND_AND_QUOTE(APPLICATION_NAME) " shutting down due to a SIGTERM or SIGQUIT signal. No regular shutdown. Errors to follow are collateral damage errors !!!!!!");
272+
fflush(stderr);
273+
}
274+
275+
ExitHandler::StartShutdown();
229276
}
230277
}
231278

@@ -285,8 +332,29 @@ namespace PluginHost {
285332
}
286333
#endif
287334

335+
static void ForcedExit() {
336+
if (_atExitActive == true) {
337+
if (_background) {
338+
syslog(LOG_ERR, EXPAND_AND_QUOTE(APPLICATION_NAME) " shutting down due to an atexit request. No regular shutdown. Errors to follow are collateral damage errors !!!!!!");
339+
} else {
340+
fprintf(stderr, EXPAND_AND_QUOTE(APPLICATION_NAME) " shutting down due to an atexit request. No regular shutdown. Errors to follow are collateral damage errors !!!!!!");
341+
fflush(stderr);
342+
}
343+
ExitHandler::Destruct();
344+
}
345+
}
346+
288347
static void UncaughtExceptions () {
348+
if (_background) {
349+
syslog(LOG_ERR, EXPAND_AND_QUOTE(APPLICATION_NAME) " shutting down due to an uncaught exception. No regular shutdown. Errors to follow are collateral damage errors !!!!!!");
350+
} else {
351+
fprintf(stderr, EXPAND_AND_QUOTE(APPLICATION_NAME) " shutting down due to an uncaught exception. No regular shutdown. Errors to follow are collateral damage errors !!!!!!");
352+
fflush(stderr);
353+
}
354+
289355
Logging::DumpException(_T("General"));
356+
357+
ExitHandler::Destruct();
290358
}
291359

292360
#ifdef __WINDOWS__
@@ -305,7 +373,7 @@ namespace PluginHost {
305373

306374
ConsoleOptions options(argc, argv);
307375

308-
if (atexit(ExitHandler::Destruct) != 0) {
376+
if (atexit(ForcedExit) != 0) {
309377
TRACE_L1("Could not register @exit handler. Argc %d.", argc);
310378
ExitHandler::Destruct();
311379
exit(EXIT_FAILURE);
@@ -766,8 +834,14 @@ namespace PluginHost {
766834
}
767835
}
768836

837+
if (_background == false) {
838+
fprintf(stderr, EXPAND_AND_QUOTE(APPLICATION_NAME) " shutting down due to a 'Q' press in the terminal. Regular shutdown\n");
839+
fflush(stderr);
840+
}
841+
769842
ExitHandler::Destruct();
770843
std::set_terminate(nullptr);
844+
_atExitActive = false;
771845
return 0;
772846

773847
} // End main.

0 commit comments

Comments
 (0)