Hello all,
My Driver Unload routine is creating threads( PsCreateSystemThread ) and after creating the thread it waits for an event to be fired by the newly created thread before it proceeds with unloading. So in Pseudo code:
VOID myUnload( )
{
PsCreateSystemThread();
KeWaitForSingleObject( );
}
There is more to it than just but that’s the general idea. The thread routine that I ‘execute’ with the created system thread will fire the event that we wait for and terminate its own thread with PsTerminateSystemThread.
VOID myThreadFunc( )
{
// Do something
KeSetEvent ( );
PsTerminateSystemThread ( 0 );
}
Now this normally runs fine, but every now and then I get a bugcheck telling me that the driver didn’t terminate all of it’s threads etc.
I assume that this happens because KeSetEvent() is called before PsTerminateSystemThread, and as soon as this is called the ‘main’ thread continues unloading, which may be faster than the PsTerminateSystemThread.
Is that idea about right? If so, should I include some sort of sleep after my KeWaitForSingleObject? Or are more correct solutions to ensure that all threads are terminated before unloading.
Thank you for your help.