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

Async client: connect()->wait vs subscribe()->wait #256

Open
InventoryTech opened this issue Mar 31, 2020 · 3 comments
Open

Async client: connect()->wait vs subscribe()->wait #256

InventoryTech opened this issue Mar 31, 2020 · 3 comments

Comments

@InventoryTech
Copy link

When I perform subscribe(...)->wait(), my understanding is that it's guaranteed that the callbacks for the iaction_listener will have completed before the wait() completes, is that correct?

When I perform connect(...)->wait(), it seems that neither the iaction_listener::on_success nor the callback::connected will necessarily have been called before the wait has completed, is that correct?

Can someone explain why? I can't seem to find any docs on the expected behaviour here.

@InventoryTech
Copy link
Author

For example (pseudo code):

async_client client;
token_ptr subToken;
Callback callback;

void Callback::connected(const std::string& cause) {
    subToken = client->subscribe('a_topic');
}

initialise() {
    client->set_callback(callback);    
    token_ptr connTok = client->connect(callback);
    connTok->wait();
    
    // callback::connected may not have been called yet, and therefore subToken is still null
    subToken->wait();
}

@InventoryTech
Copy link
Author

Reproduced the issue based on fairly minimal changes to the async_subscribe.cpp sample.

repro.txt

@InventoryTech
Copy link
Author

Ok, digging in to the C library I think I understand what's going on now. on_success for the connToken is called before the callback::connected, see MqttAsync.c:

line 2236 (*(m->connect.onSuccess))(m->connect.context, &data);
...
line 2258 (*(m->connected))(m->connected_context, reason);

Token::on_success calls cond_.notify_all(); which unblocks/completes the token->wait. This is why my connected callback has not yet been called and subToken remains null.

I wonder whether the async_subscribe.cpp sample should make this clearer? Also the documentation for the Callback::connected should make clear that it will occur after Token::on_success. Documentation in this area is quite sparse in general.

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

1 participant