Using simple io.MultiWriter(os.Stdout, os.File) would make cmd.Wait() block until either the cmd's stdout is closed or the multi-writer is closed. However, both are impossible. We need the Wait() to return much earlier than the process termination (see pkg/monitors/ptrace logic), and multi-writer ca
(artifactsDir string, w io.Writer, kind string)
| 349 | // than the process termination (see pkg/monitors/ptrace logic), and multi-writer |
| 350 | // cannot be closed at all. Hence, the pipe trick. |
| 351 | func dupAppStdStream(artifactsDir string, w io.Writer, kind string) (*os.File, *os.File, error) { |
| 352 | filename := filepath.Join(artifactsDir, "app_"+kind+".log") |
| 353 | |
| 354 | f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0o644) |
| 355 | if err != nil { |
| 356 | return nil, nil, fmt.Errorf("cannot open file %q to duplicate app's %s stream: %w", filename, kind, err) |
| 357 | } |
| 358 | |
| 359 | pr, pw, err := os.Pipe() |
| 360 | if err != nil { |
| 361 | f.Close() |
| 362 | return nil, nil, fmt.Errorf("cannot create pipe for app %s stream: %w", kind, err) |
| 363 | } |
| 364 | |
| 365 | go func() { |
| 366 | n, err := io.Copy(io.MultiWriter(w, f), pr) |
| 367 | log.Debugf("dupAppStdStream: io.Copy() finished; written=%d error=%v", n, err) |
| 368 | }() |
| 369 | |
| 370 | return pw, f, nil |
| 371 | } |
| 372 | |
| 373 | func closeAll(cs []io.Closer) { |
| 374 | for _, c := range cs { |
no test coverage detected