lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, Jun 24, 2020 at 3:45 AM Gé Weijers wrote:
> - you have to explicitly call coroutine.close(thread) to do the
> variable cleanup if the coroutine is suspended (has yielded).

If the variable is in a sub-scope of the coroutine that does not
contain any yield, it will be closed at the end of scope. If there is
a yield, the close will be postponed until the next resume or
coroutine.close.  (example in [1]). It seems "Right" to me, in the
sense of the "Last surprise principle".

On Wed, Jun 24, 2020 at 3:45 AM Gé Weijers wrote:
> - coroutine.close does not work on threads that are not suspended or
> dead,  i.e. threads that are waiting for coroutine.resume to return.

I am not sure how to test this in pure lua. Can you make a short example?

On Wed, Jun 24, 2020 at 3:45 AM Gé Weijers wrote:
> - if you call os.exit(<code>, true) from a coroutine no __close
> metamethods will be called in any thread, not even the main one.

Actually it seems from a quick test [1] that the ones of the main
thread are closed. This is consistent with the manual, tought I agree
that a way to exit closing ALL the variables from ALL the threads
could be useful.

Well... at least it is consistent assuming that "Closing the Lua
state" means also "Closing the <close> variables". I agree with
Lorenzo that this should be explicitly stated. Maybe it is worth  also
to specify that the "Lua state" it refers to is just the "Main"
thread.

---

[1] The following code

```
local x = coroutine.wrap(function()
  do
    local A <close> = setmetatable({},{
      __close = function() print("A closed") end,
    })
  end
  local B <close> = setmetatable({},{
    __close = function() print("B closed") end,
  })
  coroutine.yield()
  os.exit(0, true)
  coroutine.yield()
end)

local C <close> = setmetatable({},{
  __close = function() print("C closed") end,
})

print("Call 1")
x()
print("Call 2")
-- os.exit(0, true)
x()
print("Call 3")
```

prints:

```
Call 1
A closed
Call 2
C closed
```