• Not Answered

LWIP hangs in close(_socket) ( with socket API )

As per subject line

Enviroment IAR 6.30+on TI LM3S9B96 (demo board) + lwip 1.3.2 + SafeRTOS

Code attempts to implement TCP server that sits on port 8080.  First connection is fine, data transfer back and forth is fine.  Attempt to call close() causes lwip to hang in netconn_delete.   More specifically in callback on line 458 in tcpip.c

sys_arch_sem_wait(apimsg->msg.conn->op_completed, 0); 

I have tried to set this task priority lower than priority of lwip tasks.... no help....  System exhibits the same behavior on both blocking and non-blocking socket....

code:

static bool CreateConnection()
{
portTickType ulLastTime = xTaskGetTickCount();
int portno;     
 
    _acceptSock = -1;      
    _listenSock = socket(AF_INET, SOCK_STREAM, 0);     
    if (_listenSock < 0)
    {
      error("ERROR opening socket", true);
      return false;
    }
    
    memset((char *) &serv_addr, sizeof(serv_addr), 0);
    portno = 8080;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
    
    // Make the socket non-blocking so we don't stall the task on the accept call
    int NonBlockingFlg = 1;
    lwip_ioctl( _listenSock, FIONBIO, &NonBlockingFlg );    
    
    if (bind(_listenSock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
    {
      error("ERROR on binding", true);
      return false;
    }
    
    listen(_listenSock, 20);    
    clilen = sizeof(cli_addr);   
 
  return true;
}

static bool AcceptConnection()
{
  _acceptSock = accept(_listenSock, (struct sockaddr *) &cli_addr, &clilen);  
  return _acceptSock > 0;
}

static bool UseConnection()
{
int const IN_BUFFER_SIZE = CHARACTER_ARRAY_0_SIZE/2;
char buffer[IN_BUFFER_SIZE];
int dataLen;
bool retVal = true;
          
  dataLen = recv( _acceptSock, buffer,IN_BUFFER_SIZE - 1, MSG_DONTWAIT);
 
  if(dataLen > 0)
  { // got data
    
    if('x' == buffer[0])
    {
      return false;
    }
    else
    {
      send(_acceptSock, buffer,dataLen,0);
    }
  }
  else
  { // got some error
    int err;
    vPortEnterCritical();
    int retVal = lwip_mike_getlasterror( _acceptSock, &err);
    vPortExitCritical();
 
    if(retVal >= 0)
    {             
      switch(err)
      {
        case EWOULDBLOCK:
        // this is normal - didn't get any data and we have specified non-blocking read
        break;
      
        case ESHUTDOWN:
        // this is disconnect
        /*
        close(_newsockfd);
        _newsockfd = -1;
        close(_sockfd);          
        _sockfd = -1;
        */
        
        retVal = false;
        break;
        
        default:
        {
          char temp[128];
          sprintf(temp,"ERR: %d\r\n", err);
          mi_dbg::DOUT(mi_dbg::DBG_ENET,temp);
        }
        break;
      }
    }
  }
 
  return retVal;
}


typedef enum
{
  ENET_Create
  ,ENET_Accept
  ,ENET_Talk
  ,ENET_Disconnect
  ,ENET_Last
} EnetState_t;

EnetState_t _enetState = ENET_Create;

static void SocketTaskRx(void *pvParameters)
{
    while(1)
    {
      portTickType ulLastTime = xTaskGetTickCount();  
      
      switch(_enetState)
      {
      case  ENET_Create:
        if(lwip_init_done)
        {
          if(CreateConnection())
          {
            mi_dbg::DOUT(mi_dbg::DBG_ENET,"Looking for connection\r\n");
            _enetState =  ENET_Accept;   
          }
        }
        break;
                
      case  ENET_Accept:
          if(AcceptConnection())
          {
             mi_dbg::DOUT(mi_dbg::DBG_ENET,"Connection Accepted\r\n");
             _enetState =  ENET_Talk;     
          }
        break;
        
      case  ENET_Talk:
        if(!UseConnection())
        {
          _enetState =  ENET_Disconnect;     
        }
        break;
        
      case  ENET_Disconnect:
        mi_dbg::DOUT(mi_dbg::DBG_ENET,"Closing conneciton\r\n");
        close(_listenSock);
        close(_acceptSock);
        _enetState =  ENET_Accept;   
        break;
        
      default:
        ASSERT(0);
        break;
      }
      
      xTaskDelayUntil(&ulLastTime, 10);
    }
}

1 Reply