From b9ffe6bc6664f658af9e41f451b77076070ff610 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 10 Nov 2015 13:00:24 +0900 Subject: [PATCH] Attempting to fix deadlock --- goldsmith.go | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/goldsmith.go b/goldsmith.go index d1d3473..d7947f8 100644 --- a/goldsmith.go +++ b/goldsmith.go @@ -26,6 +26,7 @@ import ( "os" "path" "path/filepath" + "sync" ) const ( @@ -144,20 +145,43 @@ func (gs *goldsmith) makeStage() stage { } func (gs *goldsmith) chain(s stage, c Chainer) { - f, _ := c.(Filterer) + var ( + wg sync.WaitGroup + output = make(chan *File) + input = make(chan *File) + ) - allowed := make(chan *File) - defer close(allowed) + defer func() { + wg.Wait() + close(s.output) + }() - go c.Chain(gs, allowed, s.output) - - for file := range s.input { - if file.flags&FileFlagStatic != 0 || (f != nil && f.Filter(file.Path)) { + wg.Add(1) + go func() { + defer wg.Done() + for file := range output { s.output <- file - } else { - allowed <- file } - } + }() + + wg.Add(1) + go func() { + defer func() { + close(input) + wg.Done() + }() + + f, _ := c.(Filterer) + for file := range s.input { + if file.flags&FileFlagStatic != 0 || (f != nil && f.Filter(file.Path)) { + s.output <- file + } else { + input <- file + } + } + }() + + go c.Chain(gs, input, output) } func (gs *goldsmith) NewFile(path string) *File {