fix: runtime crash (sb-ext:timeout undefined), replace with listen-based polling

- Remove handler-case + sb-ext:with-timeout 0.1 pattern entirely.
  sb-ext:timeout is a condition class, not a recognized type in the
  compilation environment, causing runtime 'undefined function' crash.
- Replace with dotimes 10 * (listen *standard-input*) + sleep 0.01
  polling loop. Same 0.1s timeout, no condition type dependencies.
- Also fix handler-bind → handler-case in tui-load.lisp so the stack
  unwinds properly (running with-terminal's shutdown-backend cleanup)
  before the crash handler runs, restoring terminal to normal state.
- Fix paren imbalance (off by 1) in the new listen-based reader code.
This commit is contained in:
2026-05-15 11:36:46 -04:00
parent c1f4ad40d2
commit 9fb4393c9c
2 changed files with 55 additions and 53 deletions

View File

@@ -956,14 +956,12 @@ supplied (e.g. \"/\"), pre-fill the select filter with it."
(setf (cl-tty.select:select-filter sel)
(subseq f 0 (1- f))))))))
(on-key ch))))))))
;; Keyboard reader with CSI escape detection
(handler-case
(sb-ext:with-timeout 0.1
(let* ((raw-ch (read-char-no-hang *standard-input* nil nil)))
(when raw-ch
(let* ((code (char-code raw-ch))
;; Keyboard reader: poll with 0.1s timeout via listen (no sb-ext:timeout)
(dotimes (_ 10)
(when (listen *standard-input*)
(let* ((raw-ch (read-char-no-hang *standard-input* nil nil))
(code (and raw-ch (char-code raw-ch)))
(esc-seq (and (= code 27)
;; Poll for up to 20ms to collect CSI bytes
(let ((b nil) (t2 nil))
(dotimes (_ 20)
(when (listen *standard-input*)
@@ -980,7 +978,8 @@ supplied (e.g. \"/\"), pre-fill the select filter with it."
(65 :up) (66 :down)
(67 :right) (68 :left)
(72 :home) (70 :end)
(otherwise :escape))))))
(otherwise :escape)))))))
(when raw-ch
(queue-event
(list :type :key
:payload (list :code code
@@ -997,8 +996,9 @@ supplied (e.g. \"/\"), pre-fill the select filter with it."
(format nil "CTRL-~a"
(code-char (+ #x60 code))))
:keyword))
(t code))))))))))))
(sb-ext:timeout ())
(t code)))))))
(return)))
(sleep 0.01))
;; Check for terminal resize (SIGWINCH sets this flag)
(when (boundp 'cl-tty.input::*terminal-resized-p*)
(when cl-tty.input::*terminal-resized-p*

View File

@@ -397,14 +397,16 @@ case "$COMMAND" in
(compile-file src :output-file fasl :verbose nil :print nil))
(load fasl :verbose nil :print nil))))
(in-package :passepartout)
(handler-bind ((error (lambda (c) (ignore-errors
(handler-case
(passepartout.channel-tui:tui-main)
(error (c)
(ignore-errors
(with-open-file (f (merge-pathnames ".cache/passepartout/tui-crash.log" (user-homedir-pathname))
:direction :output :if-exists :supersede :if-does-not-exist :create)
(format f "CRASH: ~a~%~%" c) (sb-debug:print-backtrace :count 50 :stream f) (finish-output f)))
(format t "~%=== TUI CRASH ===~%CRASH: ~a~%" c)
(format t "Full backtrace saved to ~~/.cache/passepartout/tui-crash.log~%")
(sleep 3) (finish-output) (uiop:quit 1))))
(passepartout.channel-tui:tui-main))
(sleep 3) (finish-output)))
(uiop:quit 0)
LISPEOF
# Capture terminal dimensions in non-standard env vars