From 3144e77d013c344471e26cf58d3c7262b951a11a Mon Sep 17 00:00:00 2001 From: Thibault Charbonnier Date: Fri, 9 Sep 2016 17:30:25 -0700 Subject: [PATCH] fix(globalpatches) randomseed throws error when seeded in wrong context --- kong/core/globalpatches.lua | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/kong/core/globalpatches.lua b/kong/core/globalpatches.lua index 433f92905290..c69d3aae3d60 100644 --- a/kong/core/globalpatches.lua +++ b/kong/core/globalpatches.lua @@ -17,18 +17,19 @@ local seed -- unique seed for Nginx workers). -- luacheck: globals math _G.math.randomseed = function() - if ngx.get_phase() ~= "init_worker" then - ngx.log(ngx.ERR, "math.randomseed() must be called in init_worker") - elseif not seed then + if not seed then + if ngx.get_phase() ~= "init_worker" then + error("math.randomseed() must be called in init_worker", 2) + end + seed = ngx.time() + ngx.worker.pid() ngx.log(ngx.DEBUG, "random seed: ", seed, " for worker n", ngx.worker.id(), " (pid: ", ngx.worker.pid(), ")") randomseed(seed) else ngx.log(ngx.DEBUG, "attempt to seed random number generator, but ", - "already seeded") + "already seeded with ", seed) end return seed end -