Skip to content
This repository was archived by the owner on Sep 29, 2023. It is now read-only.

Commit 171b299

Browse files
anbskytiger5226
authored andcommitted
Fix out chan race condition
1 parent c034582 commit 171b299

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

ffmpeg/ffmpeg.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ type Transcoder struct {
3030
inputPipeWriter *io.WriteCloser
3131
outputPipeWriter *io.WriteCloser
3232
commandContext *context.Context
33+
done chan interface{}
3334
}
3435

3536
// New ...
3637
func New(cfg *Config) transcoder.Transcoder {
37-
return &Transcoder{config: cfg}
38+
return &Transcoder{config: cfg, done: make(chan interface{})}
3839
}
3940

4041
// Start ...
@@ -119,8 +120,9 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
119120
}()
120121

121122
go func() {
122-
defer close(out)
123123
err = cmd.Wait()
124+
t.done <- true
125+
close(out)
124126
}()
125127
} else {
126128
err = cmd.Wait()
@@ -328,9 +330,17 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress
328330
Progress.CurrentTime = currentTime
329331
Progress.Speed = currentSpeed
330332

333+
select {
334+
case <-t.done:
335+
return
336+
default:
337+
}
338+
331339
out <- *Progress
332340
}
333341
}
342+
343+
close(out)
334344
}
335345

336346
// closePipes Closes pipes if opened

0 commit comments

Comments
 (0)