Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

enc28j60_lwip_recv: input alloc packet failed


  • Please log in to reply
36 replies to this topic

#1 wendo

wendo

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 25 September 2014 - 09:08 AM

Hi

 

I'm getting this error intermittently on my N2+ when trying to send an HTTP GET command. I'm also getting "IP Address: <ip address>" printed semi regularly that isn't generated from my code. Presumably both are coming from a lower level and I have managed to track down the error message in what appears to be the NETMF firmware files, but it doesn't really make much sense to me.

 

The code that's generating the error is as below. It's called every 30 seconds from a timer. Can anyone explain what I'm doing wrong or how to get rid of the error? It seems to recover from this most of the time and continue but other times it then stops sending network traffic, although the rest of the program continues on fine.

        private static void UploadData(object sender)
        {
            var URLString = "http://data.sparkfun.com/input/" + sfPublicKey + "?private_key=" + sfPrivateKey + "&humidity=" + humidity.ToString("F1") + "&pressure=" + pressure.ToString("F0") + "&rain=" + rainfall.ToString("F0") + "&temp=" + temp.ToString("F1") + "&voltage=" + voltage.ToString("F2") + "&winddirection=" + windDirection.ToString("F0") + "&windgust=" + windGust.ToString("F1") + "&windspeed=" + windSpeed.ToString("F1") + "&charging=" + charging.ToString();
 
            try
            {
                using (HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URLString))
                {
                    request.Method = "GET";
                    request.KeepAlive = false;
                    request.Timeout = 5000;
                    request.ReadWriteTimeout = 5000;
 
                    using (var response = (HttpWebResponse)request.GetResponse())
                    {
                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            Debug.Print("HTTP Status:" + response.StatusCode + " : " + response.StatusDescription);
                        }
                    }
                    request.Dispose();
                }
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
        }


#2 Josh Hawley

Josh Hawley

    Member

  • Members
  • PipPip
  • 21 posts

Posted 25 September 2014 - 04:39 PM

alloc errors are usually tied to memory. Try lowering your memory footprint. 

 

One place to start with that is the way you are concatenating strings. Every time you add one string to another, you create a new string. in a long chain like that there are a ton of these extra strings created that waste memory. 

The StringBuilder class is made to handle that problem. 

 

try something like this:

var sb = new StringBuilder("http://data.sparkfun.com/input/");
sb.Append(sfPublicKey);
sb.Append("?private_key=");
sb.Append(sfPrivateKey);
.
.
.

then when you are done, you can call sb.ToString() to turn it back into a string.

var URLString = sb.ToString();

There is also a more advanced way to do it with a method called AppendFormat(), but it does not appear to be implemented in .netMF =(



#3 Josh Hawley

Josh Hawley

    Member

  • Members
  • PipPip
  • 21 posts

Posted 25 September 2014 - 04:45 PM

Hmm... this was also posted by the .netMF team today... 
Ref: http://blogs.msdn.com/b/netmfteam/archive/2014/09/23/net-micro-framework-sdk-beta-released.aspx
 

 

Networking

The .NET micro Framework team has also been investigating network stability issues that have been reported in the community.  We are looking at the broad range of the product's networking capabilities, including TCP, IP, UDP, and DHCP protocols.  This topic probably deserves its own blog article to go into details but in a nutshell we have been able to reproduce some of the issues reported and the team is putting together an engineering plan to resolve them.


Edited by Josh Hawley, 25 September 2014 - 04:47 PM.


#4 wendo

wendo

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 25 September 2014 - 07:32 PM

I should mention I'm running 4.3.1 firmware. Here's hoping they will fix it and we'll get a new firmware release otherwise my weather station receiver will need to move platforms. It's not ideal to have a data logger that stops logging data every day



#5 mduhalde

mduhalde

    New Member

  • Members
  • Pip
  • 1 posts

Posted 25 September 2014 - 07:48 PM

I had the same error "enc28j60_lwip_recv: input alloc packet failed"

This happens only when I plug the netduino to the corporate network. If I test any code out of the corporation network everithings go fine, but at the moment I plug it, my netduino stop working and i have to manually reset it.

 

I think that my corporation network make lot of broadcast trafic and the netduino is not able to process them and get this out of memory error.

Could be this the reason of the error???

 

I look at ENC28J60_LWIP.cpp code and can not find any other reason....

 

int enc28j60_lwip_recv( struct netif *pNetIF )

{  ...

    if (length != 0)
            {
                pPBuf = pbuf_alloc( PBUF_RAW, length, PBUF_RAM );
                if ( pPBuf )
                {
                   dataRX = (UINT8 *)pPBuf->payload;

                   .....

                }
                else
                {
                    hal_printf("enc28j60_lwip_recv: input alloc packet failed \r\n");
                } 



#6 sfx

sfx

    Advanced Member

  • Members
  • PipPipPip
  • 52 posts

Posted 25 September 2014 - 09:31 PM

Hi Wendo,

 

Can you show us how your code is being called from a timer? The more context we have the better we will be able to troubleshoot the problem. For instance, in the past, I have had problems with application "halts" when using timers in the conventional way.

 

Nathan



#7 Josh Hawley

Josh Hawley

    Member

  • Members
  • PipPip
  • 21 posts

Posted 26 September 2014 - 02:52 AM

I still think you have a memory leak somewhere. The error makes me think it is trying to allocate memory for an incoming packet and failing. I agree, seeing all of the code (minus your private key and any other login info of course) would go a long way toward helping you.



#8 Spiked

Spiked

    Advanced Member

  • Members
  • PipPipPip
  • 129 posts

Posted 26 September 2014 - 12:18 PM

One thing that caught my eye was calling request.dispose, after using it with a using statement - i'm not sure of the consequences of a double dispose.



#9 wendo

wendo

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 27 September 2014 - 06:21 PM

I put the dispose in there as I had the same idea that it was a memory leak so I wanted to make sure the request was being cleaned up. 

 

In saying that, I haven't seen the same issue for a couple of days now so I'm wondering if something else I've done has fixed it. Code is attached below. It's a decent size so it's not inline.

 

Basically I have an arduino with weather sensors attached mounted outside. It sends all the data to the Netduino via an nRF24L01+ radio system every 10 seconds and from there the Netduino uploads it to Sparkfun (and now Weather Underground) ever 30 seconds. 

 

Just thinking about how I've seen this happening it's possible the errors are actually coming from the NTP syncing which I'm doing once an hour as the only time I've seen these errors is when I've left MFDeploy sitting running for hours/days and I've had to remove all the Debug.print statements from my program to even see them as the scrollback buffer was never long enough so I don't know for sure that it's coming from the original timer.

 

Any ideas welcome

Attached Files



#10 Josh Hawley

Josh Hawley

    Member

  • Members
  • PipPip
  • 21 posts

Posted 27 September 2014 - 11:43 PM

It is possible that your NTP thread and Upload thread are colliding at the socket. You should put a lock() around all of the code in both of those methods to enforce that one of them does not walk on the other. I know that you "should" be able to maintain multiple simultaneous sockets, but the enc28j60 isn't really that strong of a networking chip.



#11 wendo

wendo

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 28 September 2014 - 05:53 AM

I'm getting a little lost trying to implement the locks. both lock and Monitor appear to require you specifying the actual object s to protech, but I just want to lock entire code blocks, and the objects are different in each block anyway?!

 

Thanks



#12 wendo

wendo

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 28 September 2014 - 06:41 AM

I've switched over to the Toolbox HTTP_Client library rather than the built in one to see if that may help, I'll also look at switching over to the Toolbox NTP library too if I continue to see it and go from there. I'd still love to know how to lock the code blocks though



#13 Spiked

Spiked

    Advanced Member

  • Members
  • PipPipPip
  • 129 posts

Posted 28 September 2014 - 02:48 PM

Lock requires any object as a reference only. So you can use any reference variable that both places that need to lock have access to.  It is often easy just to create something for this purpose then in both place surround your code with; lock(lockObject) { ... }

 

Make sure you do it at a logical place, like before a transaction starts, but not too early or for too long.

namespace junk
{
    public class Program
    {
        static Object lockObject = new Object();
        public static void Main()
        {
            new Thread(thread1).Start();
            new Thread(thread1).Start();
            new Thread(thread1).Start();
        }

        static void thread1()
        {
            lock (lockObject)
            {
                // ... do stuff
            }
        }
    }
}



#14 wendo

wendo

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 29 September 2014 - 01:48 AM

Thanks for that, and for the record, the toolbox HTTP_Client appears to make it much much worse. I'll see how I go with adding some locks. Failing that I might add an RTC and remove the NTP code entirely and see if the problem persists



#15 Josh Hawley

Josh Hawley

    Member

  • Members
  • PipPip
  • 21 posts

Posted 29 September 2014 - 05:22 AM

Does time really slip that much on a netduino? Maybe just running the NTP sync on startup is enough?



#16 wendo

wendo

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 29 September 2014 - 06:11 AM

The idea is for this to pretty much run uninterrupted for long periods of time so startup may only happen once every few months. I could probably push it out to once a week but I've got it going as often as it is pretty much exactly to catch these sorts of things :)



#17 wendo

wendo

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 29 September 2014 - 08:38 AM

Locking didn't help, onto doing a single NTP sync on boot and seeing what happens with only the web traffic



#18 Josh Hawley

Josh Hawley

    Member

  • Members
  • PipPip
  • 21 posts

Posted 01 October 2014 - 04:27 AM

Have you tried monitoring the memory usage?

 

you could just add it as another data parameter to the ones that you are already tracking. 

If you track it, you can at least confirm or eliminate a memory leak is your problem.

 

use this to get free memory:

uint freemem = Microsoft.SPOT.Debug.GC(false);


#19 wendo

wendo

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 02 October 2014 - 03:56 AM

I haven't, but that's only because I have never known how too :)

 

With only an initial NTP sync I still see it so it's not NTP that's causing the problem. I actually captured the exception and it's coming from inside System.http.

 

I do suspect a memory leak but as far as I can tell it's inside the HTTP library. Anyway, I'll start tracking it now and we'll see what we end up with. The error doesn't appear to be the actual cause of the lockups, at least not directly, the lockup itself is the above mentioned exception (so not technically a lockup) and the errors occur multiple times before that happens, seemingly at random. It also doesn't appear to be tied to one site specifically as I've enabled and disabled both the sparkfun stream and the weather underground stream and it continued to happen on both, so it's not some screwy response from the server causing it.



#20 wendo

wendo

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 04 October 2014 - 11:07 AM

So, not a memory leak, free memory stays pretty static around 79000

 

Is there some way I can catch the exceptions coming from System.Net and System.Http?






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.