From 73e9cca7019c15a92cb4cd320034652590513198 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Mon, 21 Mar 2022 13:41:44 +0100 Subject: [bugfix] Close ReadClosers properly in the media package (#434) * defer lock reader * close readers when finished with them * close the reader in the teereader when finished --- internal/processing/media/getfile.go | 12 +++++++++--- internal/processing/media/util.go | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) (limited to 'internal/processing') diff --git a/internal/processing/media/getfile.go b/internal/processing/media/getfile.go index 1faa8702f..c74951e38 100644 --- a/internal/processing/media/getfile.go +++ b/internal/processing/media/getfile.go @@ -180,9 +180,15 @@ func (p *processor) getAttachmentContent(ctx context.Context, requestingAccount return nil, 0, err } - // everything read from the readCloser by the media manager will be written into the bufferedWriter - teeReader := io.TeeReader(readCloser, bufferedWriter) - return teeReader, fileSize, nil + // Make a TeeReader so that everything read from the readCloser by the media manager will be written into the bufferedWriter. + // We wrap this in a teeReadCloser which implements io.ReadCloser, so that whoever uses the teeReader can close the readCloser + // when they're done with it. + trc := teeReadCloser{ + teeReader: io.TeeReader(readCloser, bufferedWriter), + close: readCloser.Close, + } + + return trc, fileSize, nil } // close the pipewriter after data has been piped into it, so the reader on the other side doesn't block; diff --git a/internal/processing/media/util.go b/internal/processing/media/util.go index 37dc87979..9739e70b7 100644 --- a/internal/processing/media/util.go +++ b/internal/processing/media/util.go @@ -20,6 +20,7 @@ package media import ( "fmt" + "io" "strconv" "strings" ) @@ -61,3 +62,16 @@ func parseFocus(focus string) (focusx, focusy float32, err error) { focusy = float32(fy) return } + +type teeReadCloser struct { + teeReader io.Reader + close func() error +} + +func (t teeReadCloser) Read(p []byte) (n int, err error) { + return t.teeReader.Read(p) +} + +func (t teeReadCloser) Close() error { + return t.close() +} -- cgit v1.2.3