BobVul / IdleShutdown

6 stars 2 forks source link

Race condition with mutexes and without a persistent instance. #3

Open BobVul opened 9 years ago

BobVul commented 9 years ago

Where two processes are started in quick succession without a persistent instance available, a race condition is possible where one (A) holds the mutex (very briefly) while the other (B) attempts to take it. B will proceed to send a message even though no persistent instance exists.

This race condition is exceptionally rare. And I'm not entirely sure how to fix it.

BobVul commented 9 years ago

Is there a better way than a mutex to check if a persistent instance is running?

Or perhaps I need to lock (another mutex! this feels ugly) around the check for the mutex, with the difference being that this lock would wait until it became available (WaitOne() instead of WaitOne(0)).

BobVul commented 9 years ago

There's actually another race condition: the persistent instance could die between the mutex check and sending the message.

I suppose the real solution is to abandon mutexes entirely and simply try to use the named pipe and see if it works.