Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1030182AbbKCXRX (ORCPT ); Tue, 3 Nov 2015 18:17:23 -0500 Received: from mail-lb0-f172.google.com ([209.85.217.172]:34400 "EHLO mail-lb0-f172.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965042AbbKCXRV (ORCPT ); Tue, 3 Nov 2015 18:17:21 -0500 MIME-Version: 1.0 From: Pavel Labath Date: Tue, 3 Nov 2015 15:16:59 -0800 Message-ID: Subject: ptrace and pseudoterminals To: Oleg Nesterov , linux-kernel@vger.kernel.org Content-Type: multipart/mixed; boundary=e89a8f23501569cbc20523ab18f4 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3922 Lines: 72 --e89a8f23501569cbc20523ab18f4 Content-Type: text/plain; charset=UTF-8 Hello Oleg, everyone, I have noticed something, which may be considered a race in the interaction of ptrace and pseudoterminal interfaces. Basically, what happens is this: - we have two processes: A and B. B has the slave end of the pty open, A has the master. A is tracing B. - B writes some data through the slave end and then stops. - A waits for B to stop. - A does a select on the master pty endpoint. select returns there is no data available - later, A tries the select again, and this time the data appears. We are encountering this (very rare) issue in our debugger test suite, where we check the stdout of the tracee to make sure it is behaving as expected. I have attached a small program reproducing this behavior (it fails after about 1000 iterations on a 3.13.0 kernel, I can retry it on a newer kernel next week if you believe it might work there). Interestingly, when I replace the pty with a regular pipe, it works as expected (the data is available as soon as the program stops). My question is: Is this behavior something that you would consider a bug? If yes, do you have any pointers, as to where I should look to fix it? kind regards, pavel --e89a8f23501569cbc20523ab18f4 Content-Type: text/x-csrc; charset=US-ASCII; name="pty.c" Content-Disposition: attachment; filename="pty.c" Content-Transfer-Encoding: base64 X-Attachment-Id: f_igjzvqq70 I2luY2x1ZGUgPHVuaXN0ZC5oPgojaW5jbHVkZSA8YXNzZXJ0Lmg+CiNpbmNsdWRlIDxzeXMvcHRy YWNlLmg+CiNpbmNsdWRlIDx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AgICBraWxsKGNoaWxkLCBTSUdLSUxMKTsK ICAgIGFzc2VydChjbG9zZShyZWFkZmQpID09IDApOwogICAgYXNzZXJ0KHdhaXRwaWQoY2hpbGQs ICZzdGF0dXMsIDApID09IGNoaWxkKTsKICAgIGFzc2VydChXSUZTSUdOQUxFRChzdGF0dXMpKTsK ICAgIGFzc2VydChXVEVSTVNJRyhzdGF0dXMpID09IFNJR0tJTEwpOwp9CgppbnQgbWFpbihpbnQg YXJnYykKewogICAgaW50IHBpcGVmZFsyXTsKICAgIGludCBpID0gMDsKICAgIGZvciAoOzspCiAg ICB7CiAgICAgICAgKytpOwogICAgICAgIGlmICghKGklMTApKQogICAgICAgICAgICBwcmludGYo IiVkXG4iLCBpKTsKICAgICAgICBhc3NlcnQob3BlbnB0eShwaXBlZmQrMCwgcGlwZWZkKzEsIDAs IDApID09IDApOwovLyAgICAgICAgYXNzZXJ0KHBpcGUocGlwZWZkKSA9PSAwKTsKICAgICAgICBw aWRfdCBwaWQgPSBmb3JrKCk7CiAgICAgICAgYXNzZXJ0KHBpZCAhPSAtMSk7CiAgICAgICAgaWYg KHBpZCA9PSAwKQogICAgICAgIHsKICAgICAgICAgICAgYXNzZXJ0KGNsb3NlKHBpcGVmZFswXSkg PT0gMCk7CiAgICAgICAgICAgIGNoaWxkKHBpcGVmZFsxXSk7CiAgICAgICAgICAgIHJldHVybiAx OwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICBhc3NlcnQoY2xv c2UocGlwZWZkWzFdKSA9PSAwKTsKICAgICAgICAgICAgcGFyZW50KHBpZCwgcGlwZWZkWzBdKTsK ICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gMDsKfQo= --e89a8f23501569cbc20523ab18f4-- -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/