Main Site Documentation

Socket.Accept () freeze on Fez Cobra II


#1

Hello,
I developed a web application using the MFToolkit webserver.
Using the emulator “Microsoft emulator 4.2” works fine, but using a “Fez Cobra II” motherboard I have two problems:

  1. randomly (but it happens often) the card remains frozen during “client = _listenSocket.Accept ();”, and no other connection is accepted.
    To unlock it, I have to reboot, or in debugging I have to click “Break All” and then click “continue”, either way it works for some time and then refreeze.
  2. the ExtendedWeakReference on reboot randomly does not retrieve the saved data.
    Can I use a different memory area to store the configuration data? I would not use the SD card.

I spent several “sleepless nights” trying to solve, but since it works fine with the emulator, I do not know if I am in error, or the card has problems.

Can you help?
thanks in advance
Luca

The ENC28J60 module is configured so:


this.ethernet_ENC28J60 = new GHI.Premium.Net.EthernetENC28J60(SPI.SPI_module.SPI2, Pin.P1_10, Pin.P2_11, Pin.P1_9, 4000);

This is my ExtendedWeakReference implementation


[Serializable]
public sealed class ConfigurationData : IBDConfigParam
    {
        public ConfigurationData()
        {
            NetworkConfig = new NetworkConfig();
            BoardClientConfig = new BoardClientConfig();
            MailAgentConfig = new MailAgentConfig();
            CentralId = BD_CENTRAL_ID;
        }
        private const String BD_CENTRAL_ID = "centralAlarm";
        private CredentialCollection userList = new CredentialCollection();
            
        public String CentralId { get; set; }        
        public String CentralAlarmProvider { get; set; }        
        public String InstallerCode { get; set; }
        public NetworkConfig NetworkConfig { get; set; }  
        public IBoardClientConfig BoardClientConfig { get; set; }
        public MailAgentConfig MailAgentConfig { get; set; }        
        public CredentialCollection UserList
        {
            get 
            {
                return userList;
            }
        }
    }

public class EWRPersistentData
    {
        private const int EWR_PERSISTENT_DATA_ID = 0;
        /// <summary>
        /// This static field refers to the extended weak reference object, in 
        // order to prevent it from being garbage-collected.
        /// </summary>
        private static ExtendedWeakReference persistentDataExtendedWeakReference;

        /// <summary>
        /// This private class is used as a signature to uniquely identify data 
        /// we own.  It is not necessary to create a dedicated class for this 
        /// purpose.
        /// </summary>
        private static class TypeUniqueToOurApp { }
        private static object syncRoot = new object();
        
        static EWRPersistentData()
        {            
            persistentDataExtendedWeakReference =
            ExtendedWeakReference.RecoverOrCreate(
            typeof(TypeUniqueToOurApp),  // The unique type that identifies the data.
            EWR_PERSISTENT_DATA_ID,      // The ID of the specific data item.
            ExtendedWeakReference.c_SurviveBoot | ExtendedWeakReference.c_SurvivePowerdown); // The CLR should try to persist data across a reboot or power down.

            // Indicate how important this data is.  The CLR discards 
            // OkayToThrowAway items first, then NiceToHave items, then 
            // Important items, then Critical items, and finally System items.
            // In practice, System items are virtually never discarded.
            persistentDataExtendedWeakReference.Priority =
                (Int32)ExtendedWeakReference.PriorityLevel.System;
        }

        /// <summary>
        /// The execution entry point.
        /// </summary>
        public static ConfigurationData GetConfigurationData()
        {
            // Try to get the persisted data, initializing it if it is not 
            // available.  The Target property of the extended weak reference 
            // must be cast to the actual type of the object.  The Target 
            // property is cast to prevent the object from being 
            // garbage-collected unexpectedly.
            ConfigurationData configurationData = (ConfigurationData)persistentDataExtendedWeakReference.Target;

            if (configurationData == null)
            {
                lock (syncRoot)
                {
                    configurationData = (ConfigurationData)persistentDataExtendedWeakReference.Target;
                    if (configurationData == null)
                    {
                            // The object could not be found in flash memory, so create the 
                            // object and initialize it.                
                            configurationData = new ConfigurationData();
                            SetPersistentData(configurationData);
 #if (LOG)
                            Debug.Print("Created ConfigurationData");                        
 #endif
                    }
                }
            }
            else
            {
 #if (LOG)
                Debug.Print("Successfully get ConfigurationData");
 #endif
            }
            
            return configurationData;
        }

        /// <summary>
        /// The execution entry point.
        /// </summary>
        public static void SetPersistentData(ConfigurationData persistentData)
        {               
            // Set the Target property of the extended weak reference to the 
            // boot count object, triggering persistence.
            lock (syncRoot)
            {                
                persistentDataExtendedWeakReference.Target = persistentData;
                Flush();
 #if (LOG)
                Debug.Print("Successfully write ConfigurationData");
 #endif
            }

            // The CLR needs some time to asynchronously store the data in 
            // flash.

            // The system provides no guarantee about when the data will 
            // actually be stored in flash memory.  When the data is actually 
            // stored depends on the size of the object and the speed of the 
            // flash memory.                 
        }

        /// <summary>
        /// The execution entry point.
        /// </summary>
        public static void Flush()
        {
            if (Microsoft.SPOT.Hardware.SystemInfo.SystemID.SKU != 3)
            {
                GHI.Premium.System.Util.FlushExtendedWeakReferences();
            }        
        }
    }


#2

Please use [code…

 tag around your code...

Welcome aboard...
thanks.

#3

@ luca_santoro - try to add: PushBackIntoRecoverList()


if (configurationData == null)
             {
                 lock (syncRoot)
                 {
                     configurationData = (ConfigurationData)persistentDataExtendedWeakReference.Target;
                     if (configurationData == null)
                     {
                             // The object could not be found in flash memory, so create the 
                             // object and initialize it.                
                             configurationData = new ConfigurationData();
                             SetPersistentData(configurationData);
 #if (LOG)
                             Debug.Print("Created ConfigurationData");                        
 #endif
                     }
                 }
             }
             else
             {
                configurationData.PushBackIntoRecoverList();
 #if (LOG)
                 Debug.Print("Successfully get ConfigurationData");
 #endif
             }


#4

I’ve add PushBackIntoRecoverList() and seems to work well.

I have found that the problem of socket freezing only occurs in debug on all running threads.
To unlock just put a new break point or remove one, threads start and then re-lock after some time.

Without debugging works fine.

I have installed the sdk 2013 R3, but the problem persists.
I’ve tried two Fez Cobra II (Net and Wi-Fi + ENC28) and the problem occurs in both.
Fez Cobra II + winusb driver on Mac OSX Maverick + Parallels Desktop 9 with Windows 8.1.


#5

@ luca_santoro - I don’t have an direct answer for your socket.Accept() issue… Show some code please?


#6

it is a known issue in VS2012 right now where the program stops working until you put or remove a break point…

the workaround is to use MFDeploy for your debugging… open MFDeploy choose USB pick your device… next click on File>>Connect… and voila…no more freezing while debugging…

Cheers,
Jay.