[drakma-devel] Closing streams with :want-stream t

Chris Dean ctdean at sokitomi.com
Sat Feb 3 02:27:34 UTC 2007


I have a file handle leak in my code (that is, I run out of file
handles after running for a while) and I suspect that I'm not using
drakma correctly.

I wish to use the :want-stream t parameter and read the resulting
stream directly, so I have this code:

  (defun simple-get (url)
    "Download the url using GET and return the body as a string."
    (handler-case
        (multiple-value-bind (stream code headers dummy-uri dummy-stream 
                                     must-close?)
            (drakma:http-request url :want-stream t :keep-alive nil 
                                     :method :get)
          (declare (ignore headers dummy-stream dummy-uri))
          (unwind-protect 
               (and stream
                    code
                    (= code 200)
                    (with-output-to-string (out)
                      (do ((ch (read-char stream nil :eof) 
                               (read-char stream nil :eof)))
                          ((not (characterp ch)))
                        (princ ch out))))
            (when (and stream must-close?)
              (ignore-errors (close stream)))))
      (error (condition) 
        (format t "Error ~s: ~a~%" url condition)
        nil)))

Is there anything extra I need to do to make sure that all the streams
opened by drakma are closed?

My production code is much more complex, but the simple stub above
will generate the out of file handles problem.  Besides the actual
error I can use lsof on Linux and Mac OS and see many sockets stuck in
CLOSED or CLOSE_WAIT states.  This is all under LispWorks 5.0.1

Also, when using :want-stream nil I never encounter the problem.

Cheers,
Chris Dean



More information about the Drakma-devel mailing list