From 9b9c1afcb078c2080cd1fac84be67797a33a32e9 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 12 Apr 2017 00:58:50 +0000 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Alex Vaghin --- acme/autocert/cache.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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 @@ func (d DirCache) Put(ctx context.Context, name string, data []byte) error { 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():