GithubHelp home page GithubHelp logo

valgrind leak about lua-mosquitto HOT 9 CLOSED

flukso avatar flukso commented on May 28, 2024
valgrind leak

from lua-mosquitto.

Comments (9)

ncopa avatar ncopa commented on May 28, 2024

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.

from lua-mosquitto.

ncopa avatar ncopa commented on May 28, 2024

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();
}

from lua-mosquitto.

karlp avatar karlp commented on May 28, 2024

.... did you read my first mail? That's the same code I tested before filing the bug 😄

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.

from lua-mosquitto.

ncopa avatar ncopa commented on May 28, 2024

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.

from lua-mosquitto.

karlp avatar karlp commented on May 28, 2024

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

from lua-mosquitto.

ncopa avatar ncopa commented on May 28, 2024

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.

from lua-mosquitto.

karlp avatar karlp commented on May 28, 2024

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

from lua-mosquitto.

ncopa avatar ncopa commented on May 28, 2024

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.

from lua-mosquitto.

karlp avatar karlp commented on May 28, 2024

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 :)

from lua-mosquitto.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.