MCPcopy Create free account
hub / github.com/cli/cli / SpawnSendTelemetry

Function SpawnSendTelemetry

internal/telemetry/telemetry.go:435–488  ·  view source on GitHub ↗

SpawnSendTelemetry spawns a detached subprocess to send telemetry. The payload is written to the child's stdin via a pipe so that it is not visible to other users through process argument inspection (e.g. ps aux). The parent writes the full payload and closes the pipe before returning, so no long-li

(executable string, payload SendTelemetryPayload)

Source from the content-addressed store, hash-verified

433//
434// All errors are silently ignored since telemetry is best-effort.
435func SpawnSendTelemetry(executable string, payload SendTelemetryPayload) {
436 payloadBytes, err := json.Marshal(payload)
437 if err != nil {
438 return
439 }
440
441 if len(payloadBytes) > maxPayloadSize {
442 return
443 }
444
445 // Resolve the executable to an absolute path before changing the child's
446 // working directory. Without this, a relative path (e.g. from GH_PATH) would
447 // be resolved against cmd.Dir at Start time and fail to spawn.
448 if abs, err := filepath.Abs(executable); err == nil {
449 executable = abs
450 }
451
452 cmd := exec.Command(executable, "send-telemetry")
453
454 cmd.Stdout = io.Discard
455 cmd.Stderr = io.Discard
456
457 // Set the working directory to a stable directory elsewhere so that the subprocess doesn't
458 // hold a reference to the parent's current working directory, avoiding any weirdness around
459 // deleting the parent process's current working directory while the child is still running.
460 // Only do this when we have an absolute executable path so that the child can still be found.
461 if filepath.IsAbs(executable) {
462 cmd.Dir = os.TempDir()
463 }
464
465 // Configure the child process to be detached from the parent so that it can continue running
466 // after the parent exits, and so that it doesn't receive any signals sent to the parent.
467 cmd.SysProcAttr = detachAttrs()
468
469 // Get the write end of the stdin pipe before starting.
470 stdin, err := cmd.StdinPipe()
471 if err != nil {
472 return
473 }
474
475 if err := cmd.Start(); err != nil {
476 _ = stdin.Close()
477 return
478 }
479
480 // Write the payload synchronously into the kernel pipe buffer, then close
481 // the pipe to signal EOF. The child reads the complete payload from stdin.
482 // io.Copy loops until all bytes are written, avoiding any risk of a short write.
483 _, _ = io.Copy(stdin, bytes.NewReader(payloadBytes))
484 _ = stdin.Close()
485
486 // Release resources associated with the child process since we will never Wait for it.
487 _ = cmd.Process.Release()
488}
489
490type noOpPendingEvent struct{}
491

Calls 4

CommandMethod · 0.80
detachAttrsFunction · 0.70
CloseMethod · 0.65
CopyMethod · 0.45