From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuan Fu Newsgroups: gmane.emacs.bugs Subject: bug#67977: 30.0.50; tree-sitter: Emacs crashes when accessing treesit-nodes in a narrowed buffer Date: Tue, 26 Dec 2023 20:15:24 -0800 Message-ID: <262157E9-A92C-4063-ADA4-725C156FB227@gmail.com> References: <87r0jdddxf.fsf@yandex.ru> <835y0pfkgr.fsf@gnu.org> <1DE32BF7-75D8-4F49-975D-53C782D26016@gmail.com> <08D379EF-1556-4498-8E60-F7972A25752A@gmail.com> <83edfccbwi.fsf@gnu.org> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.700.6\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="23223"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Denis Zubarev , 67977@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Dec 27 05:16:31 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1rILLK-0005r5-E1 for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 27 Dec 2023 05:16:30 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rILKw-0003r9-1z; Tue, 26 Dec 2023 23:16:06 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rILKu-0003qv-Dk for bug-gnu-emacs@gnu.org; Tue, 26 Dec 2023 23:16:04 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rILKu-00079B-66 for bug-gnu-emacs@gnu.org; Tue, 26 Dec 2023 23:16:04 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rILKs-0005rL-2D for bug-gnu-emacs@gnu.org; Tue, 26 Dec 2023 23:16:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Yuan Fu Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 27 Dec 2023 04:16:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 67977 X-GNU-PR-Package: emacs Original-Received: via spool by 67977-submit@debbugs.gnu.org id=B67977.170365054420472 (code B ref 67977); Wed, 27 Dec 2023 04:16:02 +0000 Original-Received: (at 67977) by debbugs.gnu.org; 27 Dec 2023 04:15:44 +0000 Original-Received: from localhost ([127.0.0.1]:35219 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rILKZ-0005Jb-Ix for submit@debbugs.gnu.org; Tue, 26 Dec 2023 23:15:44 -0500 Original-Received: from mail-ot1-x329.google.com ([2607:f8b0:4864:20::329]:45264) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rILKX-00053Z-3P for 67977@debbugs.gnu.org; Tue, 26 Dec 2023 23:15:42 -0500 Original-Received: by mail-ot1-x329.google.com with SMTP id 46e09a7af769-6dc026661efso281039a34.2 for <67977@debbugs.gnu.org>; Tue, 26 Dec 2023 20:15:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703650537; x=1704255337; darn=debbugs.gnu.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=aDtRq0E9WBe/Ulks3iHYDJn8gYJuapMYG1GVIv1RalY=; b=HXBZ8JLcMD0NRNhoeaPjpScu4rvNCLs4qxnkchLA9UvwqFbu2Ra84ZDB6OecRIoEpF qD5ugflZH/awFZTfBasmue3IrdgioXx5xFoY5+hrXv66cwG7dtiFOour/vOQafvRM8Wi sI5kakrpECaWDke1i5Ta0WSRRbL7YuCt3M53RbzCMYp87xyjWwv5cwYuEZoDIw5s8y9L cVfVz/HbvDkUzoCOHRXe0WUvnVvJQa4yFHtVTYtPCisAwk6w8cgeXyPrbiBYcM+lrBI2 HnE8LrVqIPR0oLcyDEXgPrGlr1zMgSZE3loiAaP9OnOvmOvpmBkgO1WHgZpZFYxDKXBq FGeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703650537; x=1704255337; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aDtRq0E9WBe/Ulks3iHYDJn8gYJuapMYG1GVIv1RalY=; b=lk9Dv/b/1fKV8HNSm5hTKw+DPpc27E4HLsF73DeYDdeRVyso/zlyRGbpJ+4wnXvxbZ nVl+t+sx0nsftP8JEDc0Y1kprPC2yRWWMWQ+kdQBf/L3o51rXCh+I2XwSoLa4ePh/8HF GqAhWoCwpudIhd6cNQvVTnoJTq9zfu0sxCrJ6QDvHzLGb+VuXVLVcK6DRB3GUIEL1d6g nefFFJdToUc1iqWPYiAN30qZQ5GKtSjleTCcgPaEAWCFEo4MjdBXxKOdamEe/+3STrf/ aZe4+O7pfCPtSdeOoW8W213oUoUg6C74jBmb1+hGwXZCrfDEtAbXLdHCjrkdwGI4jZOo YSOQ== X-Gm-Message-State: AOJu0Yx96cCO4YQVBno46+X6YkxN/fC6WEnUuZwtt5HXRRhTBVapw1V1 X68Q6rl51ETFW5oRmeTtve8= X-Google-Smtp-Source: AGHT+IG2kvdGKAI5XGsMLYFMU0owRZ0Di8uYzqHq2kKZBisCppwP32i/wlWSMmZVUnf7aMioTGGIzQ== X-Received: by 2002:a9d:7a8c:0:b0:6d8:8007:d56f with SMTP id l12-20020a9d7a8c000000b006d88007d56fmr6643075otn.28.1703650537020; Tue, 26 Dec 2023 20:15:37 -0800 (PST) Original-Received: from smtpclient.apple (172-117-161-177.res.spectrum.com. [172.117.161.177]) by smtp.gmail.com with ESMTPSA id l23-20020a62be17000000b006d45fc85962sm10590269pff.55.2023.12.26.20.15.36 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 26 Dec 2023 20:15:36 -0800 (PST) In-Reply-To: <83edfccbwi.fsf@gnu.org> X-Mailer: Apple Mail (2.3731.700.6) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:276928 Archived-At: > On Dec 23, 2023, at 11:11 PM, Eli Zaretskii wrote: >=20 >> From: Yuan Fu >> Date: Sat, 23 Dec 2023 19:00:34 -0800 >> Cc: Denis Zubarev , >> 67977@debbugs.gnu.org >>=20 >>>> Yuan, this also happens on the emacs-29 branch, so we should try >>>> fixing this crash ASAP. >>>=20 >>> Yeah. The node wants to print it=E2=80=99s type name (with = ts_node_type), which access it=E2=80=99s parse tree, but the tree is = already freed, that means the node is outdated and shouldn=E2=80=99t try = to print it=E2=80=99s type name, but should rather print =E2=80=9Coutdated= =E2=80=9D. >>>=20 >>> But simply narrowing the buffer shouldn=E2=80=99t reparse the buffer = and cause the parse tree to be freed. Anyway, let me see what=E2=80=99s = going on. >>=20 >>=20 >> I pushed a fix and now it shouldn=E2=80=99t crash anymore. However, = I=E2=80=99m yet not sure why at some point the buffer was widened. Is = there any way to track who called widen? >=20 > Run Emacs under GDB with a breakpoint at Fwiden, then look at the > backtrace. The command "xbacktrace", defined on src/.gdbinit, will > show a Lisp backtrace as well. >=20 > But I already did the above, and the answer is the expected one: it's > JIT font-lock, which calls font-lock-fontify-region, which does: >=20 > (save-restriction > (unless font-lock-dont-widen (widen)) >=20 > And if you leave blink-cursor-mode and global-eldoc-mode on (which is > the default), you have also another caller: jit-lock-context-fontify > (which is called from a timer). >=20 > Does this answer your question? Yes, they do. Many thanks! > Btw, I hope that these calls to 'widen' don't require unnecessary > reparsing by tree-sitter, do they? Yes, but only because we called treesit-node-at while the buffer is = narrowed, which triggers a reparse. Font-lock and jit-lock themselves = always access the parser with widened buffer so they don=E2=80=99t = trigger reparse on their own.=20 So it seems working in a narrowed buffer would trigger a lot of = back-and-fortch reparse. I wonder if it=E2=80=99s worth optimizing for = (eg, use two parsers behind the scenes, one for widened buffer and one = for narrowed buffer). Yuan=