Skip to content

Commit 42f4bb9

Browse files
Merge pull request #3311 from jing-rui/shimlog
fix shim std logs not close after shim exit
2 parents 94a21fc + 9e0cd52 commit 42f4bb9

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

runtime/v1/linux/runtime.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,9 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
336336
)
337337
ctx = namespaces.WithNamespace(ctx, ns)
338338
pid, _ := runc.ReadPidFile(filepath.Join(bundle.path, proc.InitPidFile))
339+
shimExit := make(chan struct{})
339340
s, err := bundle.NewShimClient(ctx, ns, ShimConnect(r.config, func() {
341+
defer close(shimExit)
340342
if _, err := r.tasks.Get(ctx, id); err != nil {
341343
// Task was never started or was already successfully deleted
342344
return
@@ -362,6 +364,18 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
362364

363365
logDirPath := filepath.Join(r.root, ns, id)
364366

367+
copyAndClose := func(dst io.Writer, src io.ReadWriteCloser) {
368+
copyDone := make(chan struct{})
369+
go func() {
370+
io.Copy(dst, src)
371+
close(copyDone)
372+
}()
373+
select {
374+
case <-shimExit:
375+
case <-copyDone:
376+
}
377+
src.Close()
378+
}
365379
shimStdoutLog, err := v1.OpenShimStdoutLog(ctx, logDirPath)
366380
if err != nil {
367381
log.G(ctx).WithError(err).WithFields(logrus.Fields{
@@ -372,9 +386,9 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
372386
continue
373387
}
374388
if r.config.ShimDebug {
375-
go io.Copy(os.Stdout, shimStdoutLog)
389+
go copyAndClose(os.Stdout, shimStdoutLog)
376390
} else {
377-
go io.Copy(ioutil.Discard, shimStdoutLog)
391+
go copyAndClose(ioutil.Discard, shimStdoutLog)
378392
}
379393

380394
shimStderrLog, err := v1.OpenShimStderrLog(ctx, logDirPath)
@@ -387,9 +401,9 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
387401
continue
388402
}
389403
if r.config.ShimDebug {
390-
go io.Copy(os.Stderr, shimStderrLog)
404+
go copyAndClose(os.Stderr, shimStderrLog)
391405
} else {
392-
go io.Copy(ioutil.Discard, shimStderrLog)
406+
go copyAndClose(ioutil.Discard, shimStderrLog)
393407
}
394408

395409
t, err := newTask(id, ns, pid, s, r.events, r.tasks, bundle)

0 commit comments

Comments
 (0)