Guard against reentrancy issues when operations may trigger autocommands or async events that could invalidate state or cause recursive execution. Check for existing execution context before proceeding and validate that pointers/state remain valid after operations that may trigger autocommands.
Guard against reentrancy issues when operations may trigger autocommands or async events that could invalidate state or cause recursive execution. Check for existing execution context before proceeding and validate that pointers/state remain valid after operations that may trigger autocommands.
Key practices:
win_new_tabpage()
or win_set_buf()
, verify that pointers and handles are still valid before using themmultiqueue_put()
to schedule on appropriate event loops rather than blockingExample from the codebase:
// Check for reentrancy before proceeding
if (in_filetype_autocmd()) {
api_set_error(err, kErrorTypeException,
"Cannot detect default while FileType autocommands are running");
return;
}
// After operations that trigger autocommands, validate state
tabpage_T *tp = win_new_tabpage(after, buf->b_ffname, enter);
if (!tp) {
// tp may have been freed by autocommands
api_set_error(err, kErrorTypeException, "Failed to create tabpage");
return 0;
}
// Validate tp->tp_firstwin is still valid before using
This prevents race conditions where autocommands modify global state during API operations, leading to crashes or data corruption.
Enter the URL of a public GitHub repository