Skip to content

Commit

Permalink
Set up callback only after target_list is loaded (#18)
Browse files Browse the repository at this point in the history
This fixes an issue observed in Kong: when restarting the kong process, the concurrent worker processes load targets, and trigger other workers's event callback. When callbacks hit before the target list is ready, the event_handler method error would happen at line 925: `the targets is nil` when indexing its `ip` field.

See Kong/kong#4453
  • Loading branch information
wuguangkuo authored and hishamhm committed Apr 4, 2019
1 parent 616e8bb commit f091fbf
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/resty/healthcheck.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1267,12 +1267,6 @@ function _M.new(opts)
-- register for events, and directly after load initial target list
-- order is important!
do
self.ev_callback = function(data, event)
-- just a wrapper to be able to access `self` as a closure
return self:event_handler(event, data.ip, data.port, data.hostname)
end
worker_events.register_weak(self.ev_callback, self.EVENT_SOURCE)

-- Lock the list, in case it is being cleared by another worker
local ok, err = locking_target_list(self, function(target_list)

Expand All @@ -1296,6 +1290,12 @@ function _M.new(opts)
self:log(ERR, "Error loading initial target list: ", err)
end

self.ev_callback = function(data, event)
-- just a wrapper to be able to access `self` as a closure
return self:event_handler(event, data.ip, data.port, data.hostname)
end
worker_events.register_weak(self.ev_callback, self.EVENT_SOURCE)

-- handle events to sync up in case there was a change by another worker
worker_events:poll()
end
Expand Down

0 comments on commit f091fbf

Please sign in to comment.