Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid possible receiver panic/deadlock on sender error #18

Merged
merged 1 commit into from
Nov 22, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,33 @@ type receiver struct {

type dynamicWalker struct {
walkChan chan *currentPath
closed bool
err error
closeCh chan struct{}
}

func newDynamicWalker() *dynamicWalker {
return &dynamicWalker{
walkChan: make(chan *currentPath, 128),
closeCh: make(chan struct{}),
}
}

func (w *dynamicWalker) update(p *currentPath) error {
if w.closed {
return errors.New("walker is closed")
select {
case <-w.closeCh:
return errors.Wrap(w.err, "walker is closed")
default:
}
if p == nil {
close(w.walkChan)
return nil
}
w.walkChan <- p
return nil
select {
case w.walkChan <- p:
return nil
case <-w.closeCh:
return errors.Wrap(w.err, "walker is closed")
}
}

func (w *dynamicWalker) fill(ctx context.Context, pathC chan<- *currentPath) error {
Expand All @@ -85,6 +93,8 @@ func (w *dynamicWalker) fill(ctx context.Context, pathC chan<- *currentPath) err
}
pathC <- p
case <-ctx.Done():
w.err = ctx.Err()
close(w.closeCh)
return ctx.Err()
}
}
Expand Down Expand Up @@ -213,10 +223,13 @@ func (r *receiver) asyncDataFunc(ctx context.Context, p string, wc io.WriteClose
return err
}
err := wwc.Wait(ctx)
if err != nil {
return err
}
r.muPipes.Lock()
delete(r.pipes, id)
r.muPipes.Unlock()
return err
return nil
}

type wrappedWriteCloser struct {
Expand Down