Skip to content

Commit 8d3c46e

Browse files
committed
fix: timeout is not respected
1 parent 38c9cb0 commit 8d3c46e

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

pkg/kbagent/service/command.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"os"
2929
"os/exec"
3030
"strings"
31+
"syscall"
3132
"time"
3233

3334
"github.com/apecloud/kubeblocks/pkg/kbagent/proto"
@@ -144,6 +145,11 @@ func runCommandX(ctx context.Context, action *proto.ExecAction, parameters map[s
144145
cmd.Stdin = stdinReader
145146
cmd.Stdout = stdoutWriter
146147
cmd.Stderr = stderrWriter
148+
cmd.WaitDelay = time.Second * 1
149+
// gracefully terminate, go will kill it after waitDelay
150+
cmd.Cancel = func() error {
151+
return cmd.Process.Signal(syscall.SIGTERM)
152+
}
147153

148154
errChan := make(chan error, 1)
149155
go func() {

pkg/kbagent/service/command_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,22 +271,20 @@ var _ = Describe("command", func() {
271271
action := &proto.ExecAction{
272272
Commands: []string{"/bin/bash", "-c", "command-not-found"},
273273
}
274-
output, err := runCommand(ctx, action, nil, nil)
274+
_, err := runCommand(ctx, action, nil, nil)
275275
Expect(err).ShouldNot(BeNil())
276276
Expect(errors.Is(err, proto.ErrFailed)).Should(BeTrue())
277277
Expect(err.Error()).Should(ContainSubstring("command not found"))
278-
Expect(output).Should(BeNil())
279278
})
280279

281280
It("timeout", func() {
282281
action := &proto.ExecAction{
283282
Commands: []string{"/bin/bash", "-c", "sleep 60"},
284283
}
285284
timeout := int32(1)
286-
output, err := runCommand(ctx, action, nil, &timeout)
285+
_, err := runCommand(ctx, action, nil, &timeout)
287286
Expect(err).ShouldNot(BeNil())
288287
Expect(errors.Is(err, proto.ErrTimedOut)).Should(BeTrue())
289-
Expect(output).Should(BeNil())
290288
})
291289
})
292290
})

0 commit comments

Comments
 (0)