-
Notifications
You must be signed in to change notification settings - Fork 703
Fix WaitGroup usage #581
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
Fix WaitGroup usage #581
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -560,7 +560,6 @@ func (builder *STI) Execute(command string, user string, config *api.Config) err | |
}() | ||
|
||
opts.Stdin = r | ||
defer wg.Wait() | ||
} | ||
|
||
go func(reader io.Reader) { | ||
|
@@ -591,16 +590,17 @@ func (builder *STI) Execute(command string, user string, config *api.Config) err | |
go dockerpkg.StreamContainerIO(errReader, &errOutput, func(a ...interface{}) { glog.Info(a...) }) | ||
|
||
err := builder.docker.RunContainer(opts) | ||
if util.IsTimeoutError(err) { | ||
// Cancel waiting for source input if the container timeouts | ||
wg.Done() | ||
} | ||
if e, ok := err.(errors.ContainerError); ok { | ||
// even with deferred close above, close errReader now so we avoid data race condition on errOutput; | ||
// closing will cause StreamContainerIO to exit, thus releasing the writer in the equation | ||
errReader.Close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I said, this is not a correct mechanism for cancellation, instead we will only call |
||
return errors.NewContainerError(config.BuilderImage, e.ErrorCode, errOutput) | ||
} | ||
// Do not wait for source input if the container times out. | ||
// FIXME: this potentially leaks a goroutine. | ||
if !util.IsTimeoutError(err) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removing the !config.LayeredBuild check of the prior fix seems incorrect to me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See https://play.golang.org/p/kpKJh9Prei If you never call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On Thursday, September 1, 2016, Rodolfo Carvalho notifications@github.com
|
||
wg.Wait() | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally we'd drop a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think we need the if-check around the wg.Wait() because the bad scenario would be:
which is the original hang we were trying to fix. that's not to say a rewrite couldn't improve it, but at least as it stands now i believe it's necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bparees thanks for challenging my tired mind :)
This means that after err := builder.docker.RunContainer(opts)
Yes, and this causes a goroutine leak.
Yes.
When There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bparees does this make sense to you after all? In my mind we're trading a panic for a leak, and based on previous discussions this is a "less critical" leak because this code is running in the builder pod and the process is short-lived. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we're in agreement, my point was that we need the "if !util.IsTimeoutError(err)" check. It sounded to me like you were implying we might not need it, but on rereading I guess all you were saying is "ideally we'd refactor this code so this check is not needed, but until we do that, it is needed, so here it is" so i think we're good (and yeah, goroutine leak. oh well. luckily this code is running in a short lived container in openshift, or a short lived process in s2i) |
||
return err | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very tricky defer statement, there is no clean way to "cancel" it.
Calling
wg.Done()
earlier introduces a concurrency problem. As @bparees and @gabemontero noted,wg.Done()
could end up being called multiple times sending the counter below 0, causing a panic.Correct code arranges to call
wg.Done()
as many times aswg.Add(n)
in all cases.