From patchwork Wed Apr 6 04:14:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Down X-Patchwork-Id: 2057 Return-Path: Delivered-To: patchwork@archlinux.org Received: from mail.archlinux.org [95.216.189.61] by patchwork.archlinux.org with IMAP (fetchmail-6.4.29) for (single-drop); Wed, 06 Apr 2022 04:14:26 +0000 (UTC) Received: from mail.archlinux.org by mail.archlinux.org with LMTP id JP0XKqETTWI6hggAK+/4rw (envelope-from ) for ; Wed, 06 Apr 2022 04:14:25 +0000 Received: from lists.archlinux.org (lists.archlinux.org [95.217.236.249]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mail.archlinux.org (Postfix) with ESMTPS id 433C8EAC992; Wed, 6 Apr 2022 04:14:24 +0000 (UTC) Received: from lists.archlinux.org (localhost [IPv6:::1]) by lists.archlinux.org (Postfix) with ESMTP id 2A37EED4C64; Wed, 6 Apr 2022 04:14:24 +0000 (UTC) X-Original-To: pacman-dev@lists.archlinux.org Delivered-To: pacman-dev@lists.archlinux.org Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lists.archlinux.org (Postfix) with ESMTPS id 2F0EDED4C55 for ; Wed, 6 Apr 2022 04:14:22 +0000 (UTC) Received: by mail-wm1-x335.google.com with SMTP id bi13-20020a05600c3d8d00b0038c2c33d8f3so2812054wmb.4 for ; Tue, 05 Apr 2022 21:14:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=J2ZOsQlrBqkptN4KXnsdtQHj9jsjPoDuDu69BdXPpi8=; b=u93LatUl816CSCVcJQX0bRy5fnNv/vNLmQF0aj0cxRR7W5ZRcjFojQTPNyenbA8lVo 37iJBUehmHS3noU2ocYAbdzbT4J6XU+2f9O/B74HCOuO0XtJte0CV8mAfaKRmLkD20vo wRr2bcs5ARWPpjyJBoOOX/6oPjvgkZCJ0OpGM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=J2ZOsQlrBqkptN4KXnsdtQHj9jsjPoDuDu69BdXPpi8=; b=XVBYzoCJnkxnPk1vAOyIf1zaEtTvFNWDL4o9Dh8lEEAQWiEZxJNNmKQfAFJp3GO4en jmv+LprYKtQ2tqPZPbhGv7xARjItZudkUfZa+rc+LGM2wCBSukqJDXzW3BQRw2EKpYKZ eSfWDT7vAUqIUUpnb6xbOkVj/NWoUimncuGZDZBHe2SLkvGG4htiS+ycofVZjyMgPCMT 7ycCRYvLh75QNZkkXAJ1Xu1o0Hm30QkT1zUfjpxt3+gX7adwD/aHjkCmFEasSy3zf8KE NDNLFWwruIRvEPlxc0Vbd7lTqBpxInpFdOut8Mbfh/qhhNL5UE/pFc5x7RfAsOs4b7WC IscQ== X-Gm-Message-State: AOAM531soWF4CWBcwbb7VoVvJwzSXDN429ffzsAhSwwplZFI/8VyN+a3 wgAPEbUIKqhj3qexE85N47QyYNH/67RhjILtUhc= X-Google-Smtp-Source: ABdhPJxlcyF5jxsbvHwhxyjKNP48Hi3n38dQt+MEpXTkQRnCfP0K4s7ZZGkaChyXA3d09Rux7v/4Ow== X-Received: by 2002:a7b:cc15:0:b0:381:3fb8:5f93 with SMTP id f21-20020a7bcc15000000b003813fb85f93mr5508758wmh.106.1649218461290; Tue, 05 Apr 2022 21:14:21 -0700 (PDT) Received: from localhost ([2620:10d:c092:400::4:2e47]) by smtp.gmail.com with ESMTPSA id k9-20020adfb349000000b00206101fc58fsm7728358wrd.110.2022.04.05.21.14.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 21:14:20 -0700 (PDT) Date: Wed, 6 Apr 2022 05:14:20 +0100 From: Chris Down To: pacman-dev@lists.archlinux.org Subject: [PATCH v2] util: Flush cursor state to stdout before removing signal handler Message-ID: MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/2.2.2 (aa28abe8) (2022-03-25) X-BeenThere: pacman-dev@lists.archlinux.org X-Mailman-Version: 2.1.39 Precedence: list List-Id: Discussion list for pacman development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: pacman-dev-bounces@lists.archlinux.org Sender: "pacman-dev" Authentication-Results: mail.archlinux.org; dkim=pass header.d=chrisdown.name header.s=google header.b=u93LatUl; dmarc=pass (policy=none) header.from=chrisdown.name; spf=pass (mail.archlinux.org: domain of pacman-dev-bounces@lists.archlinux.org designates 95.217.236.249 as permitted sender) smtp.mailfrom=pacman-dev-bounces@lists.archlinux.org X-Rspamd-Queue-Id: 433C8EAC992 X-Spamd-Result: default: False [-1.91 / 15.00]; DMARC_POLICY_ALLOW(-0.50)[chrisdown.name,none]; RCVD_DKIM_ARC_DNSWL_MED(-0.50)[]; MAILLIST(-0.20)[mailman]; R_SPF_ALLOW(-0.20)[+ip4:95.217.236.249:c]; R_DKIM_ALLOW(-0.20)[chrisdown.name:s=google]; RCVD_IN_DNSWL_MED(-0.20)[95.217.236.249:from]; MIME_GOOD(-0.10)[text/plain]; HAS_LIST_UNSUB(-0.01)[]; RCVD_IN_DNSWL_NONE(0.00)[2a00:1450:4864:20::335:received]; NEURAL_HAM(-0.00)[-1.000]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCVD_TLS_LAST(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:24940, ipnet:95.217.0.0/16, country:DE]; DKIM_TRACE(0.00)[chrisdown.name:+]; RCPT_COUNT_ONE(0.00)[1]; TO_DN_NONE(0.00)[]; RCVD_COUNT_FIVE(0.00)[5]; ARC_NA(0.00)[]; FROM_NEQ_ENVFROM(0.00)[chris@chrisdown.name,pacman-dev-bounces@lists.archlinux.org]; FROM_HAS_DN(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[pacman-dev@lists.archlinux.org]; FORGED_SENDER_MAILLIST(0.00)[]; MID_RHS_MATCH_FROM(0.00)[] X-Rspamd-Server: mail.archlinux.org It's possible that the cursor does not reappear after pressing ^C during shutdown. In my case, I noticed this when pressing ^C after getting results from `pacman -F` -- this can reasonably reliably be triggered by issuing a file query and pressing ^C shortly after results are shown. There are two reasons for this issue: 1. The graceful SIGINT handler is removed at the start of cleanup(), but the window from entering cleanup() to reaching exit() is non trivial. The main offender is FREELIST(pm_targets), which on my T14s takes >0.1s to execute. This means that if you are unlucky enough to press ^C while there, the cursor isn't coming back, because we haven't issued any command to show the cursor again yet, and the userspace signal handler is already blown away. 2. Moving console_cursor_show() to earlier in cleanup() only half solves the issue. While it's fine not to flush after _hiding_ the cursor, since it will at least make itself apparent before any other text reaches the screen, _showing_ the cursor must be followed by flushing stdout, because once the graceful SIGINT handler is gone, if you press ^C, no flush will be triggered (and thus there will be no cursor). This fixes the issue by always starting out by showing the cursor again at cleanup() time. This means that no matter where we get caught at ^C, we will not end up leaving the terminal without its beloved ensign. Signed-off-by: Chris Down --- src/pacman/pacman.c | 2 +- src/pacman/util.c | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index ea536a47..e5c6e420 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -289,6 +289,7 @@ static void setuseragent(void) */ static void cleanup(int ret) { + console_cursor_show(); remove_soft_interrupt_handler(); if(config) { /* free alpm library resources */ @@ -302,7 +303,6 @@ static void cleanup(int ret) /* free memory */ FREELIST(pm_targets); - console_cursor_show(); exit(ret); } diff --git a/src/pacman/util.c b/src/pacman/util.c index 53833d6f..e92b2012 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -1426,6 +1426,16 @@ void console_cursor_hide(void) { void console_cursor_show(void) { if(isatty(fileno(stdout))) { printf(CURSOR_SHOW_ANSICODE); + + /* We typically explicitly show the cursor either when we are + * getting input from stdin, or when we are in the process of + * exiting. In the former case, it's not guaranteed that the + * terminal will see the command before reading from stdin. In + * the latter case, we need to make sure that if we get a + * further TERM/INT after we return signal disposition to + * SIG_DFL, it doesn't leave the cursor invisible. + */ + fflush(stdout); } }