Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/telemetry: do not always fork cmd/go child #67700

Closed
rsc opened this issue May 29, 2024 · 4 comments
Closed

x/telemetry: do not always fork cmd/go child #67700

rsc opened this issue May 29, 2024 · 4 comments
Assignees
Labels
NeedsFix The path to resolution is known, but the work has not been done. release-blocker telemetry x/telemetry issues
Milestone

Comments

@rsc
Copy link
Contributor

rsc commented May 29, 2024

x/telemetry is forking a new go child process every time the go command is run, and then the child looks around and decides not to upload anything. It should look around before forking and avoid the fork 99% of the time.

@rsc rsc added NeedsFix The path to resolution is known, but the work has not been done. release-blocker labels May 29, 2024
@rsc rsc added this to the Go1.23 milestone May 29, 2024
@gopherbot gopherbot added the telemetry x/telemetry issues label May 29, 2024
@findleyr
Copy link
Contributor

Also: per discussion we should rename the X_TELEMETRY_CHILD environment variable
to something clearly go specific, such as GO_TELEMETRY_CHILD.

@sparrowhe
Copy link

sparrowhe commented May 31, 2024

I tried making some change of this issue:

diff --git a/start.go b/start.go
index 426a18b..b7baef8 100644
--- a/start.go
+++ b/start.go
@@ -242,6 +242,31 @@ func child(reportCrashes, upload bool, uploadStartTime time.Time, uploadURL stri
 		})
 	}
 	if upload {
+
+		// golang/go#67700: do not always fork cmd/go child
+		// Pre-check if there are any coditions that would prevent uploading anything.
+
+		if mode, _ := telemetry.Default.Mode(); mode == "off" {
+			// There's no work to be done if telemetry is turned off.
+			return
+		}
+		if telemetry.Default.LocalDir() == "" {
+			// The telemetry dir wasn't initialized properly, probably because
+			// os.UserConfigDir did not complete successfully. In that case
+			// there are no counters to upload, so we should just do nothing.
+			return
+		}
+		tokenfilepath := filepath.Join(telemetry.Default.LocalDir(), "upload.token")
+		ok, err := acquireUploadToken(tokenfilepath)
+		if err != nil {
+			log.Printf("error acquiring upload token: %v", err)
+			return
+		} else if !ok {
+			// It hasn't been a day since the last upload.Run attempt or there's
+			// a concurrently running uploader.
+			return
+		}
+
 		g.Go(func() error {
 			uploaderChild(uploadStartTime, uploadURL)
 			return nil
@@ -251,26 +276,6 @@ func child(reportCrashes, upload bool, uploadStartTime time.Time, uploadURL stri
 }
 
 func uploaderChild(asof time.Time, uploadURL string) {
-	if mode, _ := telemetry.Default.Mode(); mode == "off" {
-		// There's no work to be done if telemetry is turned off.
-		return
-	}
-	if telemetry.Default.LocalDir() == "" {
-		// The telemetry dir wasn't initialized properly, probably because
-		// os.UserConfigDir did not complete successfully. In that case
-		// there are no counters to upload, so we should just do nothing.
-		return
-	}
-	tokenfilepath := filepath.Join(telemetry.Default.LocalDir(), "upload.token")
-	ok, err := acquireUploadToken(tokenfilepath)
-	if err != nil {
-		log.Printf("error acquiring upload token: %v", err)
-		return
-	} else if !ok {
-		// It hasn't been a day since the last upload.Run attempt or there's
-		// a concurrently running uploader.
-		return
-	}
 	if err := upload.Run(upload.RunConfig{
 		UploadURL: uploadURL,
 		LogWriter: os.Stderr,

This is my first time contributing code, please let me know if I did something wrong. :)

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/589375 mentions this issue: telemetry: acquire upload token before starting uploader child

@matloob
Copy link
Contributor

matloob commented May 31, 2024

Hi @sparrowhe I already had a change to fix this (I just incorrectly annotated the issue number on the commit description).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix The path to resolution is known, but the work has not been done. release-blocker telemetry x/telemetry issues
Projects
Status: Done
Development

No branches or pull requests

5 participants