-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Fix run command race #1470
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 run command race #1470
Changes from all commits
77625c5
cb66d5b
7dbf2d2
0f2a90c
41a01f0
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 |
---|---|---|
|
@@ -6,13 +6,12 @@ package process | |
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os/exec" | ||
"sync" | ||
"time" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
) | ||
|
||
// TODO: This packages still uses a singleton for the Manager. | ||
|
@@ -101,7 +100,10 @@ func (pm *Manager) ExecDirEnv(timeout time.Duration, dir, desc string, env []str | |
stdOut := new(bytes.Buffer) | ||
stdErr := new(bytes.Buffer) | ||
|
||
cmd := exec.Command(cmdName, args...) | ||
ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
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. What happens if the 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. @bkcsoft The first line on this function has checked that. if timeout == -1 timout = 60 *time.Second 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. Do you want
? |
||
defer cancel() | ||
|
||
cmd := exec.CommandContext(ctx, cmdName, args...) | ||
cmd.Dir = dir | ||
cmd.Env = env | ||
cmd.Stdout = stdOut | ||
|
@@ -111,30 +113,14 @@ func (pm *Manager) ExecDirEnv(timeout time.Duration, dir, desc string, env []str | |
} | ||
|
||
pid := pm.Add(desc, cmd) | ||
done := make(chan error) | ||
go func() { | ||
done <- cmd.Wait() | ||
}() | ||
|
||
var err error | ||
select { | ||
case <-time.After(timeout): | ||
if errKill := pm.Kill(pid); errKill != nil { | ||
log.Error(4, "exec(%d:%s) failed to kill: %v", pid, desc, errKill) | ||
} | ||
<-done | ||
return "", "", ErrExecTimeout | ||
case err = <-done: | ||
} | ||
|
||
err := cmd.Wait() | ||
pm.Remove(pid) | ||
|
||
if err != nil { | ||
out := fmt.Errorf("exec(%d:%s) failed: %v stdout: %v stderr: %v", pid, desc, err, stdOut, stdErr) | ||
return stdOut.String(), stdErr.String(), out | ||
err = fmt.Errorf("exec(%d:%s) failed: %v(%v) stdout: %v stderr: %v", pid, desc, err, ctx.Err(), stdOut, stdErr) | ||
} | ||
|
||
return stdOut.String(), stdErr.String(), nil | ||
return stdOut.String(), stdErr.String(), err | ||
} | ||
|
||
// Kill and remove a process from list. | ||
|
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 only availably in 1.7+, we still need to support 1.6
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.
Why we need to support 1.6?
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.
Because some developers (@strk for example) still run 1.6. Latest version in Ubuntu 16.04 LTS is 1.6 as well http://packages.ubuntu.com/xenial/golang-go
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.
#1349 discussed before.
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.
Still not liking it 😂
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.
We can vendor the package but the reason is less compelling since it doesn't affect end-users. Otherwise we can postpone the PR until Ubuntu catching up with the latest.
By the way, what keeps you (@bkcsoft @strk) using Ubuntu's Go rather than the official one? I'm curious if there's anything I could help.
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.
Next LTS is 18.04, which should include 1.7+. IMO we should wait for that until droping support for 1.6
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.
Personally I've upgraded already, so don't think about me. Just think about random possible contributor staying safe in her latest debian/ubuntu/fedora stable system. We want to reduce the barriers of entry for any contributor.
We should also have Drone test all Go versions we want to support. For comparison Gogs is Travis-tested against Go versions from 1.5 to master.
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.
I don't agree that strictly supporting old toolchains would reward the project's developers and users that much, if any. Just speaking for my own opinion though. ❤️
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.
18.10 is released and have Go 1.8 so no need for this change.