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

Pending messages not published when using threading #722

Open
Viatorus opened this issue Mar 13, 2018 · 2 comments
Open

Pending messages not published when using threading #722

Viatorus opened this issue Mar 13, 2018 · 2 comments

Comments

@Viatorus
Copy link

Viatorus commented Mar 13, 2018

We have got a reproducible issue when publishing messages right before the end of a mosquitto connection.

Pending messages will not published after calling disconnect or loop_stop().

We are looking for a functionality to flush all messages before a connection is closed.

Check out the following example.
Only if you uncomment the thread sleep, all messages will be published.

Mosquitto v. 1.4.14

#include <mosquittopp.h>

#include <iostream>
#include <thread>

using namespace std::chrono_literals;

int main() {
	mosqpp::mosquittopp mqo("foobar");
	try {
		if (mosqpp::lib_init() != MOSQ_ERR_SUCCESS) {
			throw std::runtime_error("could not init");
		}
		if (mqo.connect("0.0.0.0") != MOSQ_ERR_SUCCESS) {
			throw std::runtime_error("could not connect to server");
		}
		if (mqo.loop_start() != MOSQ_ERR_SUCCESS) {
			throw std::runtime_error("could not start loop");
		}

		mqo.publish(nullptr, "hello");
		mqo.publish(nullptr, "world");
		mqo.publish(nullptr, "how");
		mqo.publish(nullptr, "are");
		mqo.publish(nullptr, "you");

		// Uncomment this for a "workaround"
		// std::this_thread::sleep_for(1s);

		if (mqo.disconnect() != MOSQ_ERR_SUCCESS) {
			throw std::runtime_error("could not disconnect from server");
		}
		if (mqo.loop_stop(false) != MOSQ_ERR_SUCCESS) {
			throw std::runtime_error("could not stop loop");
		}
		if (mosqpp::lib_cleanup() != MOSQ_ERR_SUCCESS) {
			throw std::runtime_error("could not cleanup");
		}

	} catch (std::exception &e) {
		std::cerr << e.what() << "\n";
	}
}
@karlp
Copy link
Contributor

karlp commented Mar 13, 2018

I was under the impression you had to call stop before disconnect?

@Viatorus
Copy link
Author

Viatorus commented Mar 16, 2018

@karlp Not necessary but, lets try it out...

I tried many ways of "closing the connection" without success. Overall this is an annoying issue. :(

Not publishing all messages

if (mqo.disconnect() != MOSQ_ERR_SUCCESS) {
	throw std::runtime_error("could not disconnect from server");
}
if (mqo.loop_stop(true) != MOSQ_ERR_SUCCESS) {
	throw std::runtime_error("could not stop loop");
}
if (mqo.disconnect() != MOSQ_ERR_SUCCESS) {
	throw std::runtime_error("could not disconnect from server");
}
if (mqo.loop_stop(false) != MOSQ_ERR_SUCCESS) {
	throw std::runtime_error("could not stop loop");
}

Endless hangs but publish all messages

if (mqo.loop_stop(false) != MOSQ_ERR_SUCCESS) { // Hangs here forever
	throw std::runtime_error("could not stop loop");
}
if (mqo.disconnect() != MOSQ_ERR_SUCCESS) {
	throw std::runtime_error("could not disconnect from server");
}

Endless hangs and not publishing all messages

if (mqo.loop_stop(true) != MOSQ_ERR_SUCCESS) { // Hangs here forever
	throw std::runtime_error("could not stop loop");
}
if (mqo.disconnect() != MOSQ_ERR_SUCCESS) {
	throw std::runtime_error("could not disconnect from server");
}

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