Skip to content

Commit 45addf8

Browse files
committed
Revert "fix: add a 1m timeout to signal shutdown (#92)"
This reverts commit 0d37a62.
1 parent 9b6f446 commit 45addf8

File tree

5 files changed

+15
-87
lines changed

5 files changed

+15
-87
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ The environment variables can be used to configure various aspects of the inner
2727
| `CODER_CPUS` | Dictates the number of CPUs to allocate the inner container. It is recommended to set this using the Kubernetes [Downward API](https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#use-container-fields-as-values-for-environment-variables). | false |
2828
| `CODER_MEMORY` | Dictates the max memory (in bytes) to allocate the inner container. It is recommended to set this using the Kubernetes [Downward API](https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#use-container-fields-as-values-for-environment-variables). | false |
2929
| `CODER_DISABLE_IDMAPPED_MOUNT` | Disables idmapped mounts in sysbox. For more information, see the [Sysbox Documentation](https://github.com/nestybox/sysbox/blob/master/docs/user-guide/configuration.md#disabling-id-mapped-mounts-on-sysbox). | false |
30-
| `CODER_SHUTDOWN_TIMEOUT` | Configure a custom shutdown timeout to wait for the boostrap command to exit. Defaults to 1 minute. | false |
3130

3231
## Coder Template
3332

cli/docker.go

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"sort"
1414
"strconv"
1515
"strings"
16-
"time"
1716

1817
dockertypes "github.com/docker/docker/api/types"
1918
"github.com/docker/docker/api/types/container"
@@ -40,10 +39,6 @@ const (
4039
// EnvBoxContainerName is the name of the inner user container.
4140
EnvBoxPullImageSecretEnvVar = "CODER_IMAGE_PULL_SECRET" //nolint:gosec
4241
EnvBoxContainerName = "CODER_CVM_CONTAINER_NAME"
43-
// We define a custom exit code to distinguish from the generic '1' when envbox exits due to a shutdown timeout.
44-
// Docker claims exit codes 125-127 so we start at 150 to
45-
// ensure we don't collide.
46-
ExitCodeShutdownTimeout = 150
4742
)
4843

4944
const (
@@ -82,9 +77,8 @@ const (
8277
// with UID/GID 1000 will be mapped to `UserNamespaceOffset` + 1000
8378
// on the host. Changing this value will result in improper mappings
8479
// on existing containers.
85-
UserNamespaceOffset = 100000
86-
devDir = "/dev"
87-
defaultShutdownTimeout = time.Minute
80+
UserNamespaceOffset = 100000
81+
devDir = "/dev"
8882
)
8983

9084
var (
@@ -108,7 +102,6 @@ var (
108102
EnvDockerConfig = "CODER_DOCKER_CONFIG"
109103
EnvDebug = "CODER_DEBUG"
110104
EnvDisableIDMappedMount = "CODER_DISABLE_IDMAPPED_MOUNT"
111-
EnvShutdownTimeout = "CODER_SHUTDOWN_TIMEOUT"
112105
)
113106

114107
var envboxPrivateMounts = map[string]struct{}{
@@ -146,7 +139,6 @@ type flags struct {
146139
cpus int
147140
memory int
148141
disableIDMappedMount bool
149-
shutdownTimeout time.Duration
150142

151143
// Test flags.
152144
noStartupLogs bool
@@ -358,7 +350,6 @@ func dockerCmd(ch chan func() error) *cobra.Command {
358350
cliflag.IntVarP(cmd.Flags(), &flags.cpus, "cpus", "", EnvCPUs, 0, "Number of CPUs to allocate inner container. e.g. 2")
359351
cliflag.IntVarP(cmd.Flags(), &flags.memory, "memory", "", EnvMemory, 0, "Max memory to allocate to the inner container in bytes.")
360352
cliflag.BoolVarP(cmd.Flags(), &flags.disableIDMappedMount, "disable-idmapped-mount", "", EnvDisableIDMappedMount, false, "Disable idmapped mounts in sysbox. Note that you may need an alternative (e.g. shiftfs).")
361-
cliflag.DurationVarP(cmd.Flags(), &flags.shutdownTimeout, "shutdown-timeout", "", EnvShutdownTimeout, defaultShutdownTimeout, "Duration after which envbox will be forcefully terminated.")
362353

363354
// Test flags.
364355
cliflag.BoolVarP(cmd.Flags(), &flags.noStartupLogs, "no-startup-log", "", "", false, "Do not log startup logs. Useful for testing.")
@@ -735,38 +726,27 @@ func runDockerCVM(ctx context.Context, log slog.Logger, client dockerutil.Docker
735726
return xerrors.Errorf("exec inspect: %w", err)
736727
}
737728

738-
shutdownCh <- killBootstrapCmd(ctx, log, bootstrapPID, bootstrapExec.ID, client, flags.shutdownTimeout)
729+
shutdownCh <- func() error {
730+
log.Debug(ctx, "killing container", slog.F("bootstrap_pid", bootstrapPID))
739731

740-
return nil
741-
}
742-
743-
// KillBootstrapCmd is the command we run when we receive a signal
744-
// to kill the envbox container.
745-
func killBootstrapCmd(ctx context.Context, log slog.Logger, pid int, execID string, client dockerutil.DockerClient, timeout time.Duration) func() error {
746-
return func() error {
747-
log.Debug(ctx, "killing container",
748-
slog.F("bootstrap_pid", pid),
749-
slog.F("timeout", timeout.String()),
750-
)
751-
752-
ctx, cancel := context.WithTimeout(ctx, timeout)
753-
defer cancel()
754732
// The PID returned is the PID _outside_ the container...
755733
//nolint:gosec
756-
out, err := exec.CommandContext(ctx, "kill", "-TERM", strconv.Itoa(pid)).CombinedOutput()
734+
out, err := exec.Command("kill", "-TERM", strconv.Itoa(bootstrapPID)).CombinedOutput()
757735
if err != nil {
758736
return xerrors.Errorf("kill bootstrap process (%s): %w", out, err)
759737
}
760738

761739
log.Debug(ctx, "sent kill signal waiting for process to exit")
762-
err = dockerutil.WaitForExit(ctx, client, execID)
740+
err = dockerutil.WaitForExit(ctx, client, bootstrapExec.ID)
763741
if err != nil {
764742
return xerrors.Errorf("wait for exit: %w", err)
765743
}
766744

767745
log.Debug(ctx, "bootstrap process successfully exited")
768746
return nil
769747
}
748+
749+
return nil
770750
}
771751

772752
//nolint:revive

cmd/envbox/main.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"runtime"
99
"syscall"
1010

11-
"golang.org/x/xerrors"
12-
1311
"cdr.dev/slog"
1412
"cdr.dev/slog/sloggers/slogjson"
1513
"github.com/coder/envbox/cli"
@@ -31,9 +29,6 @@ func main() {
3129
err := fn()
3230
if err != nil {
3331
log.Error(ctx, "shutdown function failed", slog.Error(err))
34-
if xerrors.Is(err, context.DeadlineExceeded) {
35-
os.Exit(cli.ExitCodeShutdownTimeout)
36-
}
3732
os.Exit(1)
3833
}
3934
default:

integration/docker_test.go

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,10 @@ func TestDocker(t *testing.T) {
306306
require.Error(t, err)
307307

308308
// Simulate a shutdown.
309-
exitCode := integrationtest.StopContainer(t, pool, resource.Container.ID, 30*time.Second)
310-
require.Equal(t, 0, exitCode)
309+
integrationtest.StopContainer(t, pool, resource.Container.ID, 30*time.Second)
310+
311+
err = resource.Close()
312+
require.NoError(t, err)
311313

312314
t.Logf("envbox %q started successfully, recreating...", resource.Container.ID)
313315
// Run the envbox container.
@@ -324,34 +326,6 @@ func TestDocker(t *testing.T) {
324326
})
325327
require.NoError(t, err)
326328
})
327-
328-
t.Run("ShutdownTimeout", func(t *testing.T) {
329-
t.Parallel()
330-
331-
pool, err := dockertest.NewPool("")
332-
require.NoError(t, err)
333-
334-
var (
335-
tmpdir = integrationtest.TmpDir(t)
336-
binds = integrationtest.DefaultBinds(t, tmpdir)
337-
)
338-
339-
envs := []string{fmt.Sprintf("%s=%s", cli.EnvShutdownTimeout, "1s")}
340-
341-
// Run the envbox container.
342-
resource := integrationtest.RunEnvbox(t, pool, &integrationtest.CreateDockerCVMConfig{
343-
Image: integrationtest.UbuntuImage,
344-
Username: "root",
345-
Envs: envs,
346-
Binds: binds,
347-
BootstrapScript: sigtrapForeverScript,
348-
})
349-
350-
// Simulate a shutdown.
351-
exitCode := integrationtest.StopContainer(t, pool, resource.Container.ID, 30*time.Second)
352-
// We expect it to timeout which should result in a special exit code.
353-
require.Equal(t, cli.ExitCodeShutdownTimeout, exitCode)
354-
})
355329
}
356330

357331
func requireSliceNoContains(t *testing.T, ss []string, els ...string) {
@@ -384,20 +358,6 @@ func bindMount(src, dest string, ro bool) string {
384358
return fmt.Sprintf("%s:%s", src, dest)
385359
}
386360

387-
const sigtrapForeverScript = `#!/bin/bash
388-
cleanup() {
389-
echo "Got a signal, going to sleep!" && sleep infinity
390-
exit 0
391-
}
392-
393-
trap 'cleanup' INT TERM
394-
395-
while true; do
396-
echo "Working..."
397-
sleep 1
398-
done
399-
`
400-
401361
const sigtrapScript = `#!/bin/bash
402362
cleanup() {
403363
echo "HANDLING A SIGNAL!" && touch /home/coder/foo && echo "touched file"

integration/integrationtest/docker.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,7 @@ func RunEnvbox(t *testing.T, pool *dockertest.Pool, conf *CreateDockerCVMConfig)
9191
host.CPUQuota = int64(conf.CPUs) * int64(dockerutil.DefaultCPUPeriod)
9292
})
9393
require.NoError(t, err)
94-
t.Cleanup(func() {
95-
// Only delete the container if the test passes.
96-
if !t.Failed() {
97-
resource.Close()
98-
}
99-
})
94+
// t.Cleanup(func() { _ = pool.Purge(resource) })
10095

10196
waitForCVM(t, pool, resource)
10297

@@ -269,7 +264,7 @@ func ExecEnvbox(t *testing.T, pool *dockertest.Pool, conf ExecConfig) ([]byte, e
269264
return buf.Bytes(), nil
270265
}
271266

272-
func StopContainer(t *testing.T, pool *dockertest.Pool, id string, to time.Duration) int {
267+
func StopContainer(t *testing.T, pool *dockertest.Pool, id string, to time.Duration) {
273268
t.Helper()
274269

275270
err := pool.Client.KillContainer(docker.KillContainerOptions{
@@ -288,11 +283,10 @@ func StopContainer(t *testing.T, pool *dockertest.Pool, id string, to time.Durat
288283
continue
289284
}
290285

291-
return cnt.State.ExitCode
286+
return
292287
}
293288

294289
t.Fatalf("timed out waiting for container %s to stop", id)
295-
return 1
296290
}
297291

298292
// cmdLineEnvs returns args passed to the /envbox command

0 commit comments

Comments
 (0)