Skip to content

Commit

Permalink
Merge pull request #33 from pfnet-research/kill-task-subprocesses-cle…
Browse files Browse the repository at this point in the history
…anly

Worker should kill task handler process and its descendant processes cleanly when timeout
  • Loading branch information
everpeace authored Sep 29, 2020
2 parents df55148 + 69e313c commit d0ecdb4
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.13
go-version: ^1.14
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v2
Expand Down Expand Up @@ -50,7 +50,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.13
go-version: ^1.14
id: go
- name: Check out
uses: actions/checkout@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.13
go-version: ^1.14
id: go
- name: Check out
uses: actions/checkout@v2
Expand All @@ -42,7 +42,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.13
go-version: ^1.14
id: go
- name: Check out
uses: actions/checkout@v2
Expand Down
1 change: 0 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ builds:
binary: pftaskqueue
goos:
- linux
- windows
- darwin
goarch:
- amd64
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.13.5 as builder
FROM golang:1.14 as builder
ARG RELEASE
ARG VERSION
WORKDIR /workspace
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/pfnet-research/pftaskqueue

go 1.13
go 1.14

require (
github.com/MakeNowJust/heredoc/v2 v2.0.1
Expand Down
10 changes: 9 additions & 1 deletion pkg/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"strconv"
"strings"
"sync"
"syscall"
"time"

"github.com/pfnet-research/pftaskqueue/pkg/apis/taskqueue"
Expand Down Expand Up @@ -301,7 +302,11 @@ func (w *Worker) runCommand(logger zerolog.Logger, t *task.Task) (task.TaskResul

cmdCtx, cmdCtxCancel := context.WithTimeout(w.ctx, t.Spec.ActualTimeout(w.config.TaskHandler.DefaultCommandTimeout))
defer cmdCtxCancel()
cmd := exec.CommandContext(cmdCtx, w.config.TaskHandler.Commands[0], w.config.TaskHandler.Commands[1:]...)

cmd := exec.Command(w.config.TaskHandler.Commands[0], w.config.TaskHandler.Commands[1:]...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}

// Inject workspace path to stdin
rStdout, wStdout := io.Pipe()
Expand Down Expand Up @@ -330,6 +335,9 @@ func (w *Worker) runCommand(logger zerolog.Logger, t *task.Task) (task.TaskResul
}()
select {
case <-cmdCtx.Done():
if err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); err != nil {
streamLogger.Error().Int("pid", cmd.Process.Pid).Err(err).Msg("Failed to kill the process and its descendants")
}
cmdErr = cmdCtx.Err()
case err := <-cmdDone:
cmdErr = err
Expand Down

0 comments on commit d0ecdb4

Please sign in to comment.