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

Question:packet__queue function maybe have a memory leak #2656

Open
Lieoxc opened this issue Oct 12, 2022 · 0 comments
Open

Question:packet__queue function maybe have a memory leak #2656

Lieoxc opened this issue Oct 12, 2022 · 0 comments

Comments

@Lieoxc
Copy link

Lieoxc commented Oct 12, 2022

I am using mosquitto compiled for the ATMEL Cortex-A5 as broker .
the mosquitto borker memory rises slowly,no stop.
I take some log in int packet__write(struct mosquitto *mosq) 。like that:

	while(packet->to_process > 0){
		write_length = net__write(mosq, &(packet->payload[packet->pos]), packet->to_process);
		if(write_length > 0){
			G_BYTES_SENT_INC(write_length);
			packet->to_process -= write_length;
			packet->pos += write_length;
		}else{
            #ifdef WIN32
                    errno = WSAGetLastError();
            #endif
			if(errno == EAGAIN || errno == COMPAT_EWOULDBLOCK){
				pthread_mutex_unlock(&mosq->current_out_packet_mutex);
				log__printf(NULL, MOSQ_LOG_INFO, "(%s,mid:%d): errno == EAGAIN or COMPAT_EWOULDBLOCK",mosq->id,packet->mid );
				return MOSQ_ERR_SUCCESS;// no free packet
			}else{
				pthread_mutex_unlock(&mosq->current_out_packet_mutex);
				switch(errno){
					case COMPAT_ECONNRESET:
						return MOSQ_ERR_CONN_LOST; // no free packet
					default:
						return MOSQ_ERR_ERRNO; // no free packet
				}
			}
		}
	}

beacuse my mqtt client is busy ,and cannot recv msg quickly,
when socket send buffer is fill, the net__write will cause errno == EAGAIN, then return MOSQ_ERR_SUCCESS without free packet.

when the next message need to pub, in int packet__queue(struct mosquitto *mosq, struct mosquitto__packet *packet) , i add some log :

packet->next = NULL;
pthread_mutex_lock(&mosq->out_packet_mutex);
if(mosq->out_packet){
	log__printf(NULL, MOSQ_LOG_INFO, "(%s,mid:%d): package to out_packet_last->Next,command:%x:",mosq->id,packet->mid,mosq->out_packet->command);
	mosq->out_packet_last->next = packet;
}else{
	log__printf(NULL, MOSQ_LOG_INFO, "(%s,mid:%d): package to out_packet:",mosq->id,packet->mid);
	mosq->out_packet = packet;
}
mosq->out_packet_last = packet;
pthread_mutex_unlock(&mosq->out_packet_mutex);
#ifdef WITH_BROKER
#  ifdef WITH_WEBSOCKETS
if(mosq->wsi){
	libwebsocket_callback_on_writable(mosq->ws_context, mosq->wsi);
	return 0;
}else{
	return packet__write(mosq);
}
#  else
int result = packet__write(mosq);
if(mosq->out_packet){
	log__printf(NULL, MOSQ_LOG_INFO, "(%s,mid:%d): out_packet is not NULL,command:%x :",mosq->id,packet->mid,mosq->out_packet->command);
}

return result;
#  endif

i fund that some times,it will run mosq->out_packet_last->next = packet; ,but i can not found anywhere to free mosq->out_packet_last, only see : mosq->out_packet_last = NULL

it is a memory leak? my mosquitto version is 1.5.0, but i use mosquitto 2.0.15 , it also cause memory rises slowly,no stop. i see mosquitto-2.0.15 packet__queue function and packet__write fucntion as same as mosquitto-1.5.0

this file is add log to packet__queue function ,it found some time, run mosq->out_packet_last->next = packet;
MqttServer_only_change_packet__queue.log

this file is add log to packet__queue function and packet__write function, it found that net__write cause errno == EAGAIN 。
my mqtt client run in STM singlechip ,CPU is 72MHz, cannot recv msg quickly
MqttServer.log

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