From debbugs-submit-bounces@debbugs.gnu.org Sat May 21 19:35:24 2022 Received: (at submit) by debbugs.gnu.org; 21 May 2022 23:35:24 +0000 Received: from localhost ([127.0.0.1]:43160 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nsYd0-0000tN-Di for submit@debbugs.gnu.org; Sat, 21 May 2022 19:35:24 -0400 Received: from lists.gnu.org ([209.51.188.17]:43758) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nsY9g-0000Ai-3L for submit@debbugs.gnu.org; Sat, 21 May 2022 19:05:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44490) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nsY9f-0005Ae-Se for bug-guile@gnu.org; Sat, 21 May 2022 19:05:03 -0400 Received: from mx2.mythic-beasts.com ([2a00:1098:0:82:1000:0:2:1]:35461) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nsY9d-0002FW-NF for bug-guile@gnu.org; Sat, 21 May 2022 19:05:03 -0400 Received: from [84.62.107.143] (port=40964 helo=dorian) by balrog.mythic-beasts.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92.3) (envelope-from ) id 1nsY9U-0006mh-RS for bug-guile@gnu.org; Sun, 22 May 2022 00:04:57 +0100 Date: Sun, 22 May 2022 01:04:44 +0200 From: Thomas White To: bug-guile@gnu.org Subject: Error in string->number when calling (backtrace) without COLUMNS envionment variable Message-ID: <20220522010444.6bea02a1@dorian> X-Mailer: Claws Mail 4.0.0git382 (GTK+ 3.24.31; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BlackCat-Spam-Score: 4 X-Spam-Status: No, score=0.4 Received-SPF: pass client-ip=2a00:1098:0:82:1000:0:2:1; envelope-from=taw-gl@bitwiz.me.uk; helo=mx2.mythic-beasts.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.3 (-) X-Debbugs-Envelope-To: submit X-Mailman-Approved-At: Sat, 21 May 2022 19:35:21 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -2.3 (--) Summary: This appears to be a combination of two separate problems: 1. Procedure terminal-width in system/repl/debug.scm does not gracefully handle a #f return value from (getenv "COLUMNS"). 2. The COLUMNS environment variable is only set when readline is active, which never happens if a program is run directly via a command line argument. Relates to https://debbugs.gnu.org/cgi/bugreport.cgi?bug=3D36677 ("Don't truncate backtraces"). Detailed report: I want to set up an exception handler to get a backtrace when something goes wrong in my program. The simple test code below is based on the example code found in https://www.gnu.org/software/guile/manual/html_node/Pre_002dUnwind-Debuggin= g.html The output on Guile 3.0.7 is below. Instead of a backtrace, the call to the backtrace routine produces the following error: system/repl/debug.scm:72:40: In procedure string->number: Wrong type argument in position 1 (expecting string): #f I've also tested on the latest Git version (3.0.8.13-cc455), with the same result. The line referred to in debug.scm (shown below) is all about determining the width of the terminal based on the environment variable COLUMNS. When run as shown in the example below, this variable is not set, causing string->number to fail. This is the first problem, which probably has a simple fix for someone who properly understands this routine. I might have time to look at this myself in a few weeks' time. (define terminal-width (let ((set-width (make-fluid))) (case-lambda (() (or (fluid-ref set-width) (let ((w (false-if-exception (string->number (getenv "COLUMNS"))= ))) (and (integer? w) (exact? w) (> w 0) w)) 72)) ((w) (if (or (not w) (and (integer? w) (exact? w) (> w 0))) (fluid-set! set-width w) (error "Expected a column number (a positive integer)" w)))))) The second problem is the question of why COLUMNS is not set in the first place. The problem goes away after uncommenting the (setenv .. ) lin= e in the test case code, forcing it to be set when the backtrace is generated. I think the problem is that COLUMNS is a shell variable, which is not the same thing as an envionment variable. Read here: https://stackoverflow.com/questions/1780483/lines-and-columns-environmental= -variables-lost-in-a-script In a shell, COLUMNS is usually (not always!) available. But because it's not an environment variable, it doesn't get exported to Guile. Within Guile, COLUMNS is only set after activate-readline is called. Therefore, we get proper backtraces when the program is run from an already-running REPL, like this: $ guile scheme@(guile-user)> (load "testcase.scm") ---> correct backtrace But not when running the program directly, like this: $ guile testcase.scm ---> string->number error instead of backtrace This appears to be confirmed by testing with various combinations of "guile -l" and adding/removing readline references from my ~/.guile file. Fixing the first problem will work around the overall problem in a reasonably acceptable way. We would only have backtraces with the correct width if readline is activated. To get proper width backtraces in all circumstances would require a different way of getting the terminal width. With thanks to stis on IRC this evening, who first realised what was going on. Test case code: (use-modules (ice-9 threads) (ice-9 exceptions) (ice-9 control)) ;; Uncomment the following line to work round the problem ;; (setenv "COLUMNS" "110")=20 (define (do-test) (raise-exception (make-exception (make-exception-with-message "Test exception") (make-exception-with-irritants (list 1 2 3 4 5))))) (define (call-with-backtrace thunk) (let/ec cancel (start-stack 'stack-with-backtrace (with-exception-handler (lambda (exn) (display "Error occurred\n") (backtrace) (cancel #f)) thunk)))) (define (exception-test) (call-with-backtrace (lambda () (sleep 1) (do-test)))) (exception-test) Test case output: [23:06] ~ $ guile exception-test.scm=20 ;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=3D0 ;;; or pass the --no-auto-compile argument to disable. ;;; compiling /home/taw/exception-test.scm ;;; compiled /home/taw/.cache/guile/ccache/3.0-LE-8-4.5/home/taw/exception-= test.scm.go Error occurred Backtrace: Backtrace: In ice-9/boot-9.scm: 1752:10 7 (with-exception-handler _ _ #:unwind? _ # _) 1685:16 6 (raise-exception _ #:continuable? _) In /home/taw/exception-test.scm: 18:27 5 (_ _) In unknown file: 4 (backtrace #) In system/repl/debug.scm: 148:36 3 (print-frames #(# # = =E2=80=A6) =E2=80=A6) 72:20 2 (_) In ice-9/boot-9.scm: 1747:15 1 (with-exception-handler # =E2=80=A6) In system/repl/debug.scm: 72:40 0 (_) system/repl/debug.scm:72:40: In procedure string->number: Wrong type argume= nt in position 1 (expecting string): #f [23:07] ~ $ From debbugs-submit-bounces@debbugs.gnu.org Thu Sep 22 14:51:10 2022 Received: (at 55568) by debbugs.gnu.org; 22 Sep 2022 18:51:10 +0000 Received: from localhost ([127.0.0.1]:38549 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1obRHx-0006fy-NS for submit@debbugs.gnu.org; Thu, 22 Sep 2022 14:51:10 -0400 Received: from mx1.mythic-beasts.com ([46.235.224.141]:46329) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1obRAE-0006RG-DT for 55568@debbugs.gnu.org; Thu, 22 Sep 2022 14:43:12 -0400 Received: from [2.206.196.94] (port=58614 helo=dorian) by mailhub-cam-d.mythic-beasts.com with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1obRA7-005ElW-SR for 55568@debbugs.gnu.org; Thu, 22 Sep 2022 19:43:04 +0100 Date: Thu, 22 Sep 2022 20:42:56 +0200 From: Thomas White To: 55568@debbugs.gnu.org Message-ID: <20220922204256.4f44fe95@dorian> X-Mailer: Claws Mail 4.0.0git382 (GTK+ 3.24.34; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-BlackCat-Spam-Score: 24 X-Spam-Status: No, score=2.4 X-Spam-Score: -0.3 (/) X-Debbugs-Envelope-To: 55568 X-Mailman-Approved-At: Thu, 22 Sep 2022 14:51:08 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.3 (-) We discussed this again on IRC yesterday, see logs here: https://logs.guix.gnu.org/guile/2022-09-21.log#120814 We realised that the *real* underlying issue is that false-if-exception is not working in the context of my test code, i.e. when another exception handler is set up. See further info here: https://issues.guix.gnu.org/57095#5 It would still be possible to work around the immediate issue by modifying terminal-width to check the return value of getenv instead of catching the exception in string->number. However the underlying issue will be difficult to solve.