Weird behaviour of SRFI-127 lseq in a multithreaded program

Stanislav Kljuhhin via Chicken-users <[email protected]> Sun, 22 Feb 2026 19:22:16 +0100
Newsgroups gmane.lisp.scheme.chicken
Message-ID <[email protected]>
Hello,

Consider the following minimal example:
 
---8<---
(import srfi-18)
(import srfi-121)
(import srfi-127)

(define (emit yield)
  (let loop ((i 1))
    (thread-sleep! 1)
    (yield "hello")
    (when (< i 10)
      (loop (add1 i)))))

(define (collect s)
  (display s)
  (newline))

(define (lseq-for-each* proc lseq)
  (unless (null? lseq)
    (proc (lseq-car lseq))
    (lseq-for-each* proc (lseq-cdr lseq))))

(lseq-for-each collect (generator->lseq (make-coroutine-generator emit)))
---8<—--

When I run this program (compiled or interpreted, makes no difference), nothing
happens for 10 seconds, then 10 hello's are printed at once. But if I use the
in-place implementation lseq-for-each*, then the program behaves as one might
expect, i.e. hello's are printed with a 1-second interval.

Here I used a simplified implementation of lseq-for-each*, just for brevity, but
we can copy the SRFI-127 implementation verbatim, and it still works the same.

Can you help me understand what is going on, please?

—
Stan