From patchwork Wed Apr 6 03:47:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Down X-Patchwork-Id: 2056 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 03:47:23 +0000 (UTC) Received: from mail.archlinux.org by mail.archlinux.org with LMTP id iANrO0oNTWKsgwgAK+/4rw (envelope-from ) for ; Wed, 06 Apr 2022 03:47:23 +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 DC643EAC7EC; Wed, 6 Apr 2022 03:47:21 +0000 (UTC) Received: from lists.archlinux.org (localhost [IPv6:::1]) by lists.archlinux.org (Postfix) with ESMTP id CD487ED4B52; Wed, 6 Apr 2022 03:47:21 +0000 (UTC) X-Original-To: pacman-dev@lists.archlinux.org Delivered-To: pacman-dev@lists.archlinux.org Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lists.archlinux.org (Postfix) with ESMTPS id 7A51CED4B4B for ; Wed, 6 Apr 2022 03:47:20 +0000 (UTC) Received: by mail-wr1-x42b.google.com with SMTP id d3so1233051wrb.7 for ; Tue, 05 Apr 2022 20:47:20 -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=phmAyroEcpG8n4ILNa9cae+f+pXnr7VDtzEV009mzwk=; b=ibqLGZc7z2PKT0XH/hjRnF6NvgdMjBpq5jPRd82dJ9aGYMRtZZhvH8V2NTumNWS63C KQNsgQ3MAG+rMWGF0b8w/A+bUiMDIxVFsDR4sCOYo2DhwJfmUsGFJ22oyJ7K5Cbll4We qq8O2cHS1sDDS7Y9ZFLOgOmokTtftBZKVNak8= 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=phmAyroEcpG8n4ILNa9cae+f+pXnr7VDtzEV009mzwk=; b=X9+KwLnbrcO31J130WxrwYL46ZWMIiClRYOivqFcFDD54innLRswQiJNNtjjXOcZrn z7Da6nX20m7BsoXMIvpuzKqFpYo6p7Fo0j5tOWA2dfeCy3pT8Z9eLbAsf79aN39r2E/K p7IleDr9Z+VlAdvsUGQAbpZaVLnPUXm+V0KtkRh/nTY8u/zCwr5FjkOzrlB3u9sJzUdx yzHzRvemEfjwdE6flB2ieTmEuWtTcyyEj+PJcQKJ/ZTk6/Llv0o0sNtdWRfiCaFT7oit ZQEGt4xatUNFAYVPPBXUtBOVRnPuAJWpxnWkuUzDq3tDyYGCeXaA6BNspmkkIrXgIcUc Sxxg== X-Gm-Message-State: AOAM531FntU9znuAgyGbO0yoem/IcI0NnpQrBO8qqEGc9ZqTGUhYlODG yMSoC0HLaY9qcmMW6UMSlik2vnOQGVI1+W7lCNo= X-Google-Smtp-Source: ABdhPJxF9yFz0QiQUQ4O/PUARauF0cnM69J5WEY6xYq0fbI5jpoUlr5BTc9TLPX4A5FIxieXpr2haQ== X-Received: by 2002:a05:6000:18a3:b0:204:1f84:287e with SMTP id b3-20020a05600018a300b002041f84287emr5039674wri.184.1649216839550; Tue, 05 Apr 2022 20:47:19 -0700 (PDT) Received: from localhost ([2620:10d:c092:400::4:2e47]) by smtp.gmail.com with ESMTPSA id o11-20020adf9d4b000000b001f0077ea337sm13442774wre.22.2022.04.05.20.47.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 20:47:19 -0700 (PDT) Date: Wed, 6 Apr 2022 04:47:18 +0100 From: Chris Down To: pacman-dev@lists.archlinux.org Subject: [PATCH] cleanup: Flush cursor state to stdout before removing interrupt 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=ibqLGZc7; 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: DC643EAC7EC X-Spamd-Result: default: False [-1.91 / 15.00]; DMARC_POLICY_ALLOW(-0.50)[chrisdown.name,none]; RCVD_DKIM_ARC_DNSWL_MED(-0.50)[]; RCVD_IN_DNSWL_MED(-0.20)[95.217.236.249:from]; R_SPF_ALLOW(-0.20)[+ip4:95.217.236.249:c]; MAILLIST(-0.20)[mailman]; R_DKIM_ALLOW(-0.20)[chrisdown.name:s=google]; MIME_GOOD(-0.10)[text/plain]; HAS_LIST_UNSUB(-0.01)[]; DKIM_TRACE(0.00)[chrisdown.name:+]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:24940, ipnet:95.217.0.0/16, country:DE]; RCVD_TLS_LAST(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[2a00:1450:4864:20::42b:received]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; FROM_NEQ_ENVFROM(0.00)[chris@chrisdown.name,pacman-dev-bounces@lists.archlinux.org]; RCVD_COUNT_FIVE(0.00)[5]; ARC_NA(0.00)[]; NEURAL_HAM(-0.00)[-1.000]; FROM_HAS_DN(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[pacman-dev@lists.archlinux.org]; FORGED_SENDER_MAILLIST(0.00)[]; TO_DN_NONE(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 | 9 +++++++++ 2 files changed, 10 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..d57867ab 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -1427,6 +1427,15 @@ 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); } char *safe_fgets_stdin(char *s, int size)