lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



[snipped info about fiddling with TASK_SLOTS_STEP]

LuaTask allocates TASK_ENTRY memory in TASK_SLOTS_STEP *
sizeof(TASK_ENTRY) chunks. Problem arises when it
reallocates memory to grow one step more.

Actually, the segfault doesn't happen directly at the point the realloc() happens. (After looking at the code I realised I should have said this :-) It's only later that it crashes, but apparently only after such a realloc() has taken place.

In my code, I call task.create() more than TASK_SLOTS_STEP times. All my tasks then block on task.receive(-1) until the master task has finished creating them all. The master task then messages each one in turn. Then each task sleeps for a couple of (run-time specified) seconds. Then it sort()s the table it was passed as an argument a few times, messages the master task and ends. When the master task has received messages from all the subtasks, it records success and exits.

From what I can see from debugging print()s in the code, the segfault only
happens when the first subtask finishes, like this (Sn means task n finished its sleep and is about to start sorting, and Fn means it finished its sorting and is about to exit):

:~/lua-try$ lua master.lua 255 10 1000 2
255 tasks sorting 10 items 1000 times (initial delay 2s).
Creating tasks...done.
Starting tasks...done.
Waiting...
S2 S3 S4 F3 F2 S5 S6 S7 S8 S9 S10 S11 S12 S13 S14 S15 F6 F5 F8 F7 F9 F11 F10 F12 F13 F14 F4 F15 S16 S17 F17 S18 S19 S20 S21 F21 S22 S23 S24 F24 S25
[blah, blah, snip]
F225 F218 F108 F137 F118 F141 F150 F139 F232 F181
***All reported finished

~/lua-try$ lua master.lua 256 10 10 2 256 tasks sorting 10 items 10 times (initial delay 2s).
Creating tasks...done.
Starting tasks...done.
Waiting...
S2 F2 Segmentation fault

~/lua-try$ lua master.lua 258 1000 10 2
258 tasks sorting 1000 items 10 times (initial delay 2s).
Creating tasks...done.
Starting tasks...done.
Waiting...
S2 S3 S4 S5 S6 S7 S8 S9 S10 S11 S12 S13 S14 S15 S16 S17 S18 S19 S20 F3 Segmentation fault

~/lua-try$ lua master.lua 300 10 1000 2
300 tasks sorting 10 items 1000 times (initial delay 2s).
Creating tasks...done.
Starting tasks...done.
Waiting...
S2 S3 S4 S5 S6 S7 S8 S9 S10 S11 S12 S13 S14 S15 F8 *** glibc detected *** dlua: double free or corruption (out): 0x08072330 ***
[trace of corrupted heap snipped]

The size of TASK_ENTRY structure is 52 bytes on Linux.
As a workaround, you can set TASK_SLOTS_STEP larger
enough in order to avoid reallocation

And to prevent the realloc(), I've just changed this in ltask.c, so that the TASK_SLOT_STEPping never happens and the problem is apparently prevented:

[in int_taskcreate]
  for( i = 0; i < countTask; i++)
    if( !( aTask[i].running))
      break;
  if( i == countTask) {
    TASK_ENTRY *te;
    long j;

    te = ( TASK_ENTRY *) realloc( aTask, sizeof( TASK_ENTRY) * ( countTask + TASK_SLOTS_STEP));
    if( te == NULL) {
      OsUnlockMutex( tlMutex);
      return( -1);
    }
    aTask = te;


[changed to]:

    for( i = 0; i < countTask; i++)
      if( !( aTask[i].running))
        break;
    if( i == countTask) {
        OsUnlockMutex( tlMutex);
        return( -1);
    }
    [delete to next closing squiggly bracket]

Apologies for the rather long post.