lua-mosquitto icon indicating copy to clipboard operation
lua-mosquitto copied to clipboard

Callbacks occur in same coroutine that mqtt.new is called from

Open daurnimator opened this issue 7 years ago • 6 comments

This causes issues for a few reasons. e.g. Coroutines can die:

local mqtt = require("mosquitto")
print("Main thread:", coroutine.running())
local client
do
	local co = coroutine.create(function()
		print("Calling mqtt.new from:", coroutine.running())
		client = mqtt.new()
		error() -- coroutine will now be dead.
	end)
	coroutine.resume(co)
	print("THE STATUS OF ", co, "IS NOW: ", coroutine.status(co))
end
client:callback_set("ON_CONNECT", function()
	print("On Connect called from:", coroutine.running())
	client:subscribe("$SYS/#")
end)
client:callback_set("ON_MESSAGE", function(mid, topic, payload)
	print("On Message called from:", coroutine.running())
	print(topic, payload)
end)

client:connect()
client:loop_forever()

daurnimator avatar Jan 24 '18 04:01 daurnimator

I'm not sure what you want to happen here?

karlp avatar Jan 24 '18 10:01 karlp

I'm not sure what you want to happen here?

e.g. callbacks could always happen in the thread that called loop_read/loop_write.

daurnimator avatar Jan 24 '18 12:01 daurnimator

The idea that the loop_read/loop write is a different thread than the where mosquitto_new was called is somethign you're going to have to take up with mosquitto itself. I don't really see what we can possibly do with it here, this very much sounds like you're just violating mosquitto's idea of how to use a context object.

karlp avatar Jan 24 '18 13:01 karlp

I'm still not sure what the problem is here?

karlp avatar Jan 24 '18 13:01 karlp

The idea that the loop_read/loop write is a different thread than the where mosquitto_new was called is somethign you're going to have to take up with mosquitto itself.

This issue is related to lua coroutines; not os threads; which is purely in the domain of lua bindings. I'd suggest that your bindings to loop_read, loop_write and loop_misc save the L lua_State parameter somewhere for the C callbacks to use to call back into Lua.

daurnimator avatar Jan 26 '18 02:01 daurnimator

You're going to have make a PR to show what you want here here. Having some of the routines try and use a "different" lua_State object depðending on whether it was from loop() or loop_read/loop_write sounds.... confusing to implement at least, let alone how the user will use it.

karlp avatar Jan 26 '18 17:01 karlp