Skip to content

Commit

Permalink
Move call to setThreadName()
Browse files Browse the repository at this point in the history
The call to setThreadName() is moved to avoid a race condition that
can happen with very short lived processes. If the process terminates
very quickly e.g. is a google test runner or the msi.exe command
called from a Makefile during a build, then very occasionally a
crash can occur during process termination if setThreadName() when called
from the newly created thread. This looks to be becauae the DLL it is
trying to call gets unloaded between it getting a handle to the DLL
and making the call. Moving the setThreadName() call to the creating
thread avoids this problem. The issue was only ever seen with statically
linked epics executables, I am unsure if the way a DLL based epics
program unloads might avoid this, or just make it less likely but
still possible. As mentioned above, the issue will only ever occur
to threads that are created during process termination and so would
not affect running IOCs
  • Loading branch information
FreddieAkeroyd authored and anjohnson committed Feb 21, 2024
1 parent 4383cf2 commit 4720b61
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 3 deletions.
3 changes: 1 addition & 2 deletions modules/libcom/src/osi/os/WIN32/osdThread.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,6 @@ static unsigned WINAPI epicsWin32ThreadEntry ( LPVOID lpParameter )
BOOL success;

if ( pGbl ) {
setThreadName ( pParm->id, pParm->pName );

success = FlsSetValue ( pGbl->flsIndexThreadLibraryEPICS, pParm );
if ( success ) {
osdThreadHooksRun ( ( epicsThreadId ) pParm );
Expand Down Expand Up @@ -659,6 +657,7 @@ epicsThreadId epicsThreadCreateOpt (
pParmWIN32->id = ( DWORD ) threadId ;
}

setThreadName ( pParmWIN32->id, pParmWIN32->pName );
osdPriority = epicsThreadGetOsdPriorityValue (opts->priority);
bstat = SetThreadPriority ( pParmWIN32->handle, osdPriority );
if (!bstat) {
Expand Down
2 changes: 1 addition & 1 deletion modules/libcom/src/osi/os/WIN32/osdTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ void currentTime :: startPLL ()
CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION,
& this->threadId );
assert ( this->threadHandle );
setThreadName ( this->threadId, "EPICS Time PLL" );
BOOL bstat = SetThreadPriority (
this->threadHandle, THREAD_PRIORITY_HIGHEST );
assert ( bstat );
Expand Down Expand Up @@ -496,7 +497,6 @@ static unsigned __stdcall _pllThreadEntry ( void * pCurrentTimeIn )
{
currentTime * pCT =
reinterpret_cast < currentTime * > ( pCurrentTimeIn );
setThreadName ( pCT->threadId, "EPICS Time PLL" );
while ( ! pCT->threadShutdownCmd ) {
Sleep ( currentTime :: pllDelay * 1000 /* mS */ );
pCT->updatePLL ();
Expand Down

0 comments on commit 4720b61

Please sign in to comment.