@@ -505,7 +505,7 @@ static void s_mqtt_client_init(
505505
506506 struct aws_mqtt_client_connection_311_impl * connection = user_data ;
507507 struct aws_byte_buf username_with_metrics_buf ;
508- aws_byte_buf_init ( & username_with_metrics_buf , connection -> allocator , 0 );
508+ AWS_ZERO_STRUCT ( username_with_metrics_buf );
509509
510510 if (error_code != AWS_OP_SUCCESS ) {
511511 /* client shutdown already handles this case, so just call that. */
@@ -634,16 +634,20 @@ static void s_mqtt_client_init(
634634 }
635635
636636 /* Apply metrics to username if configured */
637- if (aws_mqtt_append_sdk_metrics_to_username (
638- connection -> allocator ,
639- & username_cur ,
640- connection -> metrics_storage ? & connection -> metrics_storage -> storage_view : NULL ,
641- & username_with_metrics_buf ,
642- NULL ) == AWS_OP_SUCCESS ) {
643- username_cur = aws_byte_cursor_from_buf (& username_with_metrics_buf );
644- } else {
645- AWS_LOGF_WARN (
646- AWS_LS_MQTT_CLIENT , "id=%p: Failed to apply metrics to username, using original" , (void * )connection );
637+ if (connection -> metrics_storage ) {
638+ if (aws_mqtt_append_sdk_metrics_to_username (
639+ connection -> allocator ,
640+ & username_cur ,
641+ & connection -> metrics_storage -> storage_view ,
642+ & username_with_metrics_buf ,
643+ NULL ) == AWS_OP_SUCCESS ) {
644+ username_cur = aws_byte_cursor_from_buf (& username_with_metrics_buf );
645+ } else {
646+ AWS_LOGF_WARN (
647+ AWS_LS_MQTT_CLIENT ,
648+ "id=%p: Failed to apply metrics to username, using original" ,
649+ (void * )connection );
650+ }
647651 }
648652
649653 if (aws_byte_cursor_is_valid (& username_cur )) {
@@ -653,16 +657,20 @@ static void s_mqtt_client_init(
653657 (void * )connection ,
654658 AWS_BYTE_CURSOR_PRI (username_cur ));
655659
656- struct aws_byte_cursor password_cur = {
657- .ptr = NULL ,
658- .len = 0 ,
659- };
660+ struct aws_byte_cursor password_cur ;
661+ AWS_ZERO_STRUCT (password_cur );
660662
661663 if (connection -> password ) {
662664 password_cur = aws_byte_cursor_from_string (connection -> password );
663665 }
664666
665667 aws_mqtt_packet_connect_add_credentials (& connect , username_cur , password_cur );
668+ } else {
669+ AWS_LOGF_WARN (
670+ AWS_LS_MQTT_CLIENT ,
671+ "id=%p: Failed to set username and password. Most likely there is an issue in metrics. (e.x.: username "
672+ "is empty and metrics string exceed the size limit). " ,
673+ (void * )connection );
666674 }
667675 }
668676
@@ -685,20 +693,15 @@ static void s_mqtt_client_init(
685693 goto handle_error ;
686694 }
687695
688- if (aws_byte_buf_is_valid (& username_with_metrics_buf )) {
689- aws_byte_buf_clean_up (& username_with_metrics_buf );
690- }
691-
696+ aws_byte_buf_clean_up (& username_with_metrics_buf );
692697 return ;
693698
694699handle_error :
695700 MQTT_CLIENT_CALL_CALLBACK_ARGS (connection , on_connection_complete , aws_last_error (), 0 , false);
696701 MQTT_CLIENT_CALL_CALLBACK_ARGS (connection , on_connection_failure , aws_last_error ());
697702 aws_channel_shutdown (channel , aws_last_error ());
698703
699- if (aws_byte_buf_is_valid (& username_with_metrics_buf )) {
700- aws_byte_buf_clean_up (& username_with_metrics_buf );
701- }
704+ aws_byte_buf_clean_up (& username_with_metrics_buf );
702705
703706 if (message ) {
704707 aws_mem_release (message -> allocator , message );
@@ -1881,7 +1884,7 @@ static enum aws_mqtt_client_request_state s_subscribe_send(uint16_t packet_id, b
18811884 return AWS_MQTT_CLIENT_REQUEST_ERROR ;
18821885 }
18831886
1884- AWS_VARIABLE_LENGTH_ARRAY (uint8_t , transaction_buf , num_topics * aws_mqtt_topic_tree_action_size );
1887+ AWS_VARIABLE_LENGTH_ARRAY (uint8_t , transaction_buf , num_topics * aws_mqtt_topic_tree_action_size );
18851888 struct aws_array_list transaction ;
18861889 aws_array_list_init_static (& transaction , transaction_buf , num_topics , aws_mqtt_topic_tree_action_size );
18871890
@@ -2674,7 +2677,7 @@ static enum aws_mqtt_client_request_state s_unsubscribe_send(
26742677
26752678 static const size_t num_topics = 1 ;
26762679
2677- AWS_VARIABLE_LENGTH_ARRAY (uint8_t , transaction_buf , num_topics * aws_mqtt_topic_tree_action_size );
2680+ AWS_VARIABLE_LENGTH_ARRAY (uint8_t , transaction_buf , num_topics * aws_mqtt_topic_tree_action_size );
26782681 struct aws_array_list transaction ;
26792682 aws_array_list_init_static (& transaction , transaction_buf , num_topics , aws_mqtt_topic_tree_action_size );
26802683
@@ -3380,33 +3383,17 @@ int aws_mqtt_client_connection_set_on_operation_statistics_handler(
33803383static int s_aws_mqtt_client_connection_311_set_metrics (void * impl , const struct aws_mqtt_iot_sdk_metrics * metrics ) {
33813384
33823385 struct aws_mqtt_client_connection_311_impl * connection = impl ;
3383-
33843386 AWS_PRECONDITION (connection );
3385- if (s_check_connection_state_for_configuration (connection )) {
3386- return aws_raise_error (AWS_ERROR_INVALID_STATE );
3387- }
3388-
3389- if (metrics != NULL && aws_mqtt_validate_iot_sdk_metrics (metrics ) == AWS_OP_ERR ) {
3390- AWS_LOGF_ERROR (
3391- AWS_LS_MQTT_CLIENT , "id=%p: Invalid utf8 or forbidden codepoints in metrics." , (void * )connection );
3392- return aws_raise_error (AWS_ERROR_INVALID_UTF8 );
3393- }
3394-
3395- // TODO: Validate metadata value, when we had metadata in place.
3396-
3397- AWS_LOGF_TRACE (AWS_LS_MQTT_CLIENT , "id=%p: Setting IoT SDK metrics" , (void * )connection );
3398-
3399- /* Clean up existing metrics if any */
3400- if (connection -> metrics_storage ) {
3401- aws_mqtt_iot_sdk_metrics_storage_destroy (connection -> metrics_storage );
3402- connection -> metrics_storage = NULL ;
3403- }
34043387
3388+ struct aws_mqtt_iot_sdk_metrics_storage * metrics_storage = NULL ;
34053389 if (metrics ) {
3406- /* Initialize metrics storage */
3407- connection -> metrics_storage = aws_mqtt_iot_sdk_metrics_storage_new (connection -> allocator , metrics );
3390+ if (aws_mqtt_validate_iot_sdk_metrics (metrics ) == AWS_OP_ERR ) {
3391+ AWS_LOGF_ERROR (AWS_LS_MQTT_CLIENT , "id=%p: Invalid metrics." , (void * )connection );
3392+ return aws_raise_error (AWS_ERROR_INVALID_UTF8 );
3393+ }
34083394
3409- if (!connection -> metrics_storage ) {
3395+ metrics_storage = aws_mqtt_iot_sdk_metrics_storage_new (connection -> allocator , metrics );
3396+ if (!metrics_storage ) {
34103397 int error = aws_last_error ();
34113398 AWS_LOGF_ERROR (
34123399 AWS_LS_MQTT_CLIENT ,
@@ -3418,7 +3405,40 @@ static int s_aws_mqtt_client_connection_311_set_metrics(void *impl, const struct
34183405 }
34193406 }
34203407
3421- return AWS_OP_SUCCESS ;
3408+ int result = AWS_OP_ERR ;
3409+
3410+ /* BEGIN CRITICAL SECTION */
3411+ mqtt_connection_lock_synced_data (connection );
3412+
3413+ if (connection -> synced_data .state != AWS_MQTT_CLIENT_STATE_DISCONNECTED &&
3414+ connection -> synced_data .state != AWS_MQTT_CLIENT_STATE_CONNECTED ) {
3415+ AWS_LOGF_ERROR (
3416+ AWS_LS_MQTT_CLIENT ,
3417+ "id=%p: Connection is currently pending connect/disconnect. Unable to make configuration changes until "
3418+ "pending operation completes." ,
3419+ (void * )connection );
3420+ aws_raise_error (AWS_ERROR_INVALID_STATE );
3421+ goto done ;
3422+ }
3423+
3424+ /* Clean up existing metrics if any */
3425+ if (connection -> metrics_storage ) {
3426+ aws_mqtt_iot_sdk_metrics_storage_destroy (connection -> metrics_storage );
3427+ }
3428+ connection -> metrics_storage = metrics_storage ;
3429+ AWS_LOGF_TRACE (AWS_LS_MQTT_CLIENT , "id=%p: Setting IoT SDK metrics" , (void * )connection );
3430+ metrics_storage = NULL ;
3431+ result = AWS_OP_SUCCESS ;
3432+
3433+ done :
3434+ mqtt_connection_unlock_synced_data (connection );
3435+ /* END CRITICAL SECTION */
3436+
3437+ if (metrics_storage ) {
3438+ aws_mqtt_iot_sdk_metrics_storage_destroy (metrics_storage );
3439+ }
3440+
3441+ return result ;
34223442}
34233443
34243444static struct aws_mqtt_client_connection * s_aws_mqtt_client_connection_311_acquire (void * impl ) {
0 commit comments