net.cqueues: Add workaround for luaevent callback getting collected
authordaurnimator <quae@daurnimator.com>
Tue, 06 Jan 2015 20:01:59 -0500
changeset 6539 605a87e90e27
parent 6538 0f940a7ba489
child 6540 e4d443d05626
net.cqueues: Add workaround for luaevent callback getting collected
net/cqueues.lua
--- a/net/cqueues.lua	Tue Jan 06 19:33:25 2015 +0000
+++ b/net/cqueues.lua	Tue Jan 06 20:01:59 2015 -0500
@@ -43,7 +43,11 @@
 	cq = cqueues.new();
 	-- Only need to listen for readable; cqueues handles everything under the hood
 	local EV_READ = server.event.EV_READ;
-	server.base:addevent(cq:pollfd(), EV_READ, function(e)
+	local event_handle;
+	event_handle = server.base:addevent(cq:pollfd(), EV_READ, function(e)
+			-- Need to reference event_handle or this callback will get collected
+			-- This creates a circular reference that can only be broken if event_handle is manually :close()'d
+			local _ = event_handle;
 			assert(cq:loop(0));
 			-- Convert a cq timeout to an acceptable timeout for luaevent
 			local t = cq:timeout();