bug#62590: Strange put-char encoding-error "conversion to port encoding failed"

Christopher Baines <[email protected]> Tue, 11 Feb 2025 19:06:46 +0000
Newsgroups gmane.lisp.guile.bugs
Message-ID <[email protected]>
Christopher Baines <[email protected]> writes:

> Christopher Baines <[email protected]> writes:
>
>> I've been seeing some strange occasional errors for a while now.
>
> ...
>
>> I'm not sure what the 84 means? If I'm picking out the characters that
>> have failed to be encoded, I'm not sure why some of these would fail
>> (like space or w)?
>>
>> Unfortunately I'm not sure how to reproduce this. Suspendable ports
>> might be a factor here as that is probably in use.
>
> Still seeing these errors, including today. I've managed to stumble on a
> reliable and somewhat minimal way of reproducing them:

Here's a slightly simplified version with some comments:
put-char-exn.scm (text/plain, 2.2 KB)
(use-modules (ice-9 binary-ports)
             (rnrs bytevectors)
             (ice-9 exceptions)
             (ice-9 threads)
             (system repl debug))

(setvbuf (current-output-port) 'line)
(setvbuf (current-error-port) 'line)

(simple-format (current-error-port)
               "current-output-port: ~A (encoding: ~A)\n\n"
               (current-output-port)
               (port-encoding (current-output-port)))

(for-each
 (lambda (i)
   (call-with-new-thread
    (lambda ()
      (with-exception-handler
          (lambda (exn)
            (let ((stack (make-stack #t)))
              (print-frames (stack->vector stack)
                            (current-error-port)
                            #:count (stack-length stack))
              (print-exception
               (current-error-port)
               (stack-ref stack 3)
               '%exception
               (list exn)))
            (force-output (current-error-port))

            (primitive-exit 1))
        (lambda ()
          (while #t
            (with-exception-handler
                (lambda (exn)
                  ;; The put-char error seems to happen around here
                  (simple-format (current-output-port)
                                 "exception: ~A\n"
                                 exn)
                  #f)
              (lambda ()
                (with-throw-handler #t
                  (lambda ()
                    ;; Cause some error, I think any error will do
                    (+ 'a 1))
                  (lambda (key . args)
                    (let* ((stack (make-stack #t 3))
                           (backtrace
                            (call-with-output-string
                              (lambda (port)
                                (display-backtrace stack port)
                                (newline port)))))
                      (display
                       ;; Printing the backtrace seems significant,
                       ;; other strings don't seem to reproduce the
                       ;; behaviour
                       backtrace
                       (current-output-port))
                      #f))))
              #:unwind? #t)))))))
 (iota 2))

(sleep 60)
signature.asc (application/pgp-signature, 987 B)
-----BEGIN PGP SIGNATURE-----

iQKlBAEBCgCPFiEEPonu50WOcg2XVOCyXiijOwuE9XcFAmern8ZfFIAAAAAALgAo
aXNzdWVyLWZwckBub3RhdGlvbnMub3BlbnBncC5maWZ0aGhvcnNlbWFuLm5ldDNF
ODlFRUU3NDU4RTcyMEQ5NzU0RTBCMjVFMjhBMzNCMEI4NEY1NzcRHG1haWxAY2Jh
aW5lcy5uZXQACgkQXiijOwuE9Xdi4BAAtY7hLktaWyA0vDzb4SnFTBdHZ4VaweHh
wg9q1KpNciI98Nx+uGJVlxIc7nLf1Vwfx15V7PkJvXS5QuoYpgT7m+6+utctFh8r
Fknd6iLhnawD2Yea/PqFTQs14tbZAlXKTYNmkv3Pmy/2lSHEHJinmg779tpbBdr5
xbUt+YRg0FBE9NhJ5s3rdyLwrrXTr1+GWEbTa8LrkmwJQglnt5+PQWevy5TLDc4t
WlXLN9tUYFOwJFAqcurA4JrbD0S/goYQkdslHdGbIbSx6p+6k4ID2P5dm0jWs4ZJ
KgqmrJ2uTjhHYhfxPlfcA6QTyuW828xl112is2aCXNAYahndeMDQFsN9P+RElVH5
naOVkBUT96Ift5q2YTBcBL8Q+MtGp8a6Zea8HgHUxbnWMFLChvsh1hJXPtnww6UZ
IZv+U5JScnTMknXKrFZ9OQ4R4iPJ1pZFYKe+1y4kD/ucw/0R2TydwQp2VEpd8BE1
eJhLz2gJ0ErLMcgeySvXrapH39e51x4i7JZ+ymtsTiLpzrF1BJvgBWBka26jpXWj
aTMEjBNTbzlI5EPPaB1vbgbkCA2Kh7PJjCeoRi2+fucU9+cBCUGlTFxat1Eyy4Bp
h7VCcMVmIGDjcCWSAP2jNfW1fZ/La5prD42/ldKZRj5s+3vvQPe3Cfqe0GL57L0r
8gkGqEbs5Tg=
=M4jt
-----END PGP SIGNATURE-----