Crash following an assertion failed in tabling cleanup

I’m experiencing a crash that is hard to reproduce. In the log the crash is preceded by an Assertion failed: LD->tabling.component == NULL in pl-tabling.c:4910.

The top of the Prolog stack is:
Prolog stack:
[92] system:$tbl_free_component/1 [PC=1 in supervisor]
[91] $tabling:done_leader/4 [PC=13 in clause 2]
[89] system:catch/3 [PC=2 in clause 1]

The only place in pl-tabling.c where LD->tabling.component is set to NULL is in call_answer_completion and it’s a bit confusing to follow from the source how the code reaches that point. The free_component that is before the assert does not seem to set tabling.component to NULL (it also doesn’t check the flag FC_DESTROY like free_component_set) but I must be missing something…

SWI-Prolog version 8.5.14

@likelion FYI

Thanks for reporting. Assertion errors are always a bug. To propose a fix we first need to understand why this can happen. It seems it is possible that tabling has completed and there is still a pending done_leader/4 call. Why? Can you create a reproducible test case?

Not so far, we’ve been trying to get a stable reproducible test case for months but there are too many moving parts in the project.

Without a test case it gets really hard. You can probably avoid the assertion error by changing the code and making $tbl_free_component/1 succeed, but we have no clue about the cost. Thanks to the XSB folks the test set is quite extensive but never triggered.