lwip sockets select crash

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
gvandenbosch
Posts: 21
Joined: Sat Nov 22, 2014 8:23 pm

lwip sockets select crash

Post by gvandenbosch »

Hello,

The code I have running on top of lwip does a select on a UDP socket.
However this seems to go wrong at this line:

Code: Select all

/* Don't call SYS_ARCH_UNPROTECT() before signaling the semaphore, as this might
           lead to the select thread taking itself off the list, invalidagin the semaphore. */
        /* CHIBIOS FIX: specific variant of this call to be called from within
           a lock.*/
/*        sys_sem_signal(&scb->sem);*/
        sys_sem_signal_S(&scb->sem);


The first time the signal works without any problem, then for some reason it calls it a second time in that for loop and then comes in port_halt with the error message "inconsistent semaphore".

The lwip_select is only called once, however it goes the second time through the for loop in the event_callback function where it calls the signal, I have no idea why.
Also the next item in the list it puts itself to contains an invalid semaphore next value which probably causes the crash.

What is even more strange is that the next "scb" item which is used the second time to signal points to itself with prev and next attributes.

I have trouble understanding this behaviour, I would expect if I do a single select it only goes through the for loop once performs the signal and it's done.
But something seems to go wrong, could someone give me a pointer on this behaviour or why this might go wrong?

Another weird thing is that if I comment the reschedule in the signal it doesn't crash or if but a break after the first signal it also doesn't crash and can do multiple select after each other.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: lwip sockets select crash

Post by Giovanni »

I don't know about the specific problem, is it possible lwIP releases the semaphore and then tries to use it again? that would explain that error.

Are you using ChibiOS 3.0? probably that "CHIBIOS FIX" is no more required in 3.0. I am taking note about this thread, I plan to review the lwIP related code.

Giovanni
gvandenbosch
Posts: 21
Joined: Sat Nov 22, 2014 8:23 pm

Re: lwip sockets select crash

Post by gvandenbosch »

Hi Giovanni,

Thank you for your quick answer.

We are using the 2.6.8 stable version.

From what I am seeing, is that the signal is called, then the code in the lwip_select function continues after the wait in there.
The weird thing here is that the list is set to 0x0 because the only element in it is handled.

When that is finished it switches back to the second signal directly. where it already gone to the next item in the scb list, while there shouldn't be any.

Update
I found the following thread: http://lists.gnu.org/archive/html/lwip- ... 00117.html
It seems to solve the port_halt, but my code on top not all working properly yet, still investigating.

Cheers,
Gerard
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: lwip sockets select crash

Post by Giovanni »

That is the cause for sure, our implementation does a context switching.
gvandenbosch
Posts: 21
Joined: Sat Nov 22, 2014 8:23 pm

Re: lwip sockets select crash

Post by gvandenbosch »

Hi Giovanni,

Did some more testing and this seems to work well now.
I didn't have any problems with the select anymore.

Cheers,
Gerard
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: lwip sockets select crash

Post by Giovanni »

Hi,

I was planning a workaround for that, how did you fix it?

Giovanni
gvandenbosch
Posts: 21
Joined: Sat Nov 22, 2014 8:23 pm

Re: lwip sockets select crash

Post by gvandenbosch »

Hi Giovanni,

I actually did it quick and dirty as described in the post from the lwip mailinglist.
Just commented the line and moved it to the top of my for loop.

Image

I just looked into the lwip GIT repo and saw they did the same:
http://git.savannah.gnu.org/cgit/lwip.g ... /sockets.c

The for loop is on line 1454, and they also put that line as first in the for loop now.

Cheers,
Gerard
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: lwip sockets select crash

Post by Giovanni »

OK, I will update that file into the zip file in ChibiOS.

thanks,
Giovanni
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: lwip sockets select crash

Post by Giovanni »

Hi,

I committed the fix in the trunk, could you confirm that this fixes the issue?

Giovanni
gvandenbosch
Posts: 21
Joined: Sat Nov 22, 2014 8:23 pm

Re: lwip sockets select crash

Post by gvandenbosch »

Hi Giovanni,

For me this is working so far so good.
Thanks.

Cheers,
Gerard
Post Reply