github/cmark-gfm

Arena allocator is not thread-safe

Opened this issue · 0 comments

I'm the maintainer of Markable, a wrapper of cmark-gfm for the Janet programming language.

One of the consumers of Markable started getting the following error message intermittently:

cmark-gfm/src/blocks.c:282: finalize: Assertion `b->flags & CMARK_NODE__OPEN' failed.

After checking various things, I think the issue is that the arena allocator in arena.c uses a simple shared struct arena_chunk pointer. If you have multiple threads using multiple parsers that use the arena allocator, it's possible to reset the arena allocator from one thread while a parser in another thread is still trying to use the arena allocator.

I'm not sure what the best approach is to solve this. For Markable, I am using a copy of arena.c that makes the struct arena_chunk thread-local using a macro definition provided by Janet (see here for the modified arena.c and here for Janet's macro definition).