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

Thread-related Error with mosquitto_lib_init in a Multi-Threaded Environment #2929

Closed
KudoWu opened this issue Oct 19, 2023 · 0 comments
Closed

Comments

@KudoWu
Copy link

KudoWu commented Oct 19, 2023

Hello,

First, let me provide you with information about my operating environment:

system: Linux xxx 4.4.60 3, SMP PREEMPT, armv7l GNU/Linux
libmosquitto version: release/1.6(libmosquitto.so.1.6.15)
libssl version: OpenSSL 1.1.1g

I encountered a problem when using mosquitto_lib_init in my code. To give you some context: I created a separate thread to handle MQTT operations, including initialization, connection, and publishing. I initiate the MQTT client using mosquitto_lib_init, and after a successful connection, I call mosquitto_loop_start. Importantly, there are no other threads to operate it.

When running the code, I encountered the following issue, as identified through gdb:

#0  0x76fd7580 in pthread_rwlock_trywrlock () from /lib/ld-musl-arm.so.1
#1  0x76fd7430 in pthread_rwlock_timedwrlock () from /lib/ld-musl-arm.so.1
#2  0x76ebee00 in CRYPTO_STATIC_MUTEX_lock_write () from /usr/lib/libcrypto.so
#3  0x76e66bd0 in CRYPTO_get_ex_new_index () from /usr/lib/libcrypto.so
#4  0x76f438a4 in net__init () at /home/xxxx/Work/workspace/cross_dynamiclink_lib_src/mosquitto/lib/net_mosq.c:149
#5  0x76f417cc in mosquitto_lib_init () at /home/xxxx/Work/workspace/cross_dynamiclink_lib_src/mosquitto/lib/mosquitto.c:77
#6  0x54f78aa8 in xx::module::ModuleMqttTsp::Start() ()
#7  0x54f455c4 in void std::_Mem_fn_base<void (xx::module::ModuleMqttTsp::*)(), true>::operator()<, void>(xx::module::ModuleMqttTsp*) const ()
#8  0x54f45258 in void std::_Bind_simple<std::_Mem_fn<void (xx::module::ModuleMqttTsp::*)()> (xx::module::ModuleMqttTsp*)>::_M_invoke<0u>(std::_Index_tuple<0u>) ()
#9  0x54f44fc0 in std::_Bind_simple<std::_Mem_fn<void (xx::module::ModuleMqttTsp::*)()> (xx::module::ModuleMqttTsp*)>::operator()() ()
#10 0x54f44df0 in std::thread::_Impl<std::_Bind_simple<std::_Mem_fn<void (xx::module::ModuleMqttTsp::*)()> (xx::module::ModuleMqttTsp*)> >::_M_run() ()
#11 0x76c432fc in ?? () from /usr/lib/libstdc++.so.6
Cannot access memory at address 0x7d0f00

Upon inspecting the mosquitto.h documentation, I found the following hint:

 *	If your application uses threads you must use <mosquitto_threaded_set> to
 *	tell the library this is the case, otherwise it makes some optimisations
 *	for the single threaded case that may result in unexpected behaviour for
 *	the multi threaded case.

I should mention that even though my application operates in a multi-threaded environment, I only have one thread interacting with the MQTT client. Therefore, it appears that using mosquitto_lib_init is not incorrect in my scenario.

However, I experimented with an alternative approach in my code:

-    mosquitto_lib_init();
+    // mosquitto_lib_init();
     mosq = mosquitto_new(token.c_str(), true, NULL);
+    mosquitto_threaded_set(mosq, true);

Surprisingly, this modification resulted in successful execution without any errors. Although there is currently a problem that the server cannot receive it in publish, I think this is my own problem.

I could not find any relevant entries in the ChangeLog.txt regarding changes to mosquitto_lib_init in subsequent versions of the library. Therefore, I am seeking your assistance in understanding why this problem occurred and whether my modification is the correct solution.

Thank you very much for your support!

Bye

KudoWu

@KudoWu KudoWu closed this as completed Oct 25, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant