Skip to content

Commit

Permalink
added lsobj lua system function; fixed CurlLib memory leak when reque…
Browse files Browse the repository at this point in the history
…st is prematurely terminated
  • Loading branch information
jpswinski committed Apr 3, 2024
1 parent 01f5b8b commit 5338ef6
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 8 deletions.
23 changes: 23 additions & 0 deletions packages/core/LuaLibrarySys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const struct luaL_Reg LuaLibrarySys::sysLibs [] = {
{"healthy", LuaLibrarySys::lsys_healthy},
{"ipv4", LuaLibrarySys::lsys_ipv4},
{"lsrec", LuaLibrarySys::lsys_lsrec},
{"lsobj", LuaLibrarySys::lsys_lsobj},
{"cwd", LuaLibrarySys::lsys_cwd},
{"memu", LuaLibrarySys::lsys_memu},
{"setmemlimit", LuaLibrarySys::lsys_setmemlimit},
Expand Down Expand Up @@ -544,6 +545,28 @@ int LuaLibrarySys::lsys_lsrec (lua_State* L)
return 0;
}

/*----------------------------------------------------------------------------
* lsys_lsobj
*----------------------------------------------------------------------------*/
int LuaLibrarySys::lsys_lsobj (lua_State* L)
{
(void)L;

vector<LuaObject::object_info_t> globals;
LuaObject::getGlobalObjects(globals);
print2term("\n%30s %s\n", "Object Name", "Reference");
for(LuaObject::object_info_t& object: globals)
{
long reference_count = object.refCnt;
print2term("%30s %ld %s\n", object.objName.c_str(), reference_count, object.objType.c_str());
}

print2term("\nNumber of Global Objects: %lu\n", (unsigned long)globals.size());
print2term("Total Number of Objects: %ld\n", LuaObject::getNumObjects());

return 0;
}

/*----------------------------------------------------------------------------
* lsys_cwd - current working directory
*----------------------------------------------------------------------------*/
Expand Down
1 change: 1 addition & 0 deletions packages/core/LuaLibrarySys.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class LuaLibrarySys
static int lsys_healthy (lua_State* L);
static int lsys_ipv4 (lua_State* L);
static int lsys_lsrec (lua_State* L);
static int lsys_lsobj (lua_State* L);
static int lsys_cwd (lua_State* L);
static int lsys_memu (lua_State* L);
static int lsys_setmemlimit (lua_State* L);
Expand Down
39 changes: 39 additions & 0 deletions packages/core/LuaObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
const char* LuaObject::BASE_OBJECT_TYPE = "LuaObject";
Dictionary<LuaObject::global_object_t> LuaObject::globalObjects;
Mutex LuaObject::globalMut;
std::atomic<long> LuaObject::numObjects(0);

/******************************************************************************
* PUBLIC METHODS
Expand Down Expand Up @@ -217,6 +218,38 @@ int LuaObject::returnLuaStatus (lua_State* L, bool status, int num_obj_to_return
return num_obj_to_return;
}

/*----------------------------------------------------------------------------
* getGlobalObjects
*----------------------------------------------------------------------------*/
void LuaObject::getGlobalObjects (vector<object_info_t>& globals)
{
globalMut.lock();
{
global_object_t global_object;
const char* object_name = globalObjects.first(&global_object);
while(object_name != NULL)
{
object_info_t info = {
.objName = object_name,
.objType = global_object.lua_obj->getType(),
.refCnt = global_object.lua_obj->referenceCount
};
globals.push_back(info);
object_name = globalObjects.next(&global_object);
}
}
globalMut.unlock();
}

/*----------------------------------------------------------------------------
* getNumObjects
*----------------------------------------------------------------------------*/
long LuaObject::getNumObjects (void)
{
long num_objects = numObjects;
return num_objects;
}

/*----------------------------------------------------------------------------
* getLuaObjectByName
*----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -302,6 +335,9 @@ LuaObject::LuaObject (lua_State* L, const char* object_type, const char* meta_na
mlog(DEBUG, "Created object of type %s/%s", getType(), LuaMetaName);
}

/* Count Object */
numObjects++;

/* Start Trace */
traceId = start_trace(DEBUG, engine_trace_id, "lua_object", "{\"object_type\":\"%s\", \"meta_name\":\"%s\"}", object_type, meta_name);
}
Expand All @@ -325,6 +361,9 @@ LuaObject::~LuaObject (void)
}
}
globalMut.unlock();

/* Count Object */
numObjects--;
}

/*----------------------------------------------------------------------------
Expand Down
12 changes: 11 additions & 1 deletion packages/core/LuaObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ class LuaObject
LuaObject* luaObj;
} luaUserData_t;

typedef struct {
string objName;
string objType;
long refCnt;
} object_info_t;

/*--------------------------------------------------------------------
* Methods
*--------------------------------------------------------------------*/
Expand All @@ -96,6 +102,9 @@ class LuaObject
static const char* getLuaString (lua_State* L, int parm, bool optional=false, const char* dfltval=NULL, bool* provided=NULL);
static int returnLuaStatus (lua_State* L, bool status, int num_obj_to_return=1);

static void getGlobalObjects (vector<object_info_t>& globals);
static long getNumObjects (void);

static LuaObject* getLuaObjectByName (const char* name, const char* object_type);
bool releaseLuaObject (void); // pairs with getLuaObject(..) and getLuaObjectByName(..), returns whether object was deleted

Expand Down Expand Up @@ -141,7 +150,7 @@ class LuaObject
static int luaDestroy (lua_State* L);
static int luaName (lua_State* L);
static int luaWaitOn (lua_State* L);

/*--------------------------------------------------------------------
* Types
*--------------------------------------------------------------------*/
Expand All @@ -156,6 +165,7 @@ class LuaObject

static Dictionary<global_object_t> globalObjects;
static Mutex globalMut;
static std::atomic<long> numObjects;

std::atomic<long> referenceCount;
luaUserData_t* userData;
Expand Down
24 changes: 17 additions & 7 deletions packages/netsvc/CurlLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ long CurlLib::postAsRecord (const char* url, const char* data, Publisher* outq,
.hdr_index = 0,
.rec_size = 0,
.rec_index = 0,
.err_cnt = 0,
.rec_buf = NULL,
.outq = outq,
.url = url,
Expand Down Expand Up @@ -323,6 +324,12 @@ long CurlLib::postAsRecord (const char* url, const char* data, Publisher* outq,
{
delete [] parser.rec_buf;
}

/* Display Error Message if Errors Encountered */
if(parser.err_cnt > 0)
{
mlog(CRITICAL, "Errors encountered on request to %s: %d", url, parser.err_cnt);
}
}

/* Terminate Stream */
Expand Down Expand Up @@ -533,7 +540,7 @@ size_t CurlLib::postRecords(void *buffer, size_t size, size_t nmemb, void *userp
uint8_t* input_data = static_cast<uint8_t*>(buffer);
uint32_t input_index = 0;

while(bytes_to_process > 0)
while((!parser->active || *parser->active) && (bytes_to_process > 0))
{
if(parser->rec_size == 0) // record header
{
Expand Down Expand Up @@ -583,17 +590,20 @@ size_t CurlLib::postRecords(void *buffer, size_t size, size_t nmemb, void *userp
while((!parser->active || *parser->active) && post_status == MsgQ::STATE_TIMEOUT)
{
post_status = parser->outq->postRef(parser->rec_buf, parser->rec_size, SYS_TIMEOUT);
// reset body
if(post_status != MsgQ::STATE_TIMEOUT)
{
parser->rec_index = 0;
parser->rec_size = 0;
}
// handle post errors
if(post_status < 0)
{
// handle post errors
mlog(DEBUG, "Failed to post response for %s: %d", parser->url, post_status);
delete [] parser->rec_buf;
mlog(CRITICAL, "Failed to post response for %s: %d", parser->url, post_status);
parser->err_cnt++;
}
}

// reset body
parser->rec_index = 0;
parser->rec_size = 0;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/netsvc/CurlLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class CurlLib
uint32_t hdr_index;
uint32_t rec_size;
uint32_t rec_index;
uint32_t err_cnt;
uint8_t* rec_buf;
Publisher* outq;
const char* url;
Expand Down

0 comments on commit 5338ef6

Please sign in to comment.