From owner-freebsd-bugs@FreeBSD.ORG Sun Jan 19 03:10:00 2014 Return-Path: Delivered-To: freebsd-bugs@smarthost.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id C9FD971D for ; Sun, 19 Jan 2014 03:10:00 +0000 (UTC) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:1900:2254:206c::16:87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id A539C180C for ; Sun, 19 Jan 2014 03:10:00 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.7/8.14.7) with ESMTP id s0J3A0o2025487 for ; Sun, 19 Jan 2014 03:10:00 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.7/8.14.7/Submit) id s0J3A0EA025486; Sun, 19 Jan 2014 03:10:00 GMT (envelope-from gnats) Resent-Date: Sun, 19 Jan 2014 03:10:00 GMT Resent-Message-Id: <201401190310.s0J3A0EA025486@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Henry Hu Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 1E5AA3B2 for ; Sun, 19 Jan 2014 03:00:34 +0000 (UTC) Received: from oldred.freebsd.org (oldred.freebsd.org [IPv6:2001:1900:2254:206a::50:4]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id E41EC16F8 for ; Sun, 19 Jan 2014 03:00:33 +0000 (UTC) Received: from oldred.freebsd.org ([127.0.1.6]) by oldred.freebsd.org (8.14.5/8.14.7) with ESMTP id s0J30Xgk097649 for ; Sun, 19 Jan 2014 03:00:33 GMT (envelope-from nobody@oldred.freebsd.org) Received: (from nobody@localhost) by oldred.freebsd.org (8.14.5/8.14.5/Submit) id s0J30X85097613; Sun, 19 Jan 2014 03:00:33 GMT (envelope-from nobody) Message-Id: <201401190300.s0J30X85097613@oldred.freebsd.org> Date: Sun, 19 Jan 2014 03:00:33 GMT From: Henry Hu To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.1 Subject: misc/185873: waitpid() in linux threads fails with ECHILD X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 19 Jan 2014 03:10:00 -0000 >Number: 185873 >Category: misc >Synopsis: waitpid() in linux threads fails with ECHILD >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Sun Jan 19 03:10:00 UTC 2014 >Closed-Date: >Last-Modified: >Originator: Henry Hu >Release: FreeBSD 11-CURRENT >Organization: Columbia University >Environment: FreeBSD pepsi 11.0-CURRENT FreeBSD 11.0-CURRENT #1 r260031M: Sun Jan 5 18:25:51 EST 2014 root@pepsi:/usr/obj/usr/src/sys/MYKERNEL amd64 >Description: If a Linux program 1. use fork() to fork a child 2. create a thread 3. use waitpid() in the new thread to wait for the child Then waitpid() returns -1, with errno ECHILD. This affects some applications: 1. intellij (experimental port available) with Oracle JDK 2. android studio (based on intellij) and possible other programs using Oracle JDK. If you use java.lang.ProcessBuilder to create a child and get its output, Oracle SDK works in this way: 1. use fork() to create a child 2. create a thread to wait for the child 3. the thread calls waitpid() to wait for child exit 4. the thread reads the child's output Because waitpid() incorrectly returns -1 here before the child exits, the child may have not produced the output, which results in empty output received by the caller. The caller may incorrectly assume that the child is not working. In intellij, it calls "git --version" to obtain the git version. Because the output is empty, it assumes that git is not working, and disables some features. >How-To-Repeat: A simple test program: #include #include #include #include int child; void* worker(void* arg) { int status; printf("worker waiting\n"); int ret = waitpid(child, &status, 0); printf("waitpid ret: %d status: %d\n", ret, status); return NULL; } int main() { child = fork(); if (child == 0) { printf("child running\n"); sleep(3); printf("child exit\n"); } else { printf("forked: %d\n", child); pthread_t thr; pthread_create(&thr, NULL, worker, NULL); sleep(5); } } If run it natively on a FreeBSD/Linux machine, it outputs forked: 98484 child running worker waiting child exit waitpid ret: 98484 status: 0 However, if run a Linux version on a FreeBSD machine, it outputs forked: 95940 child running worker waiting waitpid ret: -1 status: 0 child exit >Fix: >Release-Note: >Audit-Trail: >Unformatted: