[En-Nut-Discussion] Thread is interrupted by another thread and doesn't come back.

Harald Kipp harald.kipp at egnite.de
Tue Nov 28 09:52:55 CET 2006


Hi Petr,

If the savetoque doesn't return ERRQUE_FULL you don't close
the old socket, but create a new one. Sooner or later you
run out of memory, NutTcpCreateSocket will fail and the
loop (without any NutSleep, NutEventWait or similar) will
block the complete system.

Also check the same in case of timeouts.

NutHeapAvailable() helps to verify the number of bytes used
by the program.

The THREAD SEND sets a timeout on serversock, but how can it
be sure that it is created?

Last not least I can't see any reason for using two threads.

Harald


At 08:42 28.11.2006 +0100, you wrote:
>Hello,
>
>   I'm kindly asking you for the help.
>
>   We're building some kind of a converter, which receives data from a
>client and resends them to a server, then receives data from this server
>and resends them to the connected client.
>
>There are two threads which accept connections and save sockets to the
>que. (After the mail there is a simplified code ...)
>Then there is one thread which does the communication. In the while loop
>gets sockets from the que, receives data and does the resending.
>
>What happens is that the receiving thread interrupts the sending thread
>somewhere in NutTcpReceive() or NutTcpSend().
>
>And even if there is no conection comming .. so the NutTcpAccept is just
>blocking the receive thread, it doesn't go back to the send thread. So
>the SEND hangs in the while loop. This happens just every other day.
>
>Most of the time the code works fine.
>
>I would really appreciate your help.
>
>Best regards
>
>Petr Odlozil
>-------------------------------------------------------------
>static u_long timeout = 500
>
>THREAD RECEIVE - receiving commands
>
>for(;;)
>{
>   if ((serversock=NutTcpCreateSocket()) != 0)
>    {
>         if( NutTcpAccept(serversock,commandport)==0) {
>
>         buff[0] = (((unsigned int)serversock) & 0x00FF);
>         buff[1] = ((((unsigned int)serversock) & 0xFF00) >> 8);
>         if (savetoque(buff,2,&sendque)==ERRQUE_FULL)
>         {
>            NutTcpCloseSocket(serversock);
>         }
>         NutEventPostAsync(&queries_comming);
>         }
>    }
>}
>
>THREAD SEND --- resending commands
>for(;;)
>{
>if (readfromque(pombuff,&n,0,&sendque) == ERRQUE_NOERR)
>{
>   // here is getting the serversock from pombuff and opening connection
>the clientsock
>   // then I go to receive data from serversock sending them to client
>sock, receive replies and sending them to serversock.
>
>NutTcpSetSockOpt(serversock,SO_SNDTIMEO,&timeout,sizeof(timeout));
>NutTcpSetSockOpt(serversock,SO_RCVTIMEO,&timeout,sizeof(timeout));
>
>//***somewhere in this cycle it is interrupted by the thread RECEIVE***
>and it doesn't come back to this thread
>
>while ((!NutTcpError(serversock)) && (!NutTcpError(clientsock)))
>{
>     if ((bytes= NutTcpReceive(serversock,buff,commandsize))<0)
>         {       break;
>         };
>      if ((errcode=NutTcpSend(clientsock,buff,bytes))<bytes)
>         {         break;
>         }
>      if ((bytes = NutTcpReceive(clientsock,buff,commandsize))<0)
>         {       break;  };
>      if ((errcode=NutTcpSend(serversock,buff,bytes))<bytes)
>         {  break; }
>}
>
>//if I finish everything from serversock I close sockets and wait for
>the event ...
>NutEventWait(&queries_comming,0);
>}
>_______________________________________________
>En-Nut-Discussion mailing list
>En-Nut-Discussion at egnite.de
>http://lists.egnite.de/mailman/listinfo/en-nut-discussion




More information about the En-Nut-Discussion mailing list