From: John Cowan <xxxxxx@ccil.org>
Date: Tue, Sep 3, 2019 at 5:56 PM

So here's the generator:

(define make-directory-files-generator
  (case-lambda
    ((dir dot-files?) (make-directory-files-generator* dir dot-files?))
    ((dir) (make-directory-files-generator* dir #f))))

(define (make-directory-files-generator* dir dot-files?)
  (let ((dir-obj (open-directory dir dot-files?))
          (eof (eof-object)))
    (lambda ()
      (let ((f (read-directory dir-obj)))
        (if (eq? f eof) (close-directory dir-obj))
        f))))


I haven't actually tested this, but it's at least conceptually correct. 

It works through the close-directory operation, after which it continues to attempt to read the closed dir-obj, raising a low-level POSIX exception.  On reading the generator SRFI 158, I note:

After a finite generator has generated all its values, it will return an end-of-file object for all subsequent calls.

How important is it to remedy this?

[ We're going to keep the "mostly harmless" directory-files.  In prior discussions we liked directory-fold, but decided not to expose it. ]

- Harold