Closed karlp closed 9 years ago
What does valgind say if you comment out the lua-mosquitto stuff?
#!/usr/bin/lua
--local mosq = require("mosquitto")
--mosq.init()
print("Exiting thanks")
--mosq.cleanup()
I just want make sure that the leak is in in lua-mosquitto.
Please check also that it is not libmosquitto that causes the leak. Try this tiny C app in valgrind: (build with: gcc -lmosquitto test.c
or similar)
#include <mosquitto.h>
int main(void)
{
mosquitto_lib_init();
mosquitto_lib_cleanup();
}
.... did you read my first mail? That's the same code I tested before filing the bug :smile:
It's also not the require line by itself, that's also clean. Note that I'm using the flukso code, not the ncopa code with the tweaked lua init stuff.
Also, clean lua code is valgrind clean, and lua with uloop (libubox) is also valgrind clean.
sorry for not reading the issue clear enough.
So you are saying that valgrind does not report anything with a single mosq = require("mosquitto")
?
the only thing mosq.init() does is calling mosquitto_lib_init() and the only thing mosq.cleanup() does is calling mosquitto_lib_cleanup().
There are no mallocs in the code either.
Yeah, I know, that's why I filed it here, to see if anyone had any ideas. I couldn't work it out. :person_frowning:
I've put a few examples of plain lua, lua-mosquitto, libubox+lua, plain libmosquitto here: https://github.com/karlp/valgrind-testing-lua-mosquitto
It must be something in how the registration is done by the lua runtime, but I can't see anything obvious in the difference. For reference, libubox's lua binding is here: http://git.openwrt.org/?p=project/libubox.git;a=blob;f=lua/uloop.c;h=2a0a516b5e2be149d6713d5a304ae5a14709a73c;hb=HEAD
I think I figured it out. It is a problem in either libdl/dlclose() or libmosquitto or a combination of those two. It can be reproduced without Lua.
libtestmos.c:
/* gcc -fPIC -shared -o libtestmos.so libtestmos.c -lmosquitto */
#include <stdio.h>
int testinit(void)
{
mosquitto_lib_init();
printf("initialized libmosquitto\n");
mosquitto_lib_cleanup();
printf("cleaned up libmosquitto\n");
return 0;
}
testmos.c:
/* gcc -o testmos testmos.c -ldl */
#include <dlfcn.h>
#include <stdio.h>
int dlerr(const char *s)
{
fprintf("%s: %s\n", s, dlerror());
return 1;
}
int main(void)
{
int (*testinit)(void);
void *h = dlopen("./libtestmos.so", RTLD_NOW);
if (!h)
return dlerr("dlopen");
testinit = dlsym(h, "testinit");
if (!testinit)
return dlerr("dlsym");
testinit();
dlclose(h);
return 0;
}
What is interesting is that if you don't do the dlclose(), it will not leak.
It's from a malloc in liblua, I thought it might be from the lua_replace(L, LUA_ENVIRONINDEX) call, not having anything to unwind there later? :shrugs:
I've got lots of libmosquitto code that has no valgrind problems now or earlier, so I'm not convinced it's libmosquitto at least. And still, libubox is also opened as a C library, and isn't leaking.
I've just added a luasocket example, which also has no leaks in valgrind, and also opens a C library behind the scenes. https://github.com/karlp/valgrind-testing-lua-mosquitto/blob/master/open-close-socket.lua
It's from a malloc in liblua,
Not necessarily. it is triggered via liblua but there are many ??? lines in the stacktrace
I thought it might be from the lua_replace(L, LUA_ENVIRONINDEX) call, not having anything to unwind there later? :shrugs:
no, I tested comment out those. Infact, I tested comment out many things. The only thing that made the difference was commenting out mosquitto_lib_init. That is why I bothered write the testmos testcase.
I don't think problem is in mosquitto or lua or lua-mosquitto. I think problem is in openssl and dlclose from libc. I bet you will see this symtom with any lua module that is linked to libssl.
And I suspect that you will never lose more than those 32 bytes, regarless how many times you load and unload lua-mosquitto. You still have reachable blocks so it is not necessarily a leak.
Works for me, it's definitely a one off, so yes, in general the runtime environment will reclaim memory, it's just nice to get clean valgrind runs, so you can only focus on what you are working on :)
I can't work it out, but simply opening and closing the mosquitto libs causes valgrind errors.
C code, no leaks in valgrind.
Lua code, leaks 1 32byte block in valgrind
Valgrind report...
Any ideas? I know it's not going to ever really matter, but it's nice to make them all disappear.