playground: use the correct contexts for sandbox requests

The sandbox code was incorrectly using the request context instead of
the build context when trying to determine if there was a
DeadlineExceeded error. This would manifest to the user as a blank
response in the build output, rather than the correct error.

Additionally, the sandbox code was using the incorrect context for
running the binary. This means we were not correctly enforcing
maxRunTime.

Finally, tests do not pass with a maxRunTime of 2 seconds on my machine,
and it's unclear what impact enforcing this would have on production
code. I've increased it to 5 seconds. It would be prudent to add metrics
here to determine how user programs are impacted in a follow-up issue.

Updates golang/go#25224
Updates golang/go#38052

Change-Id: I59aa8caeb63a9eec687bfbe4f69c57f71a13440d
Reviewed-on: https://go-review.googlesource.com/c/playground/+/227350
Reviewed-by: Andrew Bonventre <andybons@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
diff --git a/sandbox.go b/sandbox.go
index 58431d4..5fbc89c 100644
--- a/sandbox.go
+++ b/sandbox.go
@@ -40,7 +40,7 @@
 
 const (
 	maxCompileTime = 5 * time.Second
-	maxRunTime     = 2 * time.Second
+	maxRunTime     = 5 * time.Second
 
 	// progName is the implicit program name written to the temp
 	// dir and used in compiler and vet errors.
@@ -420,7 +420,7 @@
 	cmd.Env = append(cmd.Env, "GOPATH="+goPath)
 	t0 := time.Now()
 	if out, err := cmd.CombinedOutput(); err != nil {
-		if ctx.Err() == context.DeadlineExceeded {
+		if buildCtx.Err() == context.DeadlineExceeded {
 			log.Printf("go build timed out after %v", time.Since(t0))
 			return &response{Errors: goBuildTimeoutError}, nil
 		}
@@ -454,7 +454,7 @@
 		if err != nil {
 			return nil, err
 		}
-		req, err := http.NewRequestWithContext(ctx, "POST", sandboxBackendURL(), bytes.NewReader(exeBytes))
+		req, err := http.NewRequestWithContext(runCtx, "POST", sandboxBackendURL(), bytes.NewReader(exeBytes))
 		if err != nil {
 			return nil, err
 		}
@@ -487,7 +487,7 @@
 		cmd.Stdout = rec.Stdout()
 		cmd.Stderr = rec.Stderr()
 		if err := cmd.Run(); err != nil {
-			if ctx.Err() == context.DeadlineExceeded {
+			if runCtx.Err() == context.DeadlineExceeded {
 				// Send what was captured before the timeout.
 				events, err := rec.Events()
 				if err != nil {