pthread_create hangs with PTHREAD_STACK_MIN attribute

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

pthread_create hangs with PTHREAD_STACK_MIN attribute

Botond Botyanszki-3
Hello,
The following code hangs randomly in pthread_create. I suppose there is a
race condition somewhere.

 System      : OpenBSD 3.9
 Architecture: OpenBSD.i386
 Machine     : i386

Steps to reproduce the problem:
Compile the following code and run it in a loop until it hangs.

---------------------------------------
#include <pthread.h>
#include <errno.h>
#define CHECK(f,p) switch(f) { case EPERM: if (p) exit(1); case 0: break; default: exit(1); break; }
void *foo(void *data)
{
    return NULL;
}
int
main ()
{

    pthread_t           thread;
    pthread_attr_t      attr;
    CHECK(pthread_attr_init(&attr), 1);
    CHECK(pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN), 1);
    CHECK(pthread_create(&thread, &attr, foo, NULL), 0);

  return 0;
}
---------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: pthread_create hangs with PTHREAD_STACK_MIN attribute

Otto Moerbeek
On Mon, 29 May 2006, Botond Botyanszki wrote:

> Hello,
> The following code hangs randomly in pthread_create. I suppose there is a
> race condition somewhere.
>
>  System      : OpenBSD 3.9
>  Architecture: OpenBSD.i386
>  Machine     : i386
>
> Steps to reproduce the problem:
> Compile the following code and run it in a loop until it hangs.
>
> ---------------------------------------
> #include <pthread.h>
> #include <errno.h>
> #define CHECK(f,p) switch(f) { case EPERM: if (p) exit(1); case 0: break; default: exit(1); break; }
> void *foo(void *data)
> {
>     return NULL;
> }
> int
> main ()
> {
>
>     pthread_t           thread;
>     pthread_attr_t      attr;
>     CHECK(pthread_attr_init(&attr), 1);
>     CHECK(pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN), 1);
>     CHECK(pthread_create(&thread, &attr, foo, NULL), 0);
>
>   return 0;
> }
> ---------------------------------------

I can reproduce this on MP i386. The hang occurs while processing the
scheduler signal (SIGPROF):

0x06cd6c64 in _thread_kern_sched (scp=0x816f304c)
    at /usr/src/lib/libpthread/uthread/uthread_kern.c:66
66      {
(gdb) bt
#0  0x06cd6c64 in _thread_kern_sched (scp=0x816f304c)
    at /usr/src/lib/libpthread/uthread/uthread_kern.c:66
#1  0x06cd82f5 in _thread_sig_handler (sig=27, info=0x816f309c, scp=0x816f304c)
    at /usr/src/lib/libpthread/uthread/uthread_sig.c:86
#2  <signal handler called>
#3  0x0184c519 in _dl_sigprocmask () from /usr/libexec/ld.so
#4  0x0184f3bc in _dl_bind () from /usr/libexec/ld.so
#5  0x0184c543 in _dl_bind_start () from /usr/libexec/ld.so
#6  0x8706223c in ?? ()

If I change PTHREAD_STACK_MIN to 2*PTHREAD_STACK_MIN, all runs fine.
Can you try the same?


        -Otto

Reply | Threaded
Open this post in threaded view
|

Re: pthread_create hangs with PTHREAD_STACK_MIN attribute

Botond Botyanszki-3
On Mon, 29 May 2006 20:54:13 +0200 (CEST)
Otto Moerbeek <[hidden email]> wrote:


> >     CHECK(pthread_attr_init(&attr), 1);
> >     CHECK(pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN), 1);
> >     CHECK(pthread_create(&thread, &attr, foo, NULL), 0);

>
> If I change PTHREAD_STACK_MIN to 2*PTHREAD_STACK_MIN, all runs fine.
> Can you try the same?

It runs fine with 2*PTHREAD_STACK_MIN here too, though I fail to see how
the stack size is related to the scheduler signal ;)

Reply | Threaded
Open this post in threaded view
|

Re: pthread_create hangs with PTHREAD_STACK_MIN attribute

Otto Moerbeek
On Mon, 29 May 2006, Botond Botyanszki wrote:

> On Mon, 29 May 2006 20:54:13 +0200 (CEST)
> Otto Moerbeek <[hidden email]> wrote:
>
>
> > >     CHECK(pthread_attr_init(&attr), 1);
> > >     CHECK(pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN), 1);
> > >     CHECK(pthread_create(&thread, &attr, foo, NULL), 0);
>
> >
> > If I change PTHREAD_STACK_MIN to 2*PTHREAD_STACK_MIN, all runs fine.
> > Can you try the same?
>
> It runs fine with 2*PTHREAD_STACK_MIN here too, though I fail to see how
> the stack size is related to the scheduler signal ;)

It propably depends on the exact moment the signal gets delivered.

I assume the signal handling causes a stack overflow. The results of
the overflow are quite hard to predict. On i386 it manifests itself as
a hang; maybe a return address has been overwritten, or something like
that.

In the meantime I also tested on amd64 MP, and there using
PTHREAD_STACK_MIN causes illegal instructions. Using
2*PTHREAD_STACK_MIN works fine on amd64 also.

        -Otto