From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philipp Stephani Newsgroups: gmane.emacs.devel Subject: Re: scratch/sigchld-fd 8f0ce42 1/2: Fix deadlock when receiving SIGCHLD during 'pselect'. Date: Tue, 19 Jan 2021 21:22:04 +0100 Message-ID: References: <20210116184947.2105.45267@vcs0.savannah.gnu.org> <20210116184949.3C17C211A5@vcs0.savannah.gnu.org> <834kjd7yk5.fsf@gnu.org> <83turc7n93.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="32399"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Philipp Stephani , Emacs developers To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Jan 19 21:24:18 2021 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l1xY1-0008KJ-Pi for ged-emacs-devel@m.gmane-mx.org; Tue, 19 Jan 2021 21:24:17 +0100 Original-Received: from localhost ([::1]:52662 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l1xY0-0003J9-Pl for ged-emacs-devel@m.gmane-mx.org; Tue, 19 Jan 2021 15:24:16 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:51200) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l1xW8-0001aj-Bh for emacs-devel@gnu.org; Tue, 19 Jan 2021 15:22:24 -0500 Original-Received: from mail-oi1-x22d.google.com ([2607:f8b0:4864:20::22d]:42398) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1l1xW4-0007lE-Tu; Tue, 19 Jan 2021 15:22:20 -0500 Original-Received: by mail-oi1-x22d.google.com with SMTP id x71so6793802oia.9; Tue, 19 Jan 2021 12:22:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=NpFYVHnjPx/ZMhsevsJ9mJkwlyxJ0abHQ8QnKNRRoqE=; b=k/6GxoECtbX+o4OM3hR4XPQAoKHiyobGiuHDkf/XuKk7vnL1msBFDqfGSztxzK3U1r 59Um2uVOsoBvnu5+t60qASVUg/CrUik6hQkSd/ag7i545tWp2vwX8XIiZ7q/BWlTBT+p EpXAbUTamrJDN11IFgeMAiYhWsmDoUhZWBozwnNEKEhDS7thDwydIsFydy3eC3ncEAxZ sHR4zS0ij1RON3c++8718dmt0sUWpr0XCeeOXo5OuW1AEAMZLzNkSqcTjwlsOWbrLJ7Y ernLS0qMhG8L7+82co2t8yZiXljVAVZdfeIGkgjzhLtk9TCiicaVUfoHOdpGlxZOZlXi UaFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=NpFYVHnjPx/ZMhsevsJ9mJkwlyxJ0abHQ8QnKNRRoqE=; b=O3dalm4n64fQbyZTUYWUaKuADgbBueuynBZzvyLvbLM3cIAcD6OfGOJPIow2PYeIlx NzSiPTwNSQbnUOcR2yMyN7s/uN8qQLth4Zg+Mbw5n021/5sENwCJP8nb73WlCJPU8Q6u 1plNX597jh+1Bgw/FeKlz8uyCrPFxOE1+MhhlgELyoh4NoyPiUcaM8e0MV2/VLrnq/Iw J/Xm/KJfUKG5fKcfjKXC3t5HuPpkMDjGrQ2AQ5N6tq1UoKZg1kDBwn9ydRvtXmzFleg9 82oj8AeUziw7VPvwpJKS0YTmy0MCQfaTmcNrjzZVILFulJxg+a9NeMOpWF9SVwEyJguw Jkog== X-Gm-Message-State: AOAM530kXg92qllYGbBaS1Q9sMzda28CmnuG7OdjEQ9Tr6InRuQ2RIPr nDPWvoFkdYxL0iHYjM5DLB+7Vo3t3QxhouYwFCzoYjulmy5ZxA== X-Google-Smtp-Source: ABdhPJzQdBiK9UlhnQGfsijLkT16Vm6MYU7PORRpSNPNhn/1odAsQJjzdXewiTkynF9pBcWEAN8yaUgX9cAVWGjt9LY= X-Received: by 2002:aca:b787:: with SMTP id h129mr955071oif.150.1611087734913; Tue, 19 Jan 2021 12:22:14 -0800 (PST) In-Reply-To: <83turc7n93.fsf@gnu.org> Received-SPF: pass client-ip=2607:f8b0:4864:20::22d; envelope-from=p.stephani2@gmail.com; helo=mail-oi1-x22d.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:263182 Archived-At: Am Di., 19. Jan. 2021 um 20:14 Uhr schrieb Eli Zaretskii : > > > From: Philipp Stephani > > Date: Tue, 19 Jan 2021 19:21:39 +0100 > > Cc: Emacs developers , Philipp Stephani > > > In general, pselect is supposed to return with EINTR when SIGCHLD > > > happoens while we are inside the call to pselect, and EINTR seems to > > > be already handled by wait_reading_process_output. So I wonder why we > > > need that additional "self-pipe" to be watched by pselect. > > > > Yes, I'm wondering about that as well, but it's definitely the > > behavior I see. Before commiting to master, I ran the test > > process-tests/fd-setsize-no-crash/make-process multiple times with and > > without the commit, and the outcome was clear: without the commit > > accept-process-output would frequently hang, with the commit it never > > hangs. > > This is pure speculation, but I could imagine multiple things going on: > > - Maybe there's no guarantee that pselect actually returns EINTR on SIGCHLD. > > - Maybe EINTR is returned too early, before the signal handler got the > > chance to update the process status. > > I'd be happier if we had some direct evidence to these effects. I'd > also be surprised to hear that pselect doesn't return with EINTR when > SIGCHLD comes in. It is more likely that SIGCHLD is delivered before > we call pselect, but if that is the case, we should be able to > reliably detect that, I think. So I've added a ton of logging to process.c, and the series of events I observe (without the patch) is as follows (line numbers are approximate due to the logging statements): process.c:4729: Faccept_process_output: enter process.c:5139: wait_reading_process_output: enter process.c:5193: wait_reading_process_output: outer loop process.c:5322: wait_reading_process_output: update_tick = 261, process_tick = 261 process.c:5554: wait_reading_process_output: before pselect; max_desc = 1019 process.c:5601: wait_reading_process_output: after pselect: nfds = -1 process.c:5641: wait_reading_process_output: EINTR process.c:5193: wait_reading_process_output: outer loop process.c:5322: wait_reading_process_output: update_tick = 261, process_tick = 261 process.c:7189: handle_child_signal: enter process.c:7234: handle_child_signal: process test 5: change status to 0; new process_tick = 262 process.c:144: handle_child_signal: leave process.c:5554: wait_reading_process_output: before pselect; max_desc = 1017 and then Emacs hangs. So wait_reading_process_output indeed first receives EINTR, loops around, and checks for the process_tick change before SIGCHLD is handled. By the time it reruns pselect it's too late. > > > > In addition, AFAIU this pipe should not be needed on MS-Windows, where > > > the pselect emulation waits on the sub-process handles together with > > > the other file descriptors, and so gets awakened when a process exits > > > or dies. But again, without knowing the exact situations against > > > which this changeset tries to protect, it is hard to make a decision. > > > > It's definitely not needed on Windows, which has a superior mechanism > > anyway (process handles are waitable objects in Windows). I opted to > > create the additional pipe on Windows as well - the costs should be > > small, and it keeps the code more consistent between the operating > > systems. > > The thing is, on Windows we can only wait on up to 64 handles (unless > we complicate the code with multilevel wait, that is), so every > unnecessary descriptor we need to wait on means we can support fewer > simultaneous subprocesses. We are already limited to just 32 > subprocesses, which is quite low a number. OK, that's a good point, I didn't know about this limitation. I'll see that I can remove the pipe on Windows.