public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Ken Brown <kbrown@cornell.edu>
To: cygwin@cygwin.com
Subject: Re: (call-process ...) hangs in emacs
Date: Tue, 05 Aug 2014 17:55:00 -0000	[thread overview]
Message-ID: <53E11A93.9070800@cornell.edu> (raw)
In-Reply-To: <20140805135830.GA9994@calimero.vinschen.de>

[-- Attachment #1: Type: text/plain, Size: 4399 bytes --]

On 8/5/2014 9:58 AM, Corinna Vinschen wrote:
> On Aug  5 08:21, Ken Brown wrote:
>> On 8/4/2014 9:45 AM, Corinna Vinschen wrote:
>>> ...this, and the fact that fork/exec (vfork == fork on Cygwin) still
>>> works nicely in other scenarios points to some problem with the usage of
>>> pthread_mutexes in the application may be the culprit.
>>>
>>> For instance, is it possible that emacs expects the pthread_mutexes
>>> in malloc to be ERRORCHECK mutexes?  What if you explicitely set
>>> them to ERRORCHECK at creation time?
>>
>> That doesn't seem to be the issue, but I think I did find the problem, and
>> it looks like there might be both an emacs bug and a Cygwin bug. Here's the
>> relevant code from emacs's gmalloc.c:
>>
>> pthread_mutex_t _malloc_mutex = PTHREAD_MUTEX_INITIALIZER;
>> pthread_mutex_t _aligned_blocks_mutex = PTHREAD_MUTEX_INITIALIZER;
>>
>> [...]
>>
>>    /* Some pthread implementations call malloc for statically
>>       initialized mutexes when they are used first.  To avoid such a
>>       situation, we initialize mutexes here while their use is
>>       disabled in malloc etc.  */
>>    pthread_mutex_init (&_malloc_mutex, NULL);
>>    pthread_mutex_init (&_aligned_blocks_mutex, NULL);
>>
>>
>> The pthread_mutexes are initialized twice, resulting in undefined behavior
>> according to Posix.  That's the emacs bug.
>
> That's not the problem.  It's not necessary to call pthread_mutex_init
> on statically initialized mutexes, but it doesn't hurt either.  Only
> when calling pthread_mutex_init twice on the same object it goes
> downhill, especially when the first incarnation of the mutex was already
> locked.
>
>> But simply removing the static
>> initialization doesn't fix the problem.  On the other hand, the following
>> patch does seem to fix it, at least in preliminary testing:
>>
>> === modified file 'src/gmalloc.c'
>> --- src/gmalloc.c       2014-03-04 19:02:49 +0000
>> +++ src/gmalloc.c       2014-08-05 01:35:38 +0000
>> @@ -490,8 +490,8 @@
>>   }
>>
>>   #ifdef USE_PTHREAD
>> -pthread_mutex_t _malloc_mutex = PTHREAD_MUTEX_INITIALIZER;
>> -pthread_mutex_t _aligned_blocks_mutex = PTHREAD_MUTEX_INITIALIZER;
>> +pthread_mutex_t _malloc_mutex;
>> +pthread_mutex_t _aligned_blocks_mutex;
>>   int _malloc_thread_enabled_p;
>>
>>   static void
>> @@ -526,8 +526,11 @@
>>        initialized mutexes when they are used first.  To avoid such a
>>        situation, we initialize mutexes here while their use is
>>        disabled in malloc etc.  */
>> -  pthread_mutex_init (&_malloc_mutex, NULL);
>> -  pthread_mutex_init (&_aligned_blocks_mutex, NULL);
>> +  pthread_mutexattr_t attr1, attr2;
>> +  pthread_mutexattr_settype (&attr1, PTHREAD_MUTEX_NORMAL);
>> +  pthread_mutexattr_settype (&attr2, PTHREAD_MUTEX_NORMAL);
>> +  pthread_mutex_init (&_malloc_mutex, &attr1);
>> +  pthread_mutex_init (&_aligned_blocks_mutex, &attr2);
>>     pthread_atfork (malloc_atfork_handler_prepare,
>>                    malloc_atfork_handler_parent,
>>                    malloc_atfork_handler_child);
>>
>>
>> The first hunk avoids the double initialization, but I don't understand why
>> the second hunk does anything.  Since PTHREAD_MUTEX_NORMAL is now the
>> default, shouldn't calling pthread_mutex_init with NULL second argument be
>> equivalent to my calls to pthread_mutexattr_settype?  Does this indicate a
>> Cygwin bug, or am I misunderstanding something?
>
> AFAICS you're missing something.  Your pthread_mutexattr_t attr1, attr2
> are not initialized.  They contain some random values, thus they are not
> good objects.  The calls to pthread_mutexattr_settype as well as the
> calls to pthread_mutex_init will fail with EINVAL, but you won't see it
> due to missing error handling, and you end up without mutexes at all.
> If you call pthread_mutexattr_init before calling
> pthread_mutexattr_settype the situation shoul;d be the same as before.

Thanks for catching my mistake.  Your earlier suggestion about 
explicitly setting the pthread_mutexes to be ERRORCHECK mutexes seems to 
fix the problem (as long as I remember to call pthread_mutexattr_init). 
  The revised patch is attached.  I went back to using both the static 
and dynamic initializations as in the original code, since you said 
that's harmless.

Angelo and Katsumi, could you test it and see if it solves the problems 
you reported?  If so, I'll issue new emacs releases.

Ken

[-- Attachment #2: pthread_mutex.patch --]
[-- Type: text/plain, Size: 1248 bytes --]

=== modified file 'src/gmalloc.c'
--- src/gmalloc.c	2014-03-04 19:02:49 +0000
+++ src/gmalloc.c	2014-08-05 17:30:18 +0000
@@ -490,8 +490,8 @@
 }
 
 #ifdef USE_PTHREAD
-pthread_mutex_t _malloc_mutex = PTHREAD_MUTEX_INITIALIZER;
-pthread_mutex_t _aligned_blocks_mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t _malloc_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
+pthread_mutex_t _aligned_blocks_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
 int _malloc_thread_enabled_p;
 
 static void
@@ -526,8 +526,13 @@
      initialized mutexes when they are used first.  To avoid such a
      situation, we initialize mutexes here while their use is
      disabled in malloc etc.  */
-  pthread_mutex_init (&_malloc_mutex, NULL);
-  pthread_mutex_init (&_aligned_blocks_mutex, NULL);
+  pthread_mutexattr_t attr1, attr2;
+  pthread_mutexattr_init (&attr1);
+  pthread_mutexattr_init (&attr2);
+  pthread_mutexattr_settype (&attr1, PTHREAD_MUTEX_ERRORCHECK);
+  pthread_mutexattr_settype (&attr2, PTHREAD_MUTEX_ERRORCHECK);
+  pthread_mutex_init (&_malloc_mutex, &attr1);
+  pthread_mutex_init (&_aligned_blocks_mutex, &attr2);
   pthread_atfork (malloc_atfork_handler_prepare,
 		  malloc_atfork_handler_parent,
 		  malloc_atfork_handler_child);


[-- Attachment #3: Type: text/plain, Size: 218 bytes --]

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

  reply	other threads:[~2014-08-05 17:55 UTC|newest]

Thread overview: 84+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-01 12:51 Angelo Graziosi
2014-08-01 13:17 ` Peter Hull
2014-08-01 13:32   ` Corinna Vinschen
2014-08-04  1:03     ` Ken Brown
2014-08-04  8:00       ` Corinna Vinschen
2014-08-04 13:34         ` Ken Brown
2014-08-04 13:45           ` Corinna Vinschen
2014-08-05 12:21             ` Ken Brown
2014-08-05 13:33               ` Peter Hull
2014-08-05 13:59                 ` Peter Hull
2014-08-05 13:58               ` Corinna Vinschen
2014-08-05 17:55                 ` Ken Brown [this message]
2014-08-05 18:40                   ` Corinna Vinschen
2014-08-07 11:52                     ` Ken Brown
2014-08-07 12:51                       ` Corinna Vinschen
2014-08-07 18:54                         ` Ken Brown
2014-08-07 15:30                       ` Eric Blake
2014-08-07 18:54                         ` Ken Brown
2014-08-07 21:42                           ` Eric Blake
2014-08-08 13:27                             ` Ken Brown
2014-08-08 15:39                               ` Peter Hull
2014-08-09  1:38                                 ` Ken Brown
2014-08-18 12:28                               ` Ken Brown
2014-08-18 14:58                                 ` Peter Hull
2014-08-18 15:03                                   ` Larry Hall (Cygwin)
2014-08-25 19:00                                 ` Ken Brown
2014-08-26  9:13                                   ` Peter Hull
2014-08-26 18:55                                   ` Achim Gratz
2014-08-26 22:13                                     ` Ken Brown
2014-08-27  8:42                                       ` Corinna Vinschen
2014-08-27 12:53                                         ` Ken Brown
2014-08-27 13:47                                           ` Corinna Vinschen
2014-08-27 14:40                                             ` Eric Blake
2014-08-27 17:15                                               ` Ken Brown
2014-08-27 15:15                                           ` Achim Gratz
2014-08-28  7:25                                             ` Achim Gratz
2014-08-28  9:55                                               ` Corinna Vinschen
2014-08-28 13:18                                                 ` Corinna Vinschen
2014-08-28 15:04                                                   ` Achim Gratz
2014-08-28 15:10                                                     ` Corinna Vinschen
2014-08-28 15:27                                                   ` Achim Gratz
2014-08-29  9:59                                                     ` Achim Gratz
2014-08-29 11:09                                                       ` Corinna Vinschen
2014-08-29 18:08                                                         ` Ken Brown
2014-08-29 19:23                                                           ` Achim Gratz
2014-08-29 19:36                                                             ` Ken Brown
2014-08-29 20:00                                                               ` Achim Gratz
2014-08-29 21:38                                                                 ` Ken Brown
2014-08-29 20:05                                                               ` Andrey Repin
2014-08-29 21:43                                                               ` Corinna Vinschen
2014-08-29 23:35                                                                 ` Andrey Repin
2014-09-01 11:47                                                                   ` Corinna Vinschen
2014-09-01 11:57                                                       ` Corinna Vinschen
2014-09-01 17:38                                                         ` Achim Gratz
2014-09-02  8:32                                                           ` Corinna Vinschen
2014-09-02 17:29                                                             ` Achim Gratz
2014-09-02 19:19                                                               ` Corinna Vinschen
2014-09-02 19:42                                                                 ` Achim Gratz
2014-09-02 20:09                                                                   ` Corinna Vinschen
2014-09-02 20:23                                                                     ` Achim Gratz
2014-09-03 13:04                                                                       ` Corinna Vinschen
2014-09-03 17:59                                                                         ` Achim Gratz
2014-08-28 10:34                                             ` Eric Blake
2014-08-27 21:05                                         ` Andrey Repin
2014-08-28 10:01                                           ` Corinna Vinschen
2014-08-28 13:35                                             ` Andrey Repin
2014-08-28 14:10                                               ` Corinna Vinschen
2014-08-28 17:05                                                 ` ACL behavior in Cygwin // " Andrey Repin
2014-08-28 18:29                                                   ` Achim Gratz
2014-08-29  8:29                                                   ` Corinna Vinschen
2014-08-28 18:38                                                 ` Achim Gratz
2014-08-28 19:50                                                   ` Andrey Repin
2014-08-06  2:30                   ` Katsumi Yamaoka
2014-08-06  8:48                     ` Corinna Vinschen
2014-08-06 23:41                       ` Katsumi Yamaoka
2014-08-07  0:35                         ` Andrey Repin
2014-08-04  8:05       ` Peter Hull
2014-08-04 13:36         ` Ken Brown
  -- strict thread matches above, loose matches on Subject: below --
2014-08-06  0:15 Angelo Graziosi
2014-07-31 14:51 Peter Hull
2014-07-31 17:35 ` Ken Brown
2014-08-01  7:36 ` Peter Hull
2014-08-01 10:22 ` Katsumi Yamaoka
2014-08-01 11:33   ` Peter Hull

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=53E11A93.9070800@cornell.edu \
    --to=kbrown@cornell.edu \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).