FIX (partially) #2703: bug in stack overflow handling when inside block
authorSimon Marlow <marlowsd@gmail.com>
Mon, 20 Oct 2008 12:11:03 +0000 (12:11 +0000)
committerSimon Marlow <marlowsd@gmail.com>
Mon, 20 Oct 2008 12:11:03 +0000 (12:11 +0000)
As a result of a previous ticket (#767) we disabled the generation of
StackOverflow exceptions when inside a Control.Exception.block, on the
grounds that StackOverflow is like an asynchronous exception.  Instead
we just increase the stack size.  However, the stack size calculation
was wrong, and ended up not increasing the size of the stack, with the
result that the runtime just kept re-allocating the stack and filling
up memory.

rts/Schedule.c

index f53687a..626c097 100644 (file)
@@ -2125,10 +2125,17 @@ threadStackOverflow(Capability *cap, StgTSO *tso)
   }
 
   /* Try to double the current stack size.  If that takes us over the
-   * maximum stack size for this thread, then use the maximum instead.
-   * Finally round up so the TSO ends up as a whole number of blocks.
+   * maximum stack size for this thread, then use the maximum instead
+   * (that is, unless we're already at or over the max size and we
+   * can't raise the StackOverflow exception (see above), in which
+   * case just double the size). Finally round up so the TSO ends up as
+   * a whole number of blocks.
    */
-  new_stack_size = stg_min(tso->stack_size * 2, tso->max_stack_size);
+  if (tso->stack_size >= tso->max_stack_size) {
+      new_stack_size = tso->stack_size * 2;
+  } else { 
+      new_stack_size = stg_min(tso->stack_size * 2, tso->max_stack_size);
+  }
   new_tso_size   = (lnat)BLOCK_ROUND_UP(new_stack_size * sizeof(W_) + 
                                       TSO_STRUCT_SIZE)/sizeof(W_);
   new_tso_size = round_to_mblocks(new_tso_size);  /* Be MBLOCK-friendly */