[gambit-list] output ports created with ##open-predefined not being garbage collected
Marc Feeley
feeley at iro.umontreal.ca
Sun Apr 5 07:47:00 EDT 2020
> On Apr 5, 2020, at 1:59 AM, Alejandro Santana <santana at mailbox.org> wrote:
>
> Thanks for the explanation.
>
>> BTW, why do you need this?
>
> Just learning scheme and c. I wanted to have gambit receive s-exps over a unix domain socket. I figured
> it would be simplest if I could turn the file descriptor returned from accept into a port and call scheme's
> read on it. I can do without this though and I'd rather not have ports piling up.
>
No need to muck around with file descriptors to do this. The following program is better as it is portable to linux, macOS and Windows:
(define (start-server local-address handler)
(let ((listen-port (open-tcp-server local-address)))
(let loop ()
(handler (read listen-port))
(loop))))
(start-server "*:12345"
(lambda (conn)
(pretty-print (list 'msg= (read conn)))
(close-port conn)))
In another terminal you can send a message to the server with:
% echo "(hello world)" | nc localhost 12345
A server for handling incoming connections can be created even more simply with tcp-service-register! :
(thread-join!
(tcp-service-register!
"*:12345"
(lambda ()
(pp (list 'msg= (read))))))
> I'm curious though if there's a better way to turn a file descriptor into a port. ##open-predefined is what
> I saw mentioned when I searched the mailing list.
I have added a commit (4a9c3f8dbbbb13d587e6ffec51c5bc87bc9dc666) that fixes this issue. Now the automatic forcing on program termination will no longer happen on file descriptors passed to ##open-predefined. Your fdes.scm program no longer “leaks” memory.
Marc
More information about the Gambit-list
mailing list