MCPcopy
hub / github.com/slimtoolkit/slim / dupAppStdStream

Function dupAppStdStream

pkg/app/sensor/monitor/composite.go:351–371  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

349// than the process termination (see pkg/monitors/ptrace logic), and multi-writer
350// cannot be closed at all. Hence, the pipe trick.
351func 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
373func closeAll(cs []io.Closer) {
374 for _, c := range cs {

Callers 1

NewCompositeMonitorFunction · 0.85

Calls 1

CloseMethod · 0.65

Tested by

no test coverage detected