2008-01-09 10:42:22

by tom

[permalink] [raw]
Subject: Possible 2.6.24-rc7 issue w/respect to pthreads

To Whom It May Concern,

After I patched my 2.6.23 kernel to 2.6.24-rc7 this morning, I noticed
some odd behavior with respect to POSIX threads in a test program I had
written (originally to test epoll.)

The behavior is as follows:

1. main() creates a new thread of execution with pthread_create
2. thread_func() immediately calls pthread_detach(), which is supposed to
ensure that thread resources are cleaned up when the thread terminates.
3. The spawned thread sleeps and then prints a message "got here"
4. The main thread calls pthread_join(). According to the POSIX
documentation, this should suspend execution until the spawned thread has
terminated.

What I'm seeing is that the main thread terminates immediately. If I
comment out the call to pthread_detach(), the program runs normally. If I
boot with my 2.6.23 kernel, the program runs normally.

I'm not sure if this is some oversight on my part but similar programs
operate correctly on my Debian etch installation with 2.6.18 as well as
with the newer 23 kernel, but not with the patch.

If you have any questions, don't hesitate to email me; my address is
provided below.

See Attached (bug.c)

Tom R. Dial
[email protected]


Attachments:
bug.c (1.50 kB)

2008-01-09 10:52:26

by Marc Kleine-Budde

[permalink] [raw]
Subject: Re: Possible 2.6.24-rc7 issue w/respect to pthreads

[email protected] wrote:
> To Whom It May Concern,
>
> After I patched my 2.6.23 kernel to 2.6.24-rc7 this morning, I noticed
> some odd behavior with respect to POSIX threads in a test program I had
> written (originally to test epoll.)
>
> The behavior is as follows:
>
> 1. main() creates a new thread of execution with pthread_create
> 2. thread_func() immediately calls pthread_detach(), which is supposed to
> ensure that thread resources are cleaned up when the thread terminates.
> 3. The spawned thread sleeps and then prints a message "got here"
> 4. The main thread calls pthread_join(). According to the POSIX
> documentation, this should suspend execution until the spawned thread has
> terminated.

Quoting the man page of pthread_detach():

After pthread_detach completes, subsequent attempts to perform
pthread_join on th will fail.

regard - Marc
--
Marc Kleine-Budde Phone: +49-231-2826-924
Pengutronix - Linux Solutions for Science and Industry
Vertretung West/Dortmund http://www.pengutronix.de


Attachments:
signature.asc (252.00 B)
OpenPGP digital signature

2008-01-09 10:53:19

by Jakub Jelinek

[permalink] [raw]
Subject: Re: Possible 2.6.24-rc7 issue w/respect to pthreads

On Wed, Jan 09, 2008 at 02:35:32AM -0800, [email protected] wrote:
> After I patched my 2.6.23 kernel to 2.6.24-rc7 this morning, I noticed
> some odd behavior with respect to POSIX threads in a test program I had
> written (originally to test epoll.)
>
> The behavior is as follows:
>
> 1. main() creates a new thread of execution with pthread_create
> 2. thread_func() immediately calls pthread_detach(), which is supposed to
> ensure that thread resources are cleaned up when the thread terminates.
> 3. The spawned thread sleeps and then prints a message "got here"
> 4. The main thread calls pthread_join(). According to the POSIX
> documentation, this should suspend execution until the spawned thread has
> terminated.

Your testcase is buggy. Detached threads aren't joinable, you can't call
pthread_join on them.

Jakub

2008-01-09 11:07:48

by Eric Dumazet

[permalink] [raw]
Subject: Re: Possible 2.6.24-rc7 issue w/respect to pthreads

On Wed, 9 Jan 2008 02:35:32 -0800 (PST)
[email protected] wrote:

> To Whom It May Concern,
>
> After I patched my 2.6.23 kernel to 2.6.24-rc7 this morning, I noticed
> some odd behavior with respect to POSIX threads in a test program I had
> written (originally to test epoll.)
>
> The behavior is as follows:
>
> 1. main() creates a new thread of execution with pthread_create
> 2. thread_func() immediately calls pthread_detach(), which is supposed to
> ensure that thread resources are cleaned up when the thread terminates.
> 3. The spawned thread sleeps and then prints a message "got here"
> 4. The main thread calls pthread_join(). According to the POSIX
> documentation, this should suspend execution until the spawned thread has
> terminated.
>
> What I'm seeing is that the main thread terminates immediately. If I
> comment out the call to pthread_detach(), the program runs normally. If I
> boot with my 2.6.23 kernel, the program runs normally.
>
> I'm not sure if this is some oversight on my part but similar programs
> operate correctly on my Debian etch installation with 2.6.18 as well as
> with the newer 23 kernel, but not with the patch.
>
> If you have any questions, don't hesitate to email me; my address is
> provided below.
>
> See Attached (bug.c)
>
> Tom R. Dial
> [email protected]

Hello Tom

Your program is buggy and depends on scheduling policies.

To make it fails on 2.6.23 (or say 2.6.9) too, just add a sleep(1) before pthread_join() call

Once a thread calls pthread_detach(pthread_t this_id), it's not possible anymore to
pthread_join(this_id, &ret) or effect is undefined.

(quoting POSIX : "The results of multiple simultaneous calls to pthread_join() specifying the same target thread are undefined" )