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

add a refcount to library init/cleanup #1691

Merged
merged 1 commit into from May 20, 2020
Merged

Conversation

martingkelly
Copy link
Contributor

Add a refcount around mosquitto_lib_init and mosquitto_lib_cleanup so that multiple calls to init/cleanup don't trigger memory leaks or double-frees.

Another for this change is the following scenario that I have:

  • I have a library that internally uses mosquitto to subscribe to notifications.
  • Applications want to use mosquitto to publish notifications.
  • If both my library and applications call mosquitto init/cleanup, valgrind reports a memory leak, as the memory is clobbered in the second init call.

The only current solution is that the library requires the application to call mosquitto init/cleanup prior to initializing the library. However, it would be nice if instead both application and library could call init/cleanup and things would "just work" no matter what, especially as many applications won't use mosquitto and shouldn't have to bother about this implementation detail. Further, this breaks my library's abstraction in that if my library ever implements MQTT not using mosquitto (e.g. paho), the applications will be initializing mosquitto for no reason. In other words, this implementation detail (my library using mosquitto internally) should not be leaked to the user.

  • Have you signed the Eclipse Contributor Agreement, using the same email address as you used in your commits?
  • Do each of your commits have a "Signed-off-by" line, with the correct email address? Use "git commit -s" to generate this line for you.
  • If you are contributing a bugfix, is your work based off the fixes branch?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you successfully run make test with your changes locally?
    "make test" is failing on my machine, but it fails identically on the fixes branch (on ./02-subscribe-qos1.py).
./02-subscribe-qos1.py c/02-subscribe-qos1-async2.test
connect_async failed: Connection refused
Traceback (most recent call last):
  File "./02-subscribe-qos1.py", line 47, in <module>
    (conn, address) = sock.accept()
  File "/usr/lib/python3.8/socket.py", line 292, in accept
    fd, addr = self._accept()
socket.timeout: timed out
make[2]: *** [Makefile:36: c] Error 1
make[2]: Leaving directory '/home/martin/ws/mosquitto/test/lib'
make[1]: *** [Makefile:11: test] Error 2
make[1]: Leaving directory '/home/martin/ws/mosquitto/test'
make: *** [Makefile:75: test] Error 2

Add a refcount around mosquitto_lib_init and mosquitto_lib_cleanup so
that multiple calls to init/cleanup don't trigger memory leaks or
double-frees.

Signed-off-by: Martin Kelly <[email protected]>
@karlp
Copy link
Contributor

karlp commented May 19, 2020

Are you sure you want to ignore the cleanup until the last refcount? (also, in general, you don't want to make unrelated whitespace changes)

@martingkelly
Copy link
Contributor Author

Are you sure you want to ignore the cleanup until the last refcount?

How else would you suggest to do it? If the refcount is greater than 0, someone is still using the library, so it's not safe to cleanup the library.

(also, in general, you don't want to make unrelated whitespace changes)

I agree, and tried not to make any. Where did I make these?

@karlp
Copy link
Contributor

karlp commented May 19, 2020

Hrm, I guess, I was thinking if someone wanted to close it, they wanted it closed, but I guess that's not really useful. Nevermind the whitespace comment, I was lost in the #ifdefs.

@ralight
Copy link
Contributor

ralight commented May 20, 2020

Thanks for this, it's a good improvement.

Thinking about it now, I may go a bit further in the develop branch - changing mosquitto_new() to call lib init, and register lib cleanup to get called with atexit. With your changes, that would remove any requirement to call lib init/cleanup manually, but still allow a user to completely clean up if required.

@ralight ralight merged commit 61a50c6 into eclipse:fixes May 20, 2020
@martingkelly martingkelly deleted the init-cleanup branch May 20, 2020 16:51
@martingkelly
Copy link
Contributor Author

Thinking about it now, I may go a bit further in the develop branch - changing mosquitto_new() to call lib init, and register lib cleanup to get called with atexit. With your changes, that would remove any requirement to call lib init/cleanup manually, but still allow a user to completely clean up if required.

That sounds good to me!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants