cuongphphanoi / mongoose

Automatically exported from code.google.com/p/mongoose
MIT License
0 stars 0 forks source link

Win32 pthread_cond_wait does not reacquire the mutex #187

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Mongoose version 2.11.

The code for pthread_cond_wait looks like this:

static int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mutex) {
  HANDLE handles[] = {cv->signal, cv->broadcast};
  ReleaseMutex(*mutex);
  WaitForMultipleObjects(2, handles, FALSE, INFINITE);
  return ReleaseMutex(*mutex) == 0 ? -1 : 0;
}

The last line should reacquire the mutex, not release it for a second time. 
This results in completely unprotected access to the socket queue between 
worker and master threads.

Proposed replacement line:

  return WaitForSingleObject(*mutex, INFINITE) == WAIT_OBJECT_0? 0 : -1;

Original issue reported on code.google.com by alex.mac...@gmail.com on 13 Oct 2010 at 10:37

GoogleCodeExporter commented 9 years ago
This really seems to be a problem.
I am trying to trace down a rare bug happening on Windows only, which seems a 
typical case of memory invasion/corruption: "An operation was attempted on 
something that is not a socket".
Perhaps this issue is the cause of it, but I cannot say for sure.

Original comment by rodrigo...@gmail.com on 27 Oct 2010 at 12:18

GoogleCodeExporter commented 9 years ago
I had this problem too and found the bug reported here.

Under heavy load with ultra small requests this is easy to trigger, and the 
above fix seems to have solved the issue.

Is it not still possible that a deadlock happens between WaitForMultipleObjects 
and WaitForSingleObject?

Even if it could happen, its much more rare now.

Original comment by term...@gmail.com on 13 Nov 2010 at 2:01

GoogleCodeExporter commented 9 years ago
Just an update: after fixing this API, we have been running without the 
mysterious bug for two weeks now. We used to have problems once a day before 
the fix.

Original comment by rodrigo...@gmail.com on 16 Nov 2010 at 11:51

GoogleCodeExporter commented 9 years ago
That was fixed in 
http://code.google.com/p/mongoose/source/detail?r=995a586a8e313124060baf52a581f9
379a49cb63&path=/mongoose.c, thanks for reporting.

Original comment by valenok on 21 Jun 2011 at 9:43