[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
This commit is contained in:
tobi 2022-03-21 13:41:44 +01:00 committed by GitHub
commit 73e9cca701
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 18 deletions

View file

@ -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;