Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callbacks are called from other threads #21

Closed
daurnimator opened this issue Jan 24, 2018 · 4 comments
Closed

Callbacks are called from other threads #21

daurnimator opened this issue Jan 24, 2018 · 4 comments

Comments

@daurnimator
Copy link

When in async mode, callbacks seem to be called when mosquitto feels like it, which is entirely undefined behaviour.

local mqtt = require "mosquitto"
local gettid do
	local ffi = require "ffi" -- use luajit or https://github.com/facebookarchive/luaffifb
	ffi.cdef[[int syscall(int);]]
	gettid = function()
		return ffi.C.syscall(186)
	end
end
local client = mqtt.new()
client:callback_set("ON_CONNECT", function()
	print("connected", gettid())
	client:subscribe("$SYS/#")
end)
client:callback_set("ON_MESSAGE", function(mid, topic, payload)
	print("message", gettid(), mid, topic, payload)
end)
local broker = arg[1] or "localhost"
assert(client:connect_async(broker))
assert(client:loop_start())

client:loop_read()
client:loop_read()
client:loop_write()
client:loop_misc()
client:loop_read()
client:loop_read()
client:loop_write()
client:loop_misc()
client:loop_read()
client:loop_read()
client:loop_write()
@karlp
Copy link
Contributor

karlp commented Jan 24, 2018

your code here is completely wrong. loop_start starts a new thread. you then do not keep trying to call loop_read/write/misc yourself. the callback is called from the loop thread yes, this is well explained in the mosquitto reference

@karlp karlp closed this as completed Jan 24, 2018
@daurnimator
Copy link
Author

your code here is completely wrong. loop_start starts a new thread. you then do not keep trying to call loop_read/write/misc yourself. the callback is called from the loop thread yes, this is well explained in the mosquitto reference

Ah oops. I misread what :loop_start was for. Looks like I just call :loop_read/write/misc as required.

@daurnimator
Copy link
Author

So it appears that :loop_start is always unsafe to use in the style that lua-mosquitto binds it.
Could you remove it from the API?

@karlp
Copy link
Contributor

karlp commented Jan 26, 2018

You're perfectly free to implement your own locking and access controls, and there are totally uses where you don't need anything shared, where having it called from other threads is completely ok. I don't see any reason to just go and flat remove the binding, that seems overreaching.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants