Lua 5.3.5 'xpcall' message handler is ignored by 'collectgarbage()' function

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Lua 5.3.5 'xpcall' message handler is ignored by 'collectgarbage()' function

Guenther Kramer

Hi,

 

I am trying to generate a traceback when garbage collection raises an exception.  This of course can happen if the ‘__gc’ metamethod raises an exception.  The problem I am encountering is that the message handler is ignored:

 

C:\Tools\lua-5.3.5_Win64_bin>lua53

Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio

> t = setmetatable({}, {__gc = function(t) junk() end}) t=nil

> print(xpcall(collectgarbage, debug.traceback))

false   error in __gc metamethod (stdin:1: attempt to call a nil value (global 'junk'))

 

I would of expected a traceback, but it turns out that the message handler is not called when the exception occurs.

In our system, we have many different threads so it makes it difficult to pin down the object that is causing the garbage collection exception.  Am I doing something wrong or is this a limitation?

 

Guenther

 

v
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.3.5 'xpcall' message handler is ignored by 'collectgarbage()' function

v
On Sat, 2019-09-07 at 23:53 +0000, Guenther Kramer wrote:
> I would of expected a traceback, but it turns out that the message
> handler is not called when the exception occurs.
> In our system, we have many different threads so it makes it
> difficult to pin down the object that is causing the garbage
> collection exception.  Am I doing something wrong or is this a
> limitation?
>  
> Guenther
>  

I think the problem is that GC handler don't really have any meaningful
traceback. Lua 5.4 use warnings in destructors for exactly this reason
(so your program will not error in strange place).

By the way, there's another one case where xpcall handler is not
triggered: out of memory situation.

--
v <[hidden email]>


Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.3.5 'xpcall' message handler is ignored by 'collectgarbage()' function

Andrew Gierth
In reply to this post by Guenther Kramer
>>>>> "Guenther" == Guenther Kramer <[hidden email]> writes:

 Guenther> Hi,
 Guenther> I am trying to generate a traceback when garbage collection
 Guenther> raises an exception. This of course can happen if the '__gc'
 Guenther> metamethod raises an exception. The problem I am encountering
 Guenther> is that the message handler is ignored:

Quoth the documentation:

"(The message handler is called only for regular runtime errors. It is
not called for memory-allocation errors nor for errors while running
finalizers.)"

What you'd need to do is to wrap each __gc method in its own xpcall.

--
Andrew.