Skip to content

Commit e17a336

Browse files
authored
Merge pull request #2727 from AkihiroSuda/fix-2726
logging: respect ctx
2 parents 758930d + b045866 commit e17a336

File tree

6 files changed

+40
-7
lines changed

6 files changed

+40
-7
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ jobs:
7070

7171
test-integration:
7272
runs-on: "ubuntu-${{ matrix.ubuntu }}"
73-
timeout-minutes: 60
73+
timeout-minutes: 40
7474
strategy:
7575
fail-fast: false
7676
matrix:

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ RUN curl -L -o nydus-static.tgz "https://github.com/dragonflyoss/image-service/r
313313
mv nydus-static/nydus-image nydus-static/nydusd nydus-static/nydusify /usr/bin/ && \
314314
rm nydus-static.tgz
315315
CMD ["gotestsum", "--format=testname", "--rerun-fails=2", "--packages=github.com/containerd/nerdctl/cmd/nerdctl/...", \
316-
"--", "-timeout=60m", "-args", "-test.kill-daemon"]
316+
"--", "-timeout=30m", "-args", "-test.kill-daemon"]
317317

318318
FROM test-integration AS test-integration-rootless
319319
# Install SSH for creating systemd user session.

cmd/nerdctl/container_run_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,17 @@ func TestRunAddHostRemainsWhenAnotherContainerCreated(t *testing.T) {
510510

511511
base.Cmd("exec", containerName, "cat", "/etc/hosts").AssertOutWithFunc(checkEtcHosts)
512512
}
513+
514+
// https://github.com/containerd/nerdctl/issues/2726
515+
func TestRunRmTime(t *testing.T) {
516+
base := testutil.NewBase(t)
517+
base.Cmd("pull", testutil.CommonImage)
518+
t0 := time.Now()
519+
base.Cmd("run", "--rm", testutil.CommonImage, "true").AssertOK()
520+
t1 := time.Now()
521+
took := t1.Sub(t0)
522+
const deadline = 3 * time.Second
523+
if took > deadline {
524+
t.Fatalf("expected to have completed in %v, took %v", deadline, took)
525+
}
526+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ require (
4040
github.com/moby/sys/mount v0.3.3
4141
github.com/moby/sys/signal v0.7.0
4242
github.com/moby/term v0.5.0
43+
github.com/muesli/cancelreader v0.2.2
4344
github.com/opencontainers/go-digest v1.0.0
4445
github.com/opencontainers/image-spec v1.1.0-rc5
4546
github.com/opencontainers/runtime-spec v1.1.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
214214
github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y=
215215
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
216216
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
217+
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
218+
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
217219
github.com/multiformats/go-base32 v0.1.0 h1:pVx9xoSPqEIQG8o+UbAe7DNi51oej1NtK+aGkbLYxPE=
218220
github.com/multiformats/go-base32 v0.1.0/go.mod h1:Kj3tFY6zNr+ABYMqeUNeGvkIC/UYgtWibDcT0rExnbI=
219221
github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ89tUg4F4=

pkg/logging/logging.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/containerd/containerd/errdefs"
3232
"github.com/containerd/containerd/runtime/v2/logging"
3333
"github.com/containerd/log"
34+
"github.com/muesli/cancelreader"
3435
)
3536

3637
const (
@@ -140,10 +141,25 @@ func LoadLogConfig(dataStore, ns, id string) (LogConfig, error) {
140141
return logConfig, nil
141142
}
142143

143-
func loggingProcessAdapter(driver Driver, dataStore string, config *logging.Config) error {
144+
func loggingProcessAdapter(ctx context.Context, driver Driver, dataStore string, config *logging.Config) error {
144145
if err := driver.PreProcess(dataStore, config); err != nil {
145146
return err
146147
}
148+
149+
stdoutR, err := cancelreader.NewReader(config.Stdout)
150+
if err != nil {
151+
return err
152+
}
153+
stderrR, err := cancelreader.NewReader(config.Stderr)
154+
if err != nil {
155+
return err
156+
}
157+
go func() {
158+
<-ctx.Done() // delivered on SIGTERM
159+
stdoutR.Cancel()
160+
stderrR.Cancel()
161+
}()
162+
147163
var wg sync.WaitGroup
148164
wg.Add(3)
149165
stdout := make(chan string, 10000)
@@ -161,8 +177,8 @@ func loggingProcessAdapter(driver Driver, dataStore string, config *logging.Conf
161177
}
162178
}
163179

164-
go processLogFunc(config.Stdout, stdout)
165-
go processLogFunc(config.Stderr, stderr)
180+
go processLogFunc(stdoutR, stdout)
181+
go processLogFunc(stderrR, stderr)
166182
go func() {
167183
defer wg.Done()
168184
driver.Process(stdout, stderr)
@@ -175,7 +191,7 @@ func loggerFunc(dataStore string) (logging.LoggerFunc, error) {
175191
if dataStore == "" {
176192
return nil, errors.New("got empty data store")
177193
}
178-
return func(_ context.Context, config *logging.Config, ready func() error) error {
194+
return func(ctx context.Context, config *logging.Config, ready func() error) error {
179195
if config.Namespace == "" || config.ID == "" {
180196
return errors.New("got invalid config")
181197
}
@@ -193,7 +209,7 @@ func loggerFunc(dataStore string) (logging.LoggerFunc, error) {
193209
return err
194210
}
195211

196-
return loggingProcessAdapter(driver, dataStore, config)
212+
return loggingProcessAdapter(ctx, driver, dataStore, config)
197213
} else if !errors.Is(err, os.ErrNotExist) {
198214
// the file does not exist if the container was created with nerdctl < 0.20
199215
return err

0 commit comments

Comments
 (0)