From 6086124485612fd776278a5c45e7386089a81e36 Mon Sep 17 00:00:00 2001
From: Darren Shepherd <darren@rancher.com>
Date: Thu, 30 Jul 2015 09:47:12 -0700
Subject: [PATCH] Lazy initialize UUID for Background context

Fixes #782

Signed-off-by: Darren Shepherd <darren@rancher.com>
---
 context/context.go | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/context/context.go b/context/context.go
index 7a3a70e00..07b844ef0 100644
--- a/context/context.go
+++ b/context/context.go
@@ -1,10 +1,16 @@
 package context
 
 import (
+	"sync"
+
 	"github.com/docker/distribution/uuid"
 	"golang.org/x/net/context"
 )
 
+var (
+	once sync.Once
+)
+
 // Context is a copy of Context from the golang.org/x/net/context package.
 type Context interface {
 	context.Context
@@ -19,6 +25,13 @@ type instanceContext struct {
 
 func (ic *instanceContext) Value(key interface{}) interface{} {
 	if key == "instance.id" {
+		once.Do(func() {
+			// We want to lazy initialize the UUID such that we don't
+			// call a random generator from the package initialization
+			// code. For various reasons random could not be available
+			// https://github.com/docker/distribution/issues/782
+			ic.id = uuid.Generate().String()
+		})
 		return ic.id
 	}
 
@@ -27,7 +40,6 @@ func (ic *instanceContext) Value(key interface{}) interface{} {
 
 var background = &instanceContext{
 	Context: context.Background(),
-	id:      uuid.Generate().String(),
 }
 
 // Background returns a non-nil, empty Context. The background context