Skip to content

Commit

Permalink
bc: reserve GC parking spaces for place-main thread
Browse files Browse the repository at this point in the history
Don't try to park values when allocating a weak box or pair in a
future thread, since that creates a race on the parking spaces. A
future thread can't run a GC, so it's doesn't need to park.

Touching a future in a future allocates a weak box, so this bug could
have been responsible for many crahses.

Related to racket#3145
  • Loading branch information
mflatt committed May 2, 2020
1 parent 303b410 commit c59f72f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
18 changes: 11 additions & 7 deletions racket/src/racket/gc2/newgc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1744,14 +1744,18 @@ void *GC_malloc_pair(void *car, void *cdr)

if (TAKE_SLOW_PATH() || OVERFLOWS_GEN0(newptr)) {
NewGC *gc = GC_get_GC();
CHECK_PARK_UNUSED(gc);
gc->park[0] = car;
gc->park[1] = cdr;
if (!GC_gen0_alloc_only) {
CHECK_PARK_UNUSED(gc);
gc->park[0] = car;
gc->park[1] = cdr;
}
pair = allocate(sizeof(Scheme_Simple_Object), PAGE_PAIR);
car = gc->park[0];
cdr = gc->park[1];
gc->park[0] = NULL;
gc->park[1] = NULL;
if (!GC_gen0_alloc_only) {
car = gc->park[0];
cdr = gc->park[1];
gc->park[0] = NULL;
gc->park[1] = NULL;
}

/* Future-local allocation can fail: */
if (!pair) return NULL;
Expand Down
22 changes: 13 additions & 9 deletions racket/src/racket/gc2/weak.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void *GC_malloc_weak_array(size_t size_in_bytes, void *replace_val)
GCTYPE *gc = GC_get_GC();
GC_Weak_Array *w;

/* Allcation might trigger GC, so we use park: */
/* Allocation might trigger GC, so we use park: */
CHECK_PARK_UNUSED(gc);
gc->park[0] = replace_val;

Expand Down Expand Up @@ -303,20 +303,24 @@ void *GC_malloc_weak_box(void *p, void **secondary, int soffset, int is_late)
GCTYPE *gc = GC_get_GC();
GC_Weak_Box *w;

/* Allcation might trigger GC, so we use park: */
CHECK_PARK_UNUSED(gc);
gc->park[0] = p;
gc->park[1] = secondary;
if (!GC_gen0_alloc_only) {
/* Allcation might trigger GC, so we use park: */
CHECK_PARK_UNUSED(gc);
gc->park[0] = p;
gc->park[1] = secondary;
}

w = (GC_Weak_Box *)GC_malloc_one_tagged(sizeof(GC_Weak_Box));

/* Future-local allocation may fail: */
if (!w) return NULL;

p = gc->park[0];
secondary = (void **)gc->park[1];
gc->park[0] = NULL;
gc->park[1] = NULL;
if (!GC_gen0_alloc_only) {
p = gc->park[0];
secondary = (void **)gc->park[1];
gc->park[0] = NULL;
gc->park[1] = NULL;
}

w->type = gc->weak_box_tag;
w->val = p;
Expand Down

0 comments on commit c59f72f

Please sign in to comment.