From sysmaint@contek.com Sun Aug 5 09:35:00 2007 Return-Path: Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id C5BD216A417 for ; Sun, 5 Aug 2007 09:35:00 +0000 (UTC) (envelope-from sysmaint@contek.com) Received: from smtp02.lnh.mail.rcn.net (smtp02.lnh.mail.rcn.net [207.172.157.102]) by mx1.freebsd.org (Postfix) with ESMTP id 96A1A13C474 for ; Sun, 5 Aug 2007 09:35:00 +0000 (UTC) (envelope-from sysmaint@contek.com) Received: from mr02.lnh.mail.rcn.net ([207.172.157.22]) by smtp02.lnh.mail.rcn.net with ESMTP; 05 Aug 2007 05:06:22 -0400 Received: from smtp01.lnh.mail.rcn.net (smtp01.lnh.mail.rcn.net [207.172.4.11]) by mr02.lnh.mail.rcn.net (MOS 3.8.3-GA) with ESMTP id NQC26144; Sun, 5 Aug 2007 05:06:13 -0400 (EDT) Received: from 207-172-216-217.s979.apx1.sbo.ma.dialup.rcn.com (HELO mail.contek.com) ([207.172.216.217]) by smtp01.lnh.mail.rcn.net with ESMTP; 05 Aug 2007 05:06:12 -0400 Received: by mail.contek.com (Postfix, from userid 10112) id 2646B3F407; Sun, 5 Aug 2007 01:50:17 -0400 (EDT) Message-Id: <20070805055017.2646B3F407@mail.contek.com> Date: Sun, 5 Aug 2007 01:50:17 -0400 (EDT) From: Douglas Wells Reply-To: Douglas Wells To: FreeBSD-gnats-submit@freebsd.org Cc: sysmaint@contek.com Subject: pthread_atfork misbehaves in initial thread X-Send-Pr-Version: 3.113 >Number: 115211 >Category: threads >Synopsis: pthread_atfork misbehaves in initial thread >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-threads >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Sun Aug 05 09:40:01 GMT 2007 >Closed-Date: >Last-Modified: Mon Aug 27 13:40:01 GMT 2007 >Originator: Douglas Wells >Release: FreeBSD 6.2-RELEASE-p1 i386 >Organization: >Environment: System: FreeBSD flame.contek.com 6.2-RELEASE-p1 FreeBSD 6.2-RELEASE-p1 #0: Sun Feb 11 18:14:07 EST 2007 root@flame.contek.com:/other5/src.6.2/sys/i386/compile/FLAME.6.2 i386 Above system is patched up to SA-07:07.bind. Using default compiler: gcc version 3.4.6 [FreeBSD] 20060305. >Description: If pthread_atfork is invoked in the original thread of a process, it does *not* invoke the various handlers. If the same test is invoked from a created thread, it invokes the various handlers as expected. I don't see any exception in the POSIX standard that supports this, and the test program runs as expected on a recent OS/X. >How-To-Repeat: Use the included test program. If compiled and run as: "gcc -pthread atforkbug.c && ./a.out", example (erroneous) output is: parent pid (11263) Child exiting: (11264) child (11264) returned If compiled and run as: "gcc -DUSE_NEW_THREAD -pthread atforkbug.c && ./a.out", the program behaves as I expect: parent pid (11270) af_prepare: pid (11270) af_parent: pid (11270) af_child: pid (11271) Child exiting: (11271) child (11271) returned ------------------------- test program ---------------------- #include #include #include #include #include #include #include #include #include #if defined (USE_NEW_THREAD) bool run_in_separate_thread = true; #else bool run_in_separate_thread = false; #endif void af_prepare (void) { fprintf (stderr, "af_prepare: pid (%ld)\n", (long) getpid ()); } void af_parent (void) { fprintf (stderr, " af_parent: pid (%ld)\n", (long) getpid ()); } void af_child (void) { fprintf (stderr, " af_child: pid (%ld)\n", (long) getpid ()); } void * run_test (void *arg) { pid_t child, xpid; int err; (void) arg; err = pthread_atfork (af_prepare, af_parent, af_child); assert (err == 0); switch ((int) (child = fork ())) { case -1: assert (! "bad fork"); case 0: /* child here */ sleep (1); fprintf (stderr, "Child exiting: (%ld)\n", (long) getpid ()); exit (EXIT_SUCCESS); default: xpid = waitpid (child, NULL, 0); assert (xpid == child); fprintf (stdout, "child (%ld) returned\n", (long) child); } return NULL; } int main (void) { fprintf (stdout, "parent pid (%ld)\n", (long) getpid ()); if (run_in_separate_thread) { pthread_t tid; int err; err = pthread_create (&tid, NULL, run_test, NULL); assert (err == 0); err = pthread_join (tid, NULL); assert (err == 0); } else run_test (NULL); return EXIT_SUCCESS; } ----------------------- end test program -------------------- >Fix: Fix and/or workaround unknown (other than to execute from a created thread). >Release-Note: >Audit-Trail: From: "Craig Rodrigues" To: bug-followup@FreeBSD.org, sysmaint@contek.com Cc: Subject: Re: threads/115211: pthread_atfork misbehaves in initial thread Date: Fri, 24 Aug 2007 08:41:56 -0400 Hi, On FreeBSD-CURRENT, if I do: gcc pthread_atforkbug.c -lkse && ./a.out This is the output: parent pid (48871) Child exiting: (48872) child (48872) returned If I do: gcc pthread_atforkbug.c -lkse && ./a.out parent pid (48877) af_prepare: pid (48877) af_parent: pid (48877) af_child: pid (48878) Child exiting: (48878) child (48878) returned On FreeBSD 6.2, -pthread is mapped to -lkse, while on CURRENT, it is mapped to -lthr. Can you try your test again with -lthr on FreeBSD 6.2? This looks like a libkse bug. -- Craig Rodrigues rodrigc@crodrigues.org From: sysmaint@contek.com (Douglas Wells) To: Craig Rodrigues Cc: bug-followup@FreeBSD.org, sysmaint@contek.com Subject: Re: threads/115211: pthread_atfork misbehaves in initial thread Date: Mon, 27 Aug 2007 08:46:52 -0400 (EDT) > Hi, > > On FreeBSD-CURRENT, if I do: > > gcc pthread_atforkbug.c -lkse && ./a.out > > This is the output: > > parent pid (48871) > Child exiting: (48872) > child (48872) returned > > If I do: > > gcc pthread_atforkbug.c -lkse && ./a.out > > parent pid (48877) > af_prepare: pid (48877) > af_parent: pid (48877) > af_child: pid (48878) > Child exiting: (48878) > child (48878) returned > > > On FreeBSD 6.2, -pthread is mapped to -lkse, while on CURRENT, it is mapped > to -lthr. Can you try your test again with -lthr on FreeBSD 6.2? > This looks like a libkse bug. > > -- > Craig Rodrigues > rodrigc@crodrigues.org Yes, I can verify that using: -pthread => erroneous behavior -lthr => expected behavior on my installation of 6.2. - dmw >Unformatted: