From f3e8a0b72fc7b7f1fe5a00700c7cdd3556fc41cb Mon Sep 17 00:00:00 2001 From: Tim Scheuermann Date: Fri, 16 Dec 2022 16:29:26 +0100 Subject: [PATCH] [MM-49003] Close the pipe reader when done reading (#21854) --- jobs/export_process/worker.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/jobs/export_process/worker.go b/jobs/export_process/worker.go index 2697b659804..ddf9462b144 100644 --- a/jobs/export_process/worker.go +++ b/jobs/export_process/worker.go @@ -44,26 +44,24 @@ func MakeWorker(jobServer *jobs.JobServer, app AppIface) model.Worker { rd, wr := io.Pipe() - errCh := make(chan *model.AppError, 1) go func() { - defer close(errCh) - // Try to write without a timeout _, appErr := app.WriteFileContext(context.Background(), rd, filepath.Join(outPath, exportFilename)) - errCh <- appErr + if appErr != nil { + // we close the reader here to prevent a deadlock when the bulk exporter tries to + // write into the pipe while app.WriteFile has already returned. The error will be + // returned by the writer part of the pipe when app.BulkExport tries to call + // wr.Write() on it. + rd.CloseWithError(appErr) // CloseWithError never returns an error + } }() appErr := app.BulkExport(request.EmptyContext(app.Log()), wr, outPath, opts) - if err := wr.Close(); err != nil { - mlog.Warn("Worker: error closing writer") - } + wr.Close() // Close never returns an error if appErr != nil { return appErr } - if appErr := <-errCh; appErr != nil { - return appErr - } return nil } worker := jobs.NewSimpleWorker(jobName, jobServer, execute, isEnabled)