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

Fail operations when adapter is in destroy process #331

Closed
wants to merge 7 commits into from

Conversation

xiazhvera
Copy link
Contributor

Issue #, if available:

Description of changes:

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@xiazhvera xiazhvera changed the title Prevent operations when adapter is in destroy process Fail operations when adapter is in destroy process Oct 20, 2023
@codecov-commenter
Copy link

codecov-commenter commented Oct 20, 2023

Codecov Report

Attention: 6 lines in your changes are missing coverage. Please review.

Comparison is base (c475ef1) 82.41% compared to head (ed5a700) 82.34%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #331      +/-   ##
==========================================
- Coverage   82.41%   82.34%   -0.07%     
==========================================
  Files          20       20              
  Lines        8722     8759      +37     
==========================================
+ Hits         7188     7213      +25     
- Misses       1534     1546      +12     
Files Coverage Δ
source/v5/mqtt5_to_mqtt3_adapter.c 76.79% <83.78%> (+0.21%) ⬆️

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

* Set to true if the adapter is in destroying process (internal_ref is 0). The adapter would prevent any
* operations if `is_destroying` set to true
*/
bool is_destroying;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Judgement call: Instead of creating a new member here, we can use adapter_state to track the destroy processing. When we start the destroy process (internal ref -> 0), the adapter would not receive any lifecycle events anymore.

@@ -327,6 +344,10 @@ static int s_aws_mqtt_client_connection_5_connect(
const struct aws_mqtt_connection_options *connection_options) {

struct aws_mqtt_client_connection_5_impl *adapter = impl;
int result = s_check_adapter_destroy_status(adapter);
Copy link
Contributor

@sfod sfod Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is missing for s_aws_mqtt_5_resubscribe_existing_topics, s_aws_mqtt_client_connection_5_acquire and s_aws_mqtt_client_connection_5_release. I think that's all, but I could miss something

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated s_aws_mqtt_5_resubscribe_existing_topics.

Debatable: I feel we should directly fail the acquire/release if the adapter is in destroying process... the ASSERT instead of a validation check might be a better option here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this situation (trying to do something meaningful with connection in callback while it's being destroyed in another thread) should be pretty rare and most probably will indicate that something's wrong with user code. So, ASSERT could actually help to find a possible issue in the code.

@sfod
Copy link
Contributor

sfod commented Oct 21, 2023

Debatable: I feel slightly uneasy because of passing a "half-dead" connection object into callbacks. But this approach seems safe with the current implementation, so I don't have strong opinion about it.

Copy link
Contributor

@bretambrose bretambrose left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usage after ref-count goes to zero is not something we need to worry about.

@xiazhvera
Copy link
Contributor Author

xiazhvera commented Oct 23, 2023

Talked to Bret about the issue. The caller should be responsible to track the connection reference. If the caller have released the connection reference, the caller should never use connection even if the connection is from the callback.

As Igor mentioned, passing a "half-dead" connection object into callbacks is hard to handle. However, if we pass a null here, it has a risk to crash the user. (If the caller forget to handle NULL here).
Besides, it also breaks the contract that the user “should never use the connection if they have release it.” if they have to do a NULL check here.

We should make a rule to avoid expose the object in callbacks in the future implementation.

@xiazhvera xiazhvera closed this Oct 23, 2023
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

Successfully merging this pull request may close these issues.

4 participants