server tcp - Memory Leak

adam_esp
Posts: 37
Joined: Mon Apr 29, 2019 12:09 pm

server tcp - Memory Leak

Postby adam_esp » Mon Apr 29, 2019 12:31 pm

Hello,
It seems that closing the socket does not slow down the memory:
[0;32mI (67366) TestHeap: Free Heap size: 213628[0m
[0;32mI (68366) TestHeap: Free Heap size: 213628[0m
[0;32mI (69366) TestHeap: Free Heap size: 213628[0m
[0;32mI (70366) TestHeap: Free Heap size: 213628[0m
[0;32mI (71366) TestHeap: Free Heap size: 213628[0m
[0;32mI (71816) LL: >>Accepted new connection[0m
[0;32mI (72366) TestHeap: Free Heap size: 213128[0m
[0;32mI (73016) LL: >>Other side closed connection[0m
[0;32mI (73366) TestHeap: Free Heap size: 213360[0m
[0;32mI (74366) TestHeap: Free Heap size: 213364[0m
[0;32mI (74426) LL: >>Accepted new connection[0m
[0;32mI (75366) TestHeap: Free Heap size: 212876[0m
[0;32mI (75826) LL: >>Other side closed connection[0m
[0;32mI (76366) TestHeap: Free Heap size: 213100[0m
[0;32mI (77366) TestHeap: Free Heap size: 213100[0m
[0;32mI (77536) LL: >>Accepted new connection[0m
[0;32mI (78366) TestHeap: Free Heap size: 212616[0m
[0;32mI (79366) TestHeap: Free Heap size: 212616[0m
[0;32mI (79436) LL: >>Other side closed connection[0m
[0;32mI (80366) TestHeap: Free Heap size: 212840[0m
[0;32mI (81246) LL: >>Accepted new connection[0m
[0;32mI (81366) TestHeap: Free Heap size: 212352[0m
[0;32mI (82366) TestHeap: Free Heap size: 212352[0m
[0;32mI (83366) TestHeap: Free Heap size: 212352[0m
[0;32mI (83556) LL: >>Other side closed connection[0m
[0;32mI (84366) TestHeap: Free Heap size: 212576[0m
[0;32mI (85266) LL: >>Accepted new connection[0m
[0;32mI (85366) TestHeap: Free Heap size: 212084[0m
[0;32mI (86366) TestHeap: Free Heap size: 212088[0m
[0;32mI (87366) TestHeap: Free Heap size: 212088[0m
[0;32mI (88066) LL: >>Other side closed connection[0m
[0;32mI (88366) TestHeap: Free Heap size: 212312[0m
[0;32mI (89366) TestHeap: Free Heap size: 212312[0m
[0;32mI (90366) TestHeap: Free Heap size: 212312[0m
[0;32mI (90986) LL: >>Accepted new connection[0m
[0;32mI (91366) TestHeap: Free Heap size: 211824[0m
[0;32mI (92186) LL: >>Other side closed connection[0m
[0;32mI (92366) TestHeap: Free Heap size: 212048[0m
[0;32mI (93366) TestHeap: Free Heap size: 212048[0m
[0;32mI (93386) LL: >>Accepted new connection[0m
[0;32mI (94366) TestHeap: Free Heap size: 211564[0m
[0;32mI (94896) LL: >>Other side closed connection[0m
[0;32mI (95366) TestHeap: Free Heap size: 211788[0m
[0;32mI (96366) TestHeap: Free Heap size: 211788[0m
[0;32mI (97206) LL: >>Accepted new connection[0m
[0;32mI (97366) TestHeap: Free Heap size: 211300[0m
[0;32mI (98366) TestHeap: Free Heap size: 211300[0m
[0;32mI (98606) LL: >>Other side closed connection[0m
[0;32mI (99366) TestHeap: Free Heap size: 211524[0m
[0;32mI (99616) LL: >>Accepted new connection[0m
[0;32mI (100366) TestHeap: Free Heap size: 211040[0m
[0;32mI (101016) LL: >>Other side closed connection[0m
[0;32mI (101366) TestHeap: Free Heap size: 211264[0m
[0;32mI (102366) TestHeap: Free Heap size: 211264[0m
[0;31mE (102626) LL: Unable to accept connection: errno 23[0m
[0;32mI (103366) TestHeap: Free Heap size: 211040[0m
[0;32mI (104366) TestHeap: Free Heap size: 211040[0m
[0;32mI (105366) TestHeap: Free Heap size: 211040[0m
[0;31mE (105626) LL: Unable to accept connection: errno 113[0m
[0;32mI (106366) TestHeap: Free Heap size: 211156[0m
[0;32mI (107366) TestHeap: Free Heap size: 211156[0m
[0;32mI (108366) TestHeap: Free Heap size: 211156[0m
netconn_accept: invalid acceptmbox
[0;31mE (108626) LL: Unable to accept connection: errno 5[0m
[0;32mI (109366) TestHeap: Free Heap size: 211156[0m
does anyone have an idea why this is happening?

a piece of code:

Code: Select all



static void
local_lan_socket(void *arg)
{
	//LWIP_UNUSED_ARG(arg);
	ESP_LOGI(TAG, ":::::::::arg: %04X", (uint32_t)arg);
#ifdef IPV6_LL
	struct sockaddr_in6 dest_addr_ll;
	bzero(&dest_addr_ll.sin6_addr.un, sizeof(dest_addr_ll.sin6_addr.un));
	dest_addr_ll.sin6_family = AF_INET6;
	dest_addr_ll.sin6_port = htons(PORT);
	struct sockaddr_in6 source_addr_ll; // Large enough for both IPv4 or IPv6
#else
	struct sockaddr_in dest_addr_ll;
	dest_addr_ll.sin_addr.s_addr = htonl(INADDR_ANY);
	dest_addr_ll.sin_family = AF_INET;
	dest_addr_ll.sin_port = htons(PORT);
	struct sockaddr_in source_addr_ll;
#endif

	uint addr_len_ll = sizeof(source_addr_ll);
	static fd_set fd_ll;
	struct timeval tv_ll;

	while(1){
		if(ERR_OK != LL_task(initialise_ll)){
			ESP_LOGE(TAG, "LL_task ERR!");
			vTaskDelay(1000 / portTICK_PERIOD_MS);
			continue;
		}
#ifdef IPV6_LL
		if(0 > (ptr_s_var_ll->s_conn_ll.listen_sock_ll = lwip_socket(AF_INET6, SOCK_STREAM, IPPROTO_IPV6))){
#else
		if(0 > (ptr_s_var_ll->s_conn_ll.listen_sock_ll = lwip_socket(AF_INET, SOCK_STREAM, IPPROTO_IP))){
#endif
			LL_task(deinitialise_ll);
			ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
			vTaskDelay(1000 / portTICK_PERIOD_MS);
			continue;
		}
		if(0 != (ptr_s_var_ll->s_conn_ll.err_ll = lwip_bind(ptr_s_var_ll->s_conn_ll.listen_sock_ll, (struct sockaddr *)&dest_addr_ll, sizeof(dest_addr_ll)))){
			LL_task(deinitialise_ll);
			lwip_close(ptr_s_var_ll->s_conn_ll.listen_sock_ll);
			ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);
			vTaskDelay(3000 / portTICK_PERIOD_MS);
			continue;
		}
		if(0 != (ptr_s_var_ll->s_conn_ll.err_ll = lwip_listen(ptr_s_var_ll->s_conn_ll.listen_sock_ll, 1))){
			LL_task(deinitialise_ll);
			lwip_close(ptr_s_var_ll->s_conn_ll.listen_sock_ll);
			ESP_LOGE(TAG, "Error occurred during listen: errno %d", errno);
			vTaskDelay(3000 / portTICK_PERIOD_MS);
			continue;
		}
		while(1){
			vTaskDelay(3000 / portTICK_PERIOD_MS);

			if(0 > (ptr_s_var_ll->s_conn_ll.sock_ll = lwip_accept(ptr_s_var_ll->s_conn_ll.listen_sock_ll, (struct sockaddr *)&source_addr_ll, &addr_len_ll))){
				lwip_close(ptr_s_var_ll->s_conn_ll.listen_sock_ll);
				ESP_LOGE(TAG, "Unable to accept connection LL: errno %d", errno);
				vTaskDelay(3000 / portTICK_PERIOD_MS);
				continue;
			}

			do{
				FD_ZERO(&fd_ll);
				FD_SET(ptr_s_var_ll->s_conn_ll.sock_ll, &fd_ll);
				tv_ll.tv_sec = 15;
				lwip_setsockopt(ptr_s_var_ll->s_conn_ll.sock_ll, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv_ll, sizeof(struct timeval));
				ptr_s_var_ll->s_conn_ll.len_ll = lwip_recv(ptr_s_var_ll->s_conn_ll.sock_ll, ptr_s_var_ll->s_proc_ll.ptr_buff_ll, BUFF_SIZE_LL - 1, 0);
				if(0 > ptr_s_var_ll->s_conn_ll.len_ll){
					lwip_close(ptr_s_var_ll->s_conn_ll.sock_ll);
					ESP_LOGW(TAG, "Recv timeout [errno %d]", errno);
					break;
				}
				else if(0 == ptr_s_var_ll->s_conn_ll.len_ll){
					lwip_close(ptr_s_var_ll->s_conn_ll.sock_ll);
					ESP_LOGI(TAG, "Other side closed connection");
					break;
				}
				if(ERR_OK != (ptr_s_var_ll->s_conn_ll.err_ll = Process_LL(ptr_s_var_ll->s_proc_ll.ptr_buff_ll))){
					lwip_close(ptr_s_var_ll->s_conn_ll.sock_ll);
					ESP_LOGI(TAG, "Error ProcesLL: %d", ptr_s_var_ll->s_conn_ll.err_ll);
					break;
				}
				FD_ZERO(&fd_ll);
				FD_SET(ptr_s_var_ll->s_conn_ll.sock_ll, &fd_ll);
				tv_ll.tv_sec = 3;
				lwip_setsockopt(ptr_s_var_ll->s_conn_ll.sock_ll, SOL_SOCKET, SO_SNDTIMEO,(struct timeval *)&tv_ll,sizeof(struct timeval));
				if(0 > lwip_send(ptr_s_var_ll->s_conn_ll.sock_ll, ptr_s_var_ll->s_proc_ll.ptr_buff_ll, ptr_s_var_ll->s_conn_ll.len_ll, 0)){
					lwip_close(ptr_s_var_ll->s_conn_ll.sock_ll);
					ESP_LOGE(TAG, "Send timeout [errno %d]", errno);
					break;
				}
			}while(1);
		}
		vTaskDelay(3000 / portTICK_PERIOD_MS);
	}
	vTaskDelete(NULL);
}


static err_t LL_task(e_init_ll init_ll)
{
	err_t err_init = ERR_OK;	
	if(deinitialise_ll == init_ll)
	{		
		lwip_close(ptr_s_var_ll->s_conn_ll.listen_sock_ll);
		sys_delay_ms(1000);		
		lwip_close(ptr_s_var_ll->s_conn_ll.sock_ll);
		sys_delay_ms(1000);
		if(NULL != ptr_s_var_ll)
		{
			free(ptr_s_var_ll);		
		}		
		return err_init;
	}
	if(NULL == (ptr_s_var_ll = (s_variable_ll *)malloc(sizeof(s_variable_ll))))
	{
																			
		return ERR_MEM;
	}	
	return err_init;
}


void
InitSockLL(void *arg)
{
	xTaskCreate(local_lan_socket, "local_lan_socket", 5*1024 + BUFF_SIZE_LL, arg, 5, &ll_Handle);
}


ESP_Sprite
Posts: 9766
Joined: Thu Nov 26, 2015 4:08 am

Re: server tcp - Memory Leak

Postby ESP_Sprite » Tue Apr 30, 2019 1:44 am

Hard to say, but it is known that lwip doesn't entirely close sockets until after some time has passed (in order to handle any straggling packets that may still come in for it). Does this behaviour sustain over a longer time, e.g. 15 minutes, as well, or does the memory use stabilize?

adam_esp
Posts: 37
Joined: Mon Apr 29, 2019 12:09 pm

Re: server tcp - Memory Leak

Postby adam_esp » Wed May 01, 2019 6:39 am

Memory will leak to a certain level and it will stay that way. Communication is stopped until reset ESP32.
The problem is solved by setting:

Code: Select all

int pass;
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &pass, sizeof(pass));

Who is online

Users browsing this forum: No registered users and 147 guests