mirror of
https://git.in.rschanz.org/ryan77627/guix.git
synced 2024-12-24 21:38:07 -05:00
zlib: Fix memory leak due to revealed ports not being GC'd.
Fixes <https://bugs.gnu.org/28784>.
This mostly reverts 81a0f1cdf1
, which
introduced a regression: revealed ports are *never* GC'd (contrary to
what Guile's manual suggests).
In addition to the revert, 'close-procedure' now explicitly swallows
EBADF errors when 'close-port' is called.
* guix/zlib.scm (close-procedure): New procedure.
(make-gzip-input-port)[gzfile]: Use 'fileno' instead of 'port->fdes'.
Use 'close-procedure' instead of 'gzclose'.
(make-gzip-output-port): Likewise.
* tests/zlib.scm ("compression/decompression pipe"): Use 'port-closed?'
to determine whether PARENT has been closed.
This commit is contained in:
parent
5781c7dd27
commit
85a2b58987
2 changed files with 30 additions and 20 deletions
|
@ -149,6 +149,31 @@ (define %default-compression-level
|
||||||
;; Z_DEFAULT_COMPRESSION.
|
;; Z_DEFAULT_COMPRESSION.
|
||||||
-1)
|
-1)
|
||||||
|
|
||||||
|
(define (close-procedure gzfile port)
|
||||||
|
"Return a procedure that closes GZFILE, ensuring its underlying PORT is
|
||||||
|
closed even if closing GZFILE triggers an exception."
|
||||||
|
(let-syntax ((ignore-EBADF
|
||||||
|
(syntax-rules ()
|
||||||
|
((_ exp)
|
||||||
|
(catch 'system-error
|
||||||
|
(lambda ()
|
||||||
|
exp)
|
||||||
|
(lambda args
|
||||||
|
(unless (= EBADF (system-error-errno args))
|
||||||
|
(apply throw args))))))))
|
||||||
|
|
||||||
|
(lambda ()
|
||||||
|
(catch 'zlib-error
|
||||||
|
(lambda ()
|
||||||
|
;; 'gzclose' closes the underlying file descriptor. 'close-port'
|
||||||
|
;; calls close(2) and gets EBADF, which we swallow.
|
||||||
|
(gzclose gzfile)
|
||||||
|
(ignore-EBADF (close-port port)))
|
||||||
|
(lambda args
|
||||||
|
;; Make sure PORT is closed despite the zlib error.
|
||||||
|
(ignore-EBADF (close-port port))
|
||||||
|
(apply throw args))))))
|
||||||
|
|
||||||
(define* (make-gzip-input-port port #:key (buffer-size %default-buffer-size))
|
(define* (make-gzip-input-port port #:key (buffer-size %default-buffer-size))
|
||||||
"Return an input port that decompresses data read from PORT, a file port.
|
"Return an input port that decompresses data read from PORT, a file port.
|
||||||
PORT is automatically closed when the resulting port is closed. BUFFER-SIZE
|
PORT is automatically closed when the resulting port is closed. BUFFER-SIZE
|
||||||
|
@ -158,11 +183,7 @@ (define* (make-gzip-input-port port #:key (buffer-size %default-buffer-size))
|
||||||
(define gzfile
|
(define gzfile
|
||||||
(match (drain-input port)
|
(match (drain-input port)
|
||||||
("" ;PORT's buffer is empty
|
("" ;PORT's buffer is empty
|
||||||
;; Since 'gzclose' will eventually close the file descriptor beneath
|
(gzdopen (fileno port) "r"))
|
||||||
;; PORT, we increase PORT's revealed count and never call 'close-port'
|
|
||||||
;; on PORT since we would get EBADF if 'gzclose' already closed it (on
|
|
||||||
;; 2.0 EBADF is swallowed by 'fport_close' but on 2.2 it is raised).
|
|
||||||
(gzdopen (port->fdes port) "r"))
|
|
||||||
(_
|
(_
|
||||||
;; This is unrecoverable but it's better than having the buffered input
|
;; This is unrecoverable but it's better than having the buffered input
|
||||||
;; be lost, leading to unclear end-of-file or corrupt-data errors down
|
;; be lost, leading to unclear end-of-file or corrupt-data errors down
|
||||||
|
@ -177,8 +198,7 @@ (define (read! bv start count)
|
||||||
(gzbuffer! gzfile buffer-size))
|
(gzbuffer! gzfile buffer-size))
|
||||||
|
|
||||||
(make-custom-binary-input-port "gzip-input" read! #f #f
|
(make-custom-binary-input-port "gzip-input" read! #f #f
|
||||||
(lambda ()
|
(close-procedure gzfile port)))
|
||||||
(gzclose gzfile))))
|
|
||||||
|
|
||||||
(define* (make-gzip-output-port port
|
(define* (make-gzip-output-port port
|
||||||
#:key
|
#:key
|
||||||
|
@ -190,7 +210,7 @@ (define* (make-gzip-output-port port
|
||||||
(define gzfile
|
(define gzfile
|
||||||
(begin
|
(begin
|
||||||
(force-output port) ;empty PORT's buffer
|
(force-output port) ;empty PORT's buffer
|
||||||
(gzdopen (port->fdes port)
|
(gzdopen (fileno port)
|
||||||
(string-append "w" (number->string level)))))
|
(string-append "w" (number->string level)))))
|
||||||
|
|
||||||
(define (write! bv start count)
|
(define (write! bv start count)
|
||||||
|
@ -200,8 +220,7 @@ (define (write! bv start count)
|
||||||
(gzbuffer! gzfile buffer-size))
|
(gzbuffer! gzfile buffer-size))
|
||||||
|
|
||||||
(make-custom-binary-output-port "gzip-output" write! #f #f
|
(make-custom-binary-output-port "gzip-output" write! #f #f
|
||||||
(lambda ()
|
(close-procedure gzfile port)))
|
||||||
(gzclose gzfile))))
|
|
||||||
|
|
||||||
(define* (call-with-gzip-input-port port proc
|
(define* (call-with-gzip-input-port port proc
|
||||||
#:key (buffer-size %default-buffer-size))
|
#:key (buffer-size %default-buffer-size))
|
||||||
|
|
|
@ -57,16 +57,7 @@ (define-module (test-zlib)
|
||||||
(match (waitpid pid)
|
(match (waitpid pid)
|
||||||
((_ . status)
|
((_ . status)
|
||||||
(and (zero? status)
|
(and (zero? status)
|
||||||
|
(port-closed? parent)
|
||||||
;; PORT itself isn't closed but its underlying file
|
|
||||||
;; descriptor must have been closed by 'gzclose'.
|
|
||||||
(catch 'system-error
|
|
||||||
(lambda ()
|
|
||||||
(seek (fileno parent) 0 SEEK_CUR)
|
|
||||||
#f)
|
|
||||||
(lambda args
|
|
||||||
(= EBADF (system-error-errno args))))
|
|
||||||
|
|
||||||
(bytevector=? received data))))))))))))
|
(bytevector=? received data))))))))))))
|
||||||
|
|
||||||
(test-end)
|
(test-end)
|
||||||
|
|
Loading…
Reference in a new issue