lua-users home
lua-l archive

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


On Fri, Jul 31, 2020 at 1:13 PM Mimmo Mane wrote:
> So, the suggestion is: All coroutines in the VM should be implicitly closed on VM exit.

coroutine.wrap closes them automatically [4]

[4] The code
```
local thread = coroutine.wrap(function()
  local please <close> = setmetatable({}, {__close = function()
    print"coroutine toclose"
  end})
  error"an error in the goroutine"
  print"coroutine ends"
end)
thread()
print"program exit"
```
prints "coroutine toclose" before quitting with an error.

Yes, if an error is raised, the coroutine is correctly closed.
But not every code raises an error  :-)


local thread = coroutine.wrap(function()
   local please <close> = setmetatable({}, {__close = function()
      print"coroutine toclose"
   end})
   print"coroutine yields"
   coroutine.yield()
   print"coroutine ends"
end)
thread()
print"program exit"

I don't see "coroutine toclose" printed.
The Lua manual says we should invoke coroutine.close().
But we can't  :-)