Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/ldo.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,23 @@ void luaD_throw (lua_State *L, int errcode) {
LUAI_THROW(L, L->errorJmp);
}
else {
thrlua_log(L, DCRITICAL,
"luaD_throw: no error handler, errcode=%d, L=%p, invoking panic",
errcode, (void *)L);
L->status = cast_byte(errcode);
if (G(L)->panic) {
resetstack(L, errcode);
lua_unlock(L);
/* Do NOT call lua_unlock(L) here. When luaD_throw is reached
* via LUAI_TRY_END re-throw, the LUAI_TRY_FINALLY block has
* already run and the lock state depends on the call site:
* some FINALLY blocks call lua_unlock (lapi.c, lauxlib.c) while
* others call lua_lock (ldo.c callhook/precall). Unconditionally
* unlocking here caused a double-unlock that corrupted the
* pthread mutex on Linux/glibc, making every subsequent
* lua_lock() on this lua_State fail with EINVAL and abort().
* All direct callers of luaD_throw (lmem.c, ldebug.c, lundump.c,
* llex.c) always have an outer errorJmp so they never reach
* this panic branch. */
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Panic callback runs under Lua lock

Medium Severity

Removing lua_unlock(L) means G(L)->panic(L) now executes while the state lock may still be held. In unprotected error paths, panic handlers that call back into Lua APIs can block or fail on re-lock, changing panic behavior from prior releases.

Fix in Cursor Fix in Web

G(L)->panic(L);
}
exit(EXIT_FAILURE);
Expand Down
6 changes: 6 additions & 0 deletions src/thrlua.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ extern void lua_do_longjmp(luai_jmpbuf env, int val)
#define LUAI_TRY_END(L) \
(L)->errorJmp = lj.previous; \
if (lj.status) { \
/* Log the error and re-throw path for diagnostics. */ \
thrlua_log((L), DCRITICAL, \
"LUAI_TRY_END: error status=%d in TRY block at %s:%d, " \
"re-throwing with %s outer handler, L=%p", \
lj.status, lj.file, lj.line, \
lj.previous ? "an" : "NO", (void *)(L)); \
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
luaD_throw((L), lj.status); \
} \
} while(0)
Expand Down