acme/autocert: fix context usage

Context.Err() is not valid before Context.Done().

Updates golang/go#19856

Change-Id: I7605bb227bfc4cb542ef3db49870d4928ce704d1
Reviewed-on: https://go-review.googlesource.com/40396
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Vaghin <ddos@google.com>
diff --git a/acme/autocert/cache.go b/acme/autocert/cache.go
index 9f3e9d1..61a5fd2 100644
--- a/acme/autocert/cache.go
+++ b/acme/autocert/cache.go
@@ -77,12 +77,13 @@
 		if tmp, err = d.writeTempFile(name, data); err != nil {
 			return
 		}
-		// prevent overwriting the file if the context was cancelled
-		if ctx.Err() != nil {
-			return // no need to set err
+		select {
+		case <-ctx.Done():
+			// Don't overwrite the file if the context was canceled.
+		default:
+			newName := filepath.Join(string(d), name)
+			err = os.Rename(tmp, newName)
 		}
-		name = filepath.Join(string(d), name)
-		err = os.Rename(tmp, name)
 	}()
 	select {
 	case <-ctx.Done():