[gambit-list] output ports created with ##open-predefined not being garbage collected

Marc Feeley feeley at iro.umontreal.ca
Sun Apr 5 00:38:56 EDT 2020


The ##open-predefined procedure is meant for internal use by the runtime system.  It is intended to open one of the standard input/output file descriptors (i.e. with the “index” parameter is equal to -1, -2, -3, or -4).  For output file descriptors it registers an “exit job” that forces output on the file descriptor when the program terminates.  So the output ports created by ##open-predefined will always be reachable until the end of the program’s execution, and the will never detects that the port is unreachable, so it is never executed.

Note that the fact that ##open-predefined allows passing an “index” that is really a file descriptor is not something that you can rely upon, and in particular it will not work on Windows and when Gambit is configured with --enable-ansi-c.

Perhaps the ##open-predefined procedure could be augmented with an optional parameter to enable/disable the automatic forcing of output ports (a will can’t be used for this functionality because the termination of the program does not cause a “final gc” to determine which wills need to trigger).

BTW, why do you need this?

Marc

> On Apr 4, 2020, at 10:49 PM, santana at mailbox.org wrote:
> 
> Was fiddling with gambit's c-ffi and noticed that wills I attached to ports made with ##open-predefined weren't executing so I wrote some test code, if there's something wrong with my code please let me know.
> 
> # cat fdes.scm
> 
> (c-declare #<<c-declare-end
> 
> #include <sys/stat.h>	   
> #include <fcntl.h>
> #include <unistd.h>
> 
> c-declare-end
> )
> 
> (define-macro (c-macro var)
>  `(define ,var
>     ((c-lambda () int ,(string-append "___return(" (symbol->string var) ");")))))
> 
> ;; posix open flags
> (c-macro O_RDONLY)
> (c-macro O_WRONLY)
> (c-macro O_RDWR)
> (c-macro O_CREAT)
> 
> ;;  filename flags mode -> fdes | -1
> (define posix-open (c-lambda (nonnull-char-string int int) int "open"))
> ;; fdes -> 0 | -1
> (define posix-close (c-lambda (int) int "close"))
> 
> (define in 1)
> (define out 2)
> (define inout 3)
> 
> ;;;
> ;;; The will only executes if direction is in
> 
> (define (test-posix-open filename direction)
>  (let* ((fd (case direction
> 	       ((1) (posix-open filename (bitwise-ior O_RDONLY O_CREAT)  #o755))
> 	       ((2) (posix-open filename (bitwise-ior O_WRONLY O_CREAT)  #o755))
> 	       ((3) (posix-open filename (bitwise-ior O_RDWR O_CREAT)  #o755))))
> 	 (port (##open-predefined direction filename fd)))
>    (println "fd: " fd " port: " port)
>    (make-will port (lambda (p) (println "[WILL] closing port " p) (posix-close fd))))
>  (##gc))
> 
> ;;; when run with direction other than in this eats up all my ram
> 
> (define (test-loop filename direction)
>  (let ((fd (case direction
> 	       ((1) (posix-open filename (bitwise-ior O_RDONLY O_CREAT)  #o755))
> 	       ((2) (posix-open filename (bitwise-ior O_WRONLY O_CREAT)  #o755))
> 	       ((3) (posix-open filename (bitwise-ior O_RDWR O_CREAT)  #o755)))))
>    (let loop ()
>      (##open-predefined direction filename fd)
>      (loop))))
> 
> _______________________________________________
> Gambit-list mailing list
> Gambit-list at iro.umontreal.ca
> https://mailman.iro.umontreal.ca/cgi-bin/mailman/listinfo/gambit-list
> 





More information about the Gambit-list mailing list