Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 975a4f9

Browse files
Use synchmanager worker thread to handle process termination requests.
1 parent 5aa1bfe commit 975a4f9

File tree

7 files changed

+112
-245
lines changed

7 files changed

+112
-245
lines changed

src/pal/inc/pal.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,9 @@ typedef long time_t;
480480
#define PAL_INITIALIZE_SYNC_THREAD 0x01
481481
#define PAL_INITIALIZE_EXEC_ALLOCATOR 0x02
482482
#define PAL_INITIALIZE_STD_HANDLES 0x04
483-
#define PAL_INITIALIZE_SIGNAL_THREAD 0x08
484483

485484
// PAL_Initialize() flags
486-
#define PAL_INITIALIZE (PAL_INITIALIZE_SYNC_THREAD | PAL_INITIALIZE_STD_HANDLES | PAL_INITIALIZE_SIGNAL_THREAD)
485+
#define PAL_INITIALIZE (PAL_INITIALIZE_SYNC_THREAD | PAL_INITIALIZE_STD_HANDLES)
487486

488487
// PAL_InitializeDLL() flags - don't start any of the helper threads
489488
#define PAL_INITIALIZE_DLL PAL_INITIALIZE_NONE

src/pal/src/exception/seh.cpp

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,6 @@ PHARDWARE_EXCEPTION_SAFETY_CHECK_FUNCTION g_safeExceptionCheckFunction = NULL;
5757

5858
PGET_GCMARKER_EXCEPTION_CODE g_getGcMarkerExceptionCode = NULL;
5959

60-
PTERMINATION_REQUEST_HANDLER g_terminationRequestHandler = nullptr;
61-
62-
/* Internal function declarations *********************************************/
63-
64-
#if !HAVE_MACH_EXCEPTIONS
65-
PAL_ERROR
66-
StartExternalSignalHandlerThread(
67-
CPalThread *pthr);
68-
#endif // !HAVE_MACH_EXCEPTIONS
69-
7060
/* Internal function definitions **********************************************/
7161

7262
/*++
@@ -93,17 +83,6 @@ SEHInitialize (CPalThread *pthrCurrent, DWORD flags)
9383
SEHCleanup();
9484
return FALSE;
9585
}
96-
97-
if (flags & PAL_INITIALIZE_SIGNAL_THREAD)
98-
{
99-
PAL_ERROR palError = StartExternalSignalHandlerThread(pthrCurrent);
100-
if (palError != NO_ERROR)
101-
{
102-
ERROR("StartExternalSignalHandlerThread returned %d\n", palError);
103-
SEHCleanup();
104-
return FALSE;
105-
}
106-
}
10786
#endif
10887

10988
return TRUE;
@@ -155,26 +134,6 @@ PAL_SetHardwareExceptionHandler(
155134
g_safeExceptionCheckFunction = exceptionCheckFunction;
156135
}
157136

158-
/*++
159-
Function:
160-
PAL_SetTerminationRequestHandler
161-
162-
Register a termination request handler.
163-
164-
Parameters:
165-
terminationHandler - handler for termination request
166-
167-
Return value:
168-
None
169-
--*/
170-
VOID
171-
PALAPI
172-
PAL_SetTerminationRequestHandler(
173-
IN PTERMINATION_REQUEST_HANDLER terminationHandler)
174-
{
175-
g_terminationRequestHandler = terminationHandler;
176-
}
177-
178137
/*++
179138
Function:
180139
PAL_SetGetGcMarkerExceptionCode
@@ -294,15 +253,6 @@ SEHProcessException(PEXCEPTION_POINTERS pointers)
294253
// Unhandled hardware exception pointers->ExceptionRecord->ExceptionCode at pointers->ExceptionRecord->ExceptionAddress
295254
}
296255

297-
VOID
298-
SEHHandleTerminationRequest()
299-
{
300-
if (g_terminationRequestHandler != NULL)
301-
{
302-
g_terminationRequestHandler();
303-
}
304-
}
305-
306256
/*++
307257
Function :
308258
SEHEnable

src/pal/src/exception/signal.cpp

Lines changed: 6 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,6 @@ struct sigaction g_previous_sigint;
9494
struct sigaction g_previous_sigquit;
9595
struct sigaction g_previous_sigterm;
9696

97-
// Pipe used for sending signal notifications to a helper thread
98-
int g_signalPipe[2] = { 0, 0 };
99-
100-
DWORD g_dwExternalSignalHandlerThreadId = 0;
101-
10297
/* public function definitions ************************************************/
10398

10499
/*++
@@ -137,6 +132,7 @@ BOOL SEHInitializeSignals()
137132
handle_signal(SIGSEGV, sigsegv_handler, &g_previous_sigsegv);
138133
handle_signal(SIGINT, sigint_handler, &g_previous_sigint);
139134
handle_signal(SIGQUIT, sigquit_handler, &g_previous_sigquit);
135+
handle_signal(SIGTERM, sigterm_handler, &g_previous_sigterm);
140136

141137
#ifdef INJECT_ACTIVATION_SIGNAL
142138
handle_signal(INJECT_ACTIVATION_SIGNAL, inject_activation_handler, NULL);
@@ -184,13 +180,7 @@ void SEHCleanupSignals()
184180
restore_signal(SIGSEGV, &g_previous_sigsegv);
185181
restore_signal(SIGINT, &g_previous_sigint);
186182
restore_signal(SIGQUIT, &g_previous_sigquit);
187-
188-
// Only restore if the signal handler thread was started and
189-
// the previous handler was saved.
190-
if (g_dwExternalSignalHandlerThreadId != 0)
191-
{
192-
restore_signal(SIGTERM, &g_previous_sigterm);
193-
}
183+
restore_signal(SIGTERM, &g_previous_sigterm);
194184
}
195185

196186
/* internal function definitions **********************************************/
@@ -397,34 +387,6 @@ static void sigquit_handler(int code, siginfo_t *siginfo, void *context)
397387
kill(gPID, code);
398388
}
399389

400-
/*++
401-
Function :
402-
HandleExternalSignal
403-
404-
Write to a pipe to kick off handling of the signal.
405-
406-
Parameters :
407-
signalCode - code of the external signal
408-
409-
(no return value)
410-
--*/
411-
static void HandleExternalSignal(int signalCode)
412-
{
413-
BYTE signalCodeByte = (BYTE)signalCode;
414-
ssize_t writtenBytes;
415-
do
416-
{
417-
writtenBytes = write(g_signalPipe[1], &signalCodeByte, 1);
418-
}
419-
while ((writtenBytes == -1) && (errno == EINTR));
420-
421-
if (writtenBytes == -1)
422-
{
423-
// Fatal error
424-
abort();
425-
}
426-
}
427-
428390
/*++
429391
Function :
430392
sigterm_handler
@@ -440,7 +402,10 @@ static void sigterm_handler(int code, siginfo_t *siginfo, void *context)
440402
{
441403
if (PALIsInitialized())
442404
{
443-
HandleExternalSignal(code);
405+
// g_pSynchronizationManager shouldn't be null if PAL is initialized.
406+
_ASSERTE(g_pSynchronizationManager != nullptr);
407+
408+
g_pSynchronizationManager->SendTerminationRequestToWorkerThread();
444409
}
445410
else
446411
{
@@ -693,111 +658,4 @@ void restore_signal(int signal_id, struct sigaction *previousAction)
693658
}
694659
}
695660

696-
static
697-
DWORD
698-
PALAPI
699-
ExternalSignalHandlerThreadRoutine(
700-
PVOID
701-
);
702-
703-
PAL_ERROR
704-
StartExternalSignalHandlerThread(
705-
CPalThread *pthr)
706-
{
707-
PAL_ERROR palError = NO_ERROR;
708-
709-
#ifndef DO_NOT_USE_SIGNAL_HANDLING_THREAD
710-
HANDLE hThread;
711-
712-
if (pipe(g_signalPipe) != 0)
713-
{
714-
palError = ERROR_CANNOT_MAKE;
715-
goto done;
716-
}
717-
718-
palError = InternalCreateThread(
719-
pthr,
720-
NULL,
721-
0,
722-
ExternalSignalHandlerThreadRoutine,
723-
NULL,
724-
0,
725-
SignalHandlerThread, // Want no_suspend variant
726-
&g_dwExternalSignalHandlerThreadId,
727-
&hThread
728-
);
729-
730-
if (palError != NO_ERROR)
731-
{
732-
ERROR("Failure creating external signal handler thread (%d)\n", palError);
733-
goto done;
734-
}
735-
736-
InternalCloseHandle(pthr, hThread);
737-
738-
handle_signal(SIGTERM, sigterm_handler, &g_previous_sigterm);
739-
#endif // DO_NOT_USE_SIGNAL_HANDLING_THREAD
740-
741-
done:
742-
743-
return palError;
744-
}
745-
746-
static
747-
DWORD
748-
PALAPI
749-
ExternalSignalHandlerThreadRoutine(
750-
PVOID
751-
)
752-
{
753-
DWORD dwThreadId;
754-
bool fContinue = TRUE;
755-
HANDLE hThread;
756-
PAL_ERROR palError = NO_ERROR;
757-
CPalThread *pthr = InternalGetCurrentThread();
758-
759-
//
760-
// Wait for a signal to occur
761-
//
762-
763-
while (fContinue)
764-
{
765-
BYTE signalCode;
766-
ssize_t bytesRead;
767-
768-
do
769-
{
770-
bytesRead = read(g_signalPipe[0], &signalCode, 1);
771-
}
772-
while ((bytesRead == -1) && (errno == EINTR));
773-
774-
if (bytesRead == -1)
775-
{
776-
// Fatal error
777-
abort();
778-
}
779-
780-
switch (signalCode)
781-
{
782-
case SIGTERM:
783-
{
784-
SEHHandleTerminationRequest();
785-
}
786-
787-
default:
788-
ASSERT("Unexpected signal %d in signal thread\n", signalCode);
789-
abort();
790-
break;
791-
}
792-
}
793-
794-
//
795-
// Perform an immediate (non-graceful) shutdown
796-
//
797-
798-
_exit(EXIT_FAILURE);
799-
800-
return 0;
801-
}
802-
803661
#endif // !HAVE_MACH_EXCEPTIONS

src/pal/src/include/pal/corunix.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,10 @@ namespace CorUnix
11241124
CPalThread *pThread
11251125
) = 0;
11261126

1127+
virtual
1128+
PAL_ERROR
1129+
SendTerminationRequestToWorkerThread() = 0;
1130+
11271131
//
11281132
// This routine is primarily meant for use by WaitForMultipleObjects[Ex].
11291133
// The caller must individually release each of the returned controller

src/pal/src/include/pal/seh.hpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,6 @@ Return value:
7474
VOID
7575
SEHProcessException(PEXCEPTION_POINTERS pointers);
7676

77-
/*++
78-
Function:
79-
SEHHandleTerminationRequest
80-
81-
Send a process termination request to a registered handler.
82-
83-
Parameters:
84-
None
85-
--*/
86-
VOID
87-
SEHHandleTerminationRequest();
88-
8977
#if !HAVE_MACH_EXCEPTIONS
9078
// TODO: Implement for Mach exceptions. Not in CoreCLR surface area.
9179
/*++

0 commit comments

Comments
 (0)