Stumped by read-unbuffered behavior in comparse port
"Diego A. Mundo via Chicken-users" <[email protected]> Wed, 11 Mar 2026 06:30:07 +0000
| Newsgroups | gmane.lisp.scheme.chicken |
|---|---|
| Message-ID | <4xiDC6pLD1LC3uCI2vDCpSMZ-0wLyjAlcSo7XwmkXUUBNS0q2pobfoyTnFnXS3wraDLoGsRmMNTDOk1OvVVx_PM0qp6__w3aViwduHdaUpM=@pm.me> |
I have an in-progress port of comparse to CHICKEN 6 here:
https://git.sr.ht/~dieggsy/comparse
There was one failing test concerning reading from a custom port created using make-input port (which I've modified from the original for C6):
(define (make-unbuffered-port s)
(let ((in (open-input-string s)))
(make-input-port (lambda () (read-char in))
(lambda () (char-ready? in))
(lambda () (close-input-port in))
#:peek-char
(lambda () (peek-char in))
#:read-bytevector
(lambda (port bytes bytevector offset)
(read-bytevector! bytevector in offset (+ offset bytes)))
#:read-line
(lambda (port max)
(read-line in max)))))
(test-group "unbuffered ports"
(let ((up (make-unbuffered-port "foo bar")))
(test "foo bar" (parse (char-seq "foo bar") up))))
It seems to be some kind of memory leak, and only happens in compiled code, while the test passes in interpreted code. In particular, the issue seems to be in this read-unbuffered:
(define (read-chunk in)
(and (not (eof-object? (peek-char in)))
(let ((chunk (read-buffered in)))
(if (string-null? chunk)
(read-string 1 in)
chunk))))
Then in the top commit, I simply print out the result of (read-unbuffered in) inline, and somehow this fixes the test... ???
(define (pro o)
(write o)
(newline)
o)
(define (read-chunk in)
(and (not (eof-object? (peek-char in)))
(let ((chunk (pro (read-buffered in))))
(if (string-null? chunk)
(read-string 1 in)
chunk))))
Help, I have no idea what I'm looking at.
Diego