runner: async -> sync

This commit is contained in:
criyle 2021-06-05 16:16:05 -07:00
parent 963e027b0a
commit 26c5f63dcb
12 changed files with 59 additions and 119 deletions

View File

@ -133,7 +133,7 @@ type containerRunner struct {
container.ExecveParam
}
func (r *containerRunner) Run(c context.Context) <-chan runner.Result {
func (r *containerRunner) Run(c context.Context) runner.Result {
return r.Environment.Execve(c, r.ExecveParam)
}
@ -369,7 +369,10 @@ func start() (*runner.Result, error) {
c, cancel := context.WithTimeout(context.Background(), time.Duration(int64(realTimeLimit)*int64(time.Second)))
defer cancel()
s := r.Run(c)
s := make(chan runner.Result, 1)
go func() {
s <- r.Run(c)
}()
rTime := time.Now()
select {

View File

@ -40,11 +40,10 @@ func BenchmarkContainer(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
m := <-ch
for pb.Next() {
rt := m.Execve(context.TODO(), ExecveParam{
Args: []string{"/bin/echo"},
r := m.Execve(context.TODO(), ExecveParam{
Args: []string{"/bin/true"},
Env: []string{"PATH=/bin"},
})
r := <-rt
if r.Status != runner.StatusNormal {
b.Error(r.Status, r.Error)
}
@ -55,13 +54,12 @@ func BenchmarkContainer(b *testing.B) {
func TestContainerSuccess(t *testing.T) {
t.Parallel()
m := getEnv(t, nil)
rt := m.Execve(context.TODO(), ExecveParam{
Args: []string{"/bin/echo"},
r := m.Execve(context.TODO(), ExecveParam{
Args: []string{"/bin/true"},
Env: []string{"PATH=/bin"},
})
r := <-rt
if r.Status != runner.StatusNormal {
t.Fatal(r.Status, r.Error)
t.Fatal(r.Status, r.Error, r)
}
}
@ -80,11 +78,10 @@ func TestContainerSetCred(t *testing.T) {
t.Skip("root required for this test")
}
m := getEnv(t, credgen{})
rt := m.Execve(context.TODO(), ExecveParam{
Args: []string{"/bin/echo"},
r := m.Execve(context.TODO(), ExecveParam{
Args: []string{"/bin/true"},
Env: []string{"PATH=/bin"},
})
r := <-rt
if r.Status != runner.StatusNormal {
t.Fatal(r.Status, r.Error)
}
@ -93,11 +90,10 @@ func TestContainerSetCred(t *testing.T) {
func TestContainerNotExists(t *testing.T) {
t.Parallel()
m := getEnv(t, nil)
rt := m.Execve(context.TODO(), ExecveParam{
r := m.Execve(context.TODO(), ExecveParam{
Args: []string{"not_exists"},
Env: []string{"PATH=/bin"},
})
r := <-rt
if r.Status != runner.StatusRunnerError {
t.Fatal(r.Status, r.Error)
}
@ -107,14 +103,13 @@ func TestContainerSyncFuncFail(t *testing.T) {
t.Parallel()
m := getEnv(t, nil)
err := errors.New("test error")
rt := m.Execve(context.TODO(), ExecveParam{
Args: []string{"/bin/echo"},
r := m.Execve(context.TODO(), ExecveParam{
Args: []string{"/bin/true"},
Env: []string{"PATH=/bin"},
SyncFunc: func(pid int) error {
return err
},
})
r := <-rt
if r.Status != runner.StatusRunnerError {
t.Fatal(r.Status, r.Error)
}

View File

@ -97,7 +97,7 @@ func (c *containerServer) handleExecve(cmd *execCmd, msg unixsocket.Msg) error {
}
func (c *containerServer) handleExecveStarted(pid int) error {
// At this point, either recv kill / send result would be happend
// At this point, either recv kill / send result would be happened
// host -> container: kill
// container -> host: result
// container -> host: done
@ -107,7 +107,7 @@ func (c *containerServer) handleExecveStarted(pid int) error {
var ret waitPidResult
select {
case <-c.done: // socket error happend
case <-c.done: // socket error happened
return c.err
case <-c.recvCh: // kill cmd received

View File

@ -66,7 +66,7 @@ type Environment interface {
Open([]OpenCmd) ([]*os.File, error)
Delete(p string) error
Reset() error
Execve(context.Context, ExecveParam) <-chan runner.Result
Execve(context.Context, ExecveParam) runner.Result
Destroy() error
}
@ -82,8 +82,6 @@ type container struct {
recvCh chan recvReply
sendCh chan sendCmd
execveWaitCh chan execveWait
}
type recvReply struct {
@ -224,12 +222,9 @@ func (b *Builder) startContainer() (*container, error) {
recvCh: make(chan recvReply, 1),
sendCh: make(chan sendCmd, 1),
done: make(chan struct{}),
execveWaitCh: make(chan execveWait, 1),
}
go c.sendLoop()
go c.recvLoop()
go c.execveWaitLoop()
return c, nil
}

View File

@ -39,22 +39,12 @@ type ExecveParam struct {
}
// Execve runs process inside container. It accepts context cancelation as time limit exceeded.
func (c *container) Execve(ctx context.Context, param ExecveParam) <-chan runner.Result {
func (c *container) Execve(ctx context.Context, param ExecveParam) runner.Result {
c.mu.Lock()
defer c.mu.Unlock()
sTime := time.Now()
// make sure goroutine not leaked (blocked) even if result is not consumed
result := make(chan runner.Result, 1)
errResult := func(f string, v ...interface{}) <-chan runner.Result {
result <- runner.Result{
Status: runner.StatusRunnerError,
Error: fmt.Sprintf(f, v...),
}
return result
}
// if execve with fd, put fd at the first parameter
var files []int
if param.ExecFile > 0 {
@ -77,21 +67,18 @@ func (c *container) Execve(ctx context.Context, param ExecveParam) <-chan runner
ExecCmd: execCmd,
}
if err := c.sendCmd(cm, msg); err != nil {
c.mu.Unlock()
return errResult("execve: sendCmd %v", err)
}
// sync function
reply, msg, err := c.recvReply()
rep, msg, err := c.recvReply()
if err != nil {
c.mu.Unlock()
return errResult("execve: recvReply %v", err)
}
// if sync function did not involved
if reply.Error != nil || msg.Cred == nil {
if rep.Error != nil || msg.Cred == nil {
// tell kill function to exit and sync
c.execveSyncKill()
c.mu.Unlock()
return errResult("execve: no pid received or error %v", reply.Error)
return errResult("execve: no pid received or error %v", rep.Error)
}
if param.SyncFunc != nil {
if err := param.SyncFunc(int(msg.Cred.Pid)); err != nil {
@ -99,64 +86,34 @@ func (c *container) Execve(ctx context.Context, param ExecveParam) <-chan runner
c.execveSyncKill()
// tell kill function to exit and sync
c.execveSyncKill()
c.mu.Unlock()
return errResult("execve: syncfunc failed %v", err)
}
}
// send to syncFunc ack ok
if err := c.sendCmd(cmd{Cmd: cmdOk}, unixsocket.Msg{}); err != nil {
c.mu.Unlock()
return errResult("execve: ack failed %v", err)
}
c.execveWait(ctx, sTime, result)
return result
// wait for done
return c.waitForDone(ctx, sTime)
}
func (c *container) execveWait(ctx context.Context, sTime time.Time, result chan runner.Result) {
func (c *container) waitForDone(ctx context.Context, sTime time.Time) runner.Result {
mTime := time.Now()
select {
case <-c.done: // socket error
return convertReplyResult(reply{}, sTime, mTime, c.err)
// wait for result / cancel
c.execveWaitCh <- execveWait{
ctx: ctx,
sTime: sTime,
mTime: mTime,
result: result,
}
}
case <-ctx.Done(): // cancel
c.sendCmd(cmd{Cmd: cmdKill}, unixsocket.Msg{}) // kill
reply, _, _ := c.recvReply()
_, _, err := c.recvReply()
return convertReplyResult(reply, sTime, mTime, err)
type execveWait struct {
ctx context.Context
sTime time.Time
mTime time.Time
result chan runner.Result
}
func (c *container) execveWaitLoop() {
for {
waitParam := <-c.execveWaitCh
ctx := waitParam.ctx
sTime := waitParam.sTime
mTime := waitParam.mTime
result := waitParam.result
select {
case <-c.done: // socket error
result <- convertReplyResult(reply{}, sTime, mTime, c.err)
case <-ctx.Done(): // cancel
c.sendCmd(cmd{Cmd: cmdKill}, unixsocket.Msg{}) // kill
reply, _, _ := c.recvReply()
_, _, err := c.recvReply()
result <- convertReplyResult(reply, sTime, mTime, err)
case ret := <-c.recvCh: // result
c.sendCmd(cmd{Cmd: cmdKill}, unixsocket.Msg{}) // kill
_, _, err := c.recvReply()
result <- convertReplyResult(ret.Reply, sTime, mTime, err)
}
c.mu.Unlock()
case ret := <-c.recvCh: // result
c.sendCmd(cmd{Cmd: cmdKill}, unixsocket.Msg{}) // kill
_, _, err := c.recvReply()
return convertReplyResult(ret.Reply, sTime, mTime, err)
}
}
@ -196,3 +153,10 @@ func (c *container) execveSyncKill() {
c.sendCmd(cmd{Cmd: cmdKill}, unixsocket.Msg{})
c.recvReply()
}
func errResult(f string, v ...interface{}) runner.Result {
return runner.Result{
Status: runner.StatusRunnerError,
Error: fmt.Sprintf(f, v...),
}
}

4
go.mod
View File

@ -7,7 +7,7 @@ require (
github.com/elastic/go-seccomp-bpf v1.1.0
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/net v0.0.0-20210330230544-e57232859fb2
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44
golang.org/x/net v0.0.0-20210525063256-abc453219eb5
golang.org/x/sys v0.0.0-20210603125802-9665404d3644
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

11
go.sum
View File

@ -15,16 +15,17 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20210330230544-e57232859fb2 h1:nGCZOty+lVDsc4H2qPFksI5Se296+V+GhMiL/TzmYNk=
golang.org/x/net v0.0.0-20210330230544-e57232859fb2/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 h1:wjuX4b5yYQnEQHzd+CBcrcC6OVR2J1CN6mUy0oSxIPo=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 h1:Bli41pIlzTzf3KEY06n+xnzK/BESIg2ze4Pgfh/aI8c=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210603125802-9665404d3644 h1:CA1DEQ4NdKphKeL70tvsWNdT5oFh1lOjihRcEDROi0I=
golang.org/x/sys v0.0.0-20210603125802-9665404d3644/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -11,18 +11,9 @@ import (
"github.com/criyle/go-sandbox/runner"
)
// Trace starts new goroutine and trace runner with ptrace
func (t *Tracer) Trace(c context.Context) <-chan runner.Result {
result := make(chan runner.Result, 1)
go func() {
result <- t.TraceRun(c)
}()
return result
}
// TraceRun start and traces all child process by runner in the calling goroutine
// Trace start and traces all child process by runner in the calling goroutine
// parameter done used to cancel work, start is used notify child starts
func (t *Tracer) TraceRun(c context.Context) (result runner.Result) {
func (t *Tracer) Trace(c context.Context) (result runner.Result) {
// ptrace is thread based (kernel proc)
runtime.LockOSThread()
defer runtime.UnlockOSThread()

View File

@ -10,7 +10,7 @@ import (
)
// Run starts the tracing process
func (r *Runner) Run(c context.Context) <-chan runner.Result {
func (r *Runner) Run(c context.Context) runner.Result {
ch := &forkexec.Runner{
Args: r.Args,
Env: r.Env,

View File

@ -31,6 +31,6 @@ func (r Result) String() string {
return fmt.Sprintf("Result[RunnerFailed(%s)][%v %v][%v %v]", r.Error, r.Time, r.Memory, r.SetUpTime, r.RunningTime)
default:
return fmt.Sprintf("Result[%v(%s)][%v %v][%v %v]", r.Status, r.Error, r.Time, r.Memory, r.SetUpTime, r.RunningTime)
return fmt.Sprintf("Result[%v(%s %d)][%v %v][%v %v]", r.Status, r.Error, r.ExitStatus, r.Time, r.Memory, r.SetUpTime, r.RunningTime)
}
}

View File

@ -6,5 +6,5 @@ import (
// Runner interface defines method to start running
type Runner interface {
Run(context.Context) <-chan Result
Run(context.Context) Result
}

View File

@ -20,16 +20,7 @@ const (
)
// Run starts the unshared process
func (r *Runner) Run(c context.Context) <-chan runner.Result {
result := make(chan runner.Result, 1)
go func() {
result <- r.trace(c)
}()
return result
}
// Trace tracks child processes
func (r *Runner) trace(c context.Context) (result runner.Result) {
func (r *Runner) Run(c context.Context) (result runner.Result) {
ch := &forkexec.Runner{
Args: r.Args,
Env: r.Env,