Skip to content

Commit

Permalink
feat: ensure proper closing of gzip and file writers/readers to preve…
Browse files Browse the repository at this point in the history
…nt resource leaks (ethereum-optimism#11475)

* add gzipCloser struct

* add Close method

* fix OpenDecompressed func

* fix CompressByFileType func

* knit

* Apply suggestions from code review

close both even if one fails

Co-authored-by: Adrian Sutton <[email protected]>

* fix WriteCloser Close method

* fix name for more general

* fix writercloser name for more geneeral

* add construction function for WrappedCloser

* using construction func

* seperate wrapped closer struct to wrapped_closer.go

---------

Co-authored-by: Adrian Sutton <[email protected]>
  • Loading branch information
siddharth0a and ajsutton authored Aug 19, 2024
1 parent f243ad0 commit 698633c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
8 changes: 4 additions & 4 deletions op-service/ioutil/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ import (
// OpenDecompressed opens a reader for the specified file and automatically gzip decompresses the content
// if the filename ends with .gz
func OpenDecompressed(path string) (io.ReadCloser, error) {
var r io.ReadCloser
r, err := os.Open(path)
if err != nil {
return nil, err
}
if IsGzip(path) {
r, err = gzip.NewReader(r)
gr, err := gzip.NewReader(r)
if err != nil {
r.Close()
return nil, fmt.Errorf("failed to create gzip reader: %w", err)
}
return NewWrappedReadCloser(gr, r), nil
}
return r, nil
}

// OpenCompressed opens a file for writing and automatically compresses the content if the filename ends with .gz
func OpenCompressed(file string, flag int, perm os.FileMode) (io.WriteCloser, error) {
var out io.WriteCloser
out, err := os.OpenFile(file, flag, perm)
if err != nil {
return nil, err
Expand Down Expand Up @@ -70,7 +70,7 @@ func IsGzip(path string) bool {

func CompressByFileType(file string, out io.WriteCloser) io.WriteCloser {
if IsGzip(file) {
return gzip.NewWriter(out)
return NewWrappedWriteCloser(gzip.NewWriter(out), out)
}
return out
}
44 changes: 44 additions & 0 deletions op-service/ioutil/wrapped_closer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ioutil

import (
"errors"
"io"
)

// WrappedReadCloser is a struct that closes both the gzip.Reader and the underlying io.Closer.
type WrappedReadCloser struct {
io.ReadCloser
closer io.Closer
}

// WrappedWriteCloser is a struct that closes both the gzip.Writer and the underlying io.Closer.
type WrappedWriteCloser struct {
io.WriteCloser
closer io.Closer
}

// Close closes both the gzip.Reader and the underlying reader.
func (g *WrappedReadCloser) Close() error {
return errors.Join(g.ReadCloser.Close(), g.closer.Close())
}

// Close closes both the gzip.Writer and the underlying writer.
func (g *WrappedWriteCloser) Close() error {
return errors.Join(g.WriteCloser.Close(), g.closer.Close())
}

// NewWrappedReadCloser is a constructor function that initializes a WrappedReadCloser structure.
func NewWrappedReadCloser(r io.ReadCloser, c io.Closer) *WrappedReadCloser {
return &WrappedReadCloser{
ReadCloser: r,
closer: c,
}
}

// NewWrappedWriteCloser is a constructor function that initializes a WrappedWriteCloser structure.
func NewWrappedWriteCloser(r io.WriteCloser, c io.Closer) *WrappedWriteCloser {
return &WrappedWriteCloser{
WriteCloser: r,
closer: c,
}
}

0 comments on commit 698633c

Please sign in to comment.