From 8968aa93371c2098ca0104f7221151ac4a2c66ab Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 25 May 2021 00:36:31 +0000 Subject: [PATCH] fix(storage): make Writer.CloseWithError block until writer exits Writer.Close blocks on w.donec to return only after the spawned writer goroutine has exited. Previously CloseWithError did not block on this channel however, meaning it could return while the writer goroutine was still running, which could then lead to a caller e.g. closing the underlying Client, leading to a crash in the still-running goroutine. This adds the same block on the channel to CloseWithError to ensure that however a caller closes a Writer, it is fully closed when the close method returns and that caller can safely Close the client. --- storage/writer.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/storage/writer.go b/storage/writer.go index 1843a814155..c50388bbd57 100644 --- a/storage/writer.go +++ b/storage/writer.go @@ -258,7 +258,9 @@ func (w *Writer) CloseWithError(err error) error { if !w.opened { return nil } - return w.pw.CloseWithError(err) + err = w.pw.CloseWithError(err) + <-w.donec + return err } // Attrs returns metadata about a successfully-written object.