AnsweredHot!Packet heap not freed when closing a socket with no network connection

Author
paul_o
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2017/11/29 09:15:58
  • Location: 0
  • Status: offline
2017/11/30 02:24:21 (permalink)
0

Packet heap not freed when closing a socket with no network connection

Hi,
I've been trying to find an answer to this problem but have had no joy and hope the solution is something I've overlooked.
 
This problem only occurs when I have no network connection.
My pic32MX is trying to connect to another device on the network using the TCPIP_TCP_ClientOpen function.
The memory is allocated to the socket and its state becomes TCPIP_TCP_STATE_CLIENT_WAIT_CONNECT
The TCPIP_MAC_PKT_FLAG_QUEUED flag on the socket packet is set
As the socket never connects to the network I then close the socket using the TCPIP_TCP_Close function
All memory is freed except for the packet memory, which is roughly 200 bytes. If I attempt enough connections the heap eventually becomes used up and I can no longer make any further TCP connections.
 
The reason I found for the memory not being freed is due to the TCPIP_MAC_PKT_FLAG_QUEUED flag. A piece of logic in the _TxSktFreeLockedV4Pkt function states it will be freed by the acknowledge, but this acknowledge never comes.
 
The 2 ways I've found round this is either not to attempt a connection if the pic32MX is not connected to the network, or manually removing the TCPIP_MAC_PKT_FLAG_QUEUED flag prior to closing the socket. However if the network connection is poor enough that my code does try a connection when the network is down I would like a clean way to free up the resources that doesn't rely on a workaround function.
 
Has anyone come up against this before? As I haven't found anything related to this problem my guess is I've missed something obvious. Any help would be appreciated.
 
I'm using a v1_09 of Harmony.
 
Thanks,
 
Paul 
#1
rainad
Moderator
  • Total Posts : 966
  • Reward points : 0
  • Joined: 2009/05/01 13:39:25
  • Location: 0
  • Status: offline
Re: Packet heap not freed when closing a socket with no network connection 2017/11/30 19:45:44 (permalink)
0
It may be that the packet is queued by ARP to obtain the MAC address of the gateway. But the ARP timeout is pretty long by default. You could try to use a small timeout and see if this solves your problem.
I'll try to look into it and detect what really happens. This occurs with no network cable plugged in, or under what exact conditions?
 
 
#2
paul_o
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2017/11/29 09:15:58
  • Location: 0
  • Status: offline
Re: Packet heap not freed when closing a socket with no network connection 2017/12/01 01:55:27 (permalink)
0
Hi rainad,
 
I connect to a router and have a static IP address. I make several successful TCP calls to another device on the network. I then remove the network cable from the router so that I no longer have a network connection and attempt to connect to the device again. This is when my available heap size reduces by 208 bytes and is never returned.
 
I looked at the ARP timeouts and the only large one was 'Time-Out for a Solved Entry to be Removed if not Referenced in Seconds' which I reduced to 60. I then tried again and after 10 minutes I still didn't have the memory returned to the heap.
 
Thanks for your help.
#3
rainad
Moderator
  • Total Posts : 966
  • Reward points : 0
  • Joined: 2009/05/01 13:39:25
  • Location: 0
  • Status: offline
Re: Packet heap not freed when closing a socket with no network connection 2017/12/01 09:24:50 (permalink)
0
If the connection is already established, then the TCP socket will re-try to send its packets and stay connected for ever. It cannot possible know that the destination host is off line, disconnected, routers crashed, etc. along the way.
Enable keep-alive if you want the socket to give up when the connection was lost.
 
 
#4
paul_o
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2017/11/29 09:15:58
  • Location: 0
  • Status: offline
Re: Packet heap not freed when closing a socket with no network connection 2017/12/04 02:08:11 (permalink)
0
I gave that a go, but it didn't change anything.
I'm not convinced the TCP socket is trying to resend its packet, as when I reconnect the cable no message is sent to the external device. Also, the function _TcpSocketSetSourceInterface returns false when I call TCPIP_TCP_ClientOpen, so I guess it knows there is no connection.
I assumed the packet was queued because there was no network connection, so that when the connection was re-established, the packet could be sent. However, for some reason, the packet is never processed again and is forgotten about.
#5
rainad
Moderator
  • Total Posts : 966
  • Reward points : 0
  • Joined: 2009/05/01 13:39:25
  • Location: 0
  • Status: offline
Re: Packet heap not freed when closing a socket with no network connection 2017/12/04 16:18:55 (permalink)
4 (1)
I've confirmed that there's a memory leak when the socket is opened and there is no network connection.
I've fixed the code. It's kind of late to include the fix in the coming v2.05 release but I'll see what can be done.
Thank you for finding and reporting this.
 
 
 
 
#6
NKurzman
A Guy on the Net
  • Total Posts : 16027
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: Packet heap not freed when closing a socket with no network connection 2017/12/04 16:52:07 (permalink)
4 (1)
he is Using V 1.09.  Can you Post the Fix?
#7
paul_o
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2017/11/29 09:15:58
  • Location: 0
  • Status: offline
Re: Packet heap not freed when closing a socket with no network connection 2017/12/05 01:36:34 (permalink)
0
Thanks for your help rainad, much appreciated.
#8
rainad
Moderator
  • Total Posts : 966
  • Reward points : 0
  • Joined: 2009/05/01 13:39:25
  • Location: 0
  • Status: offline
Re: Packet heap not freed when closing a socket with no network connection 2017/12/05 10:15:32 (permalink) ☼ Best Answerby paul_o 2017/12/06 03:08:24
5 (1)
Here's a version for v1.09.
Please give it a try and see if it fixes the the issue for you.
 
 P.S. I'd recommend upgrading to latest v2 rev though.
post edited by rainad - 2017/12/05 10:18:03
#9
NKurzman
A Guy on the Net
  • Total Posts : 16027
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: Packet heap not freed when closing a socket with no network connection 2017/12/05 10:45:35 (permalink)
5 (1)
If Only Everyone could update to the Latest.
#10
friesen
Super Member
  • Total Posts : 1920
  • Reward points : 0
  • Joined: 2008/05/08 05:23:35
  • Location: Indiana, USA
  • Status: offline
Re: Packet heap not freed when closing a socket with no network connection 2017/12/05 17:10:45 (permalink)
0
@Rainad, can you post a patch/fix for 2.x ?

Erik Friesen
#11
rainad
Moderator
  • Total Posts : 966
  • Reward points : 0
  • Joined: 2009/05/01 13:39:25
  • Location: 0
  • Status: offline
Re: Packet heap not freed when closing a socket with no network connection 2017/12/06 14:04:53 (permalink)
0
Here's the updated version for v2.04.
 
#12
friesen
Super Member
  • Total Posts : 1920
  • Reward points : 0
  • Joined: 2008/05/08 05:23:35
  • Location: Indiana, USA
  • Status: offline
Re: Packet heap not freed when closing a socket with no network connection 2017/12/06 14:11:28 (permalink)
0
Would you mind giving the patch instead of the complete file?

Erik Friesen
#13
rainad
Moderator
  • Total Posts : 966
  • Reward points : 0
  • Joined: 2009/05/01 13:39:25
  • Location: 0
  • Status: offline
Re: Packet heap not freed when closing a socket with no network connection 2017/12/11 16:10:20 (permalink)
0
Sorry for the delay.
You can compare the file I provided with the one from the v2.04 distribution:
- one line removed in the _TCPv4Flush() function
- _TcpSend() function has changed (around line #3629 or so).
 
#14
friesen
Super Member
  • Total Posts : 1920
  • Reward points : 0
  • Joined: 2008/05/08 05:23:35
  • Location: Indiana, USA
  • Status: offline
Re: Packet heap not freed when closing a socket with no network connection 2017/12/12 09:11:56 (permalink)
0
I tried, but there seemed to be a ton of unrelated changes, like tab changes.

A side note, I'd like to see properly formatted code to a single standard. Also, every if should have a curly brace.

Erik Friesen
#15
Jump to:
© 2018 APG vNext Trial Version 4.5