Skip to content

Commit cb3741f

Browse files
committed
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 <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 15e9685 commit cb3741f

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

sandbox.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040

4141
const (
4242
maxCompileTime = 5 * time.Second
43-
maxRunTime = 2 * time.Second
43+
maxRunTime = 5 * time.Second
4444

4545
// progName is the implicit program name written to the temp
4646
// dir and used in compiler and vet errors.
@@ -420,7 +420,7 @@ func compileAndRun(ctx context.Context, req *request) (*response, error) {
420420
cmd.Env = append(cmd.Env, "GOPATH="+goPath)
421421
t0 := time.Now()
422422
if out, err := cmd.CombinedOutput(); err != nil {
423-
if ctx.Err() == context.DeadlineExceeded {
423+
if buildCtx.Err() == context.DeadlineExceeded {
424424
log.Printf("go build timed out after %v", time.Since(t0))
425425
return &response{Errors: goBuildTimeoutError}, nil
426426
}
@@ -454,7 +454,7 @@ func compileAndRun(ctx context.Context, req *request) (*response, error) {
454454
if err != nil {
455455
return nil, err
456456
}
457-
req, err := http.NewRequestWithContext(ctx, "POST", sandboxBackendURL(), bytes.NewReader(exeBytes))
457+
req, err := http.NewRequestWithContext(runCtx, "POST", sandboxBackendURL(), bytes.NewReader(exeBytes))
458458
if err != nil {
459459
return nil, err
460460
}
@@ -487,7 +487,7 @@ func compileAndRun(ctx context.Context, req *request) (*response, error) {
487487
cmd.Stdout = rec.Stdout()
488488
cmd.Stderr = rec.Stderr()
489489
if err := cmd.Run(); err != nil {
490-
if ctx.Err() == context.DeadlineExceeded {
490+
if runCtx.Err() == context.DeadlineExceeded {
491491
// Send what was captured before the timeout.
492492
events, err := rec.Events()
493493
if err != nil {

0 commit comments

Comments
 (0)