Skip to content

Commit

Permalink
Make minetest abort on lua panic
Browse files Browse the repository at this point in the history
Currently, lua does a regular exit() after a lua panic, which can make
a problem hard to debug. Invoking FATAL_ERROR() instead will print
some useful information, and abort() minetest, so that a debugger can
be used to analyze the situation.
  • Loading branch information
Rogier-5 authored and paramat committed Dec 24, 2016
1 parent a95f983 commit a76e769
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/script/cpp_api/s_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extern "C" {

#include <stdio.h>
#include <cstdarg>
#include <sstream>


class ModNameStorer
Expand Down Expand Up @@ -77,6 +78,8 @@ ScriptApiBase::ScriptApiBase() :
m_luastack = luaL_newstate();
FATAL_ERROR_IF(!m_luastack, "luaL_newstate() failed");

lua_atpanic(m_luastack, &luaPanic);

luaL_openlibs(m_luastack);

// Make the ScriptApiBase* accessible to ModApiBase
Expand Down Expand Up @@ -120,6 +123,16 @@ ScriptApiBase::~ScriptApiBase()
lua_close(m_luastack);
}

int ScriptApiBase::luaPanic(lua_State *L)
{
std::ostringstream oss;
oss << "LUA PANIC: unprotected error in call to Lua API ("
<< lua_tostring(L, -1) << ")";
FATAL_ERROR(oss.str().c_str());
// NOTREACHED
return 0;
}

void ScriptApiBase::loadMod(const std::string &script_path,
const std::string &mod_name)
{
Expand Down
2 changes: 2 additions & 0 deletions src/script/cpp_api/s_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class ScriptApiBase {
#endif

private:
static int luaPanic(lua_State *L);

lua_State* m_luastack;

Server* m_server;
Expand Down

0 comments on commit a76e769

Please sign in to comment.