Skip to content

Instantly share code, notes, and snippets.

@wjkoh
Last active March 18, 2025 06:55
Show Gist options
  • Save wjkoh/8a3adea949cd8edddf6975bc8463b420 to your computer and use it in GitHub Desktop.
Save wjkoh/8a3adea949cd8edddf6975bc8463b420 to your computer and use it in GitHub Desktop.
Go: Data Race Quiz! Shadowing or Stomping?
package main
import (
"context"
"io"
"time"
)
type uploader struct{}
// Shadowing or Stomping?
// TODO: In this function, which lines cause a data race, specifically a simultaneous read and write to the same data?
func (u *uploader) BackupAndUpload(ctx context.Context) (string, error) {
pr, pw := io.Pipe()
go func() {
_, err := u.Backup(ctx, pw)
pw.CloseWithError(err)
}()
ctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
defer cancel()
return u.Upload(ctx, pr)
}
@wjkoh
Copy link
Author

wjkoh commented Mar 18, 2025

Hint: Line 15, _, err := u.Backup(ctx, pw), potentially reads from X, while line 18, ctx, cancel := context.WithTimeout(ctx, 10*time.Minute), writes to X at the same time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment