[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: RFE: refman - adding mention of os.exit not closing variables with default arguments.
- From: Roberto Ierusalimschy <roberto@...>
- Date: Wed, 24 Jun 2020 11:25:14 -0300
> Another trap you can fall into is to put a local ... <close> variable
> inside a coroutine. There are a number of problems with that:
> - you have to explicitly call coroutine.close(thread) to do the
> variable cleanup if the coroutine is suspended (has yielded).
> - coroutine.close does not work on threads that are not suspended or
> dead, i.e. threads that are waiting for coroutine.resume to return.
> - if you call os.exit(<code>, true) from a coroutine no __close
> metamethods will be called in any thread, not even the main one.
> The behavior of os.exit is in my opinion a bit inconsistent, it
> unwinds the stack and closes to-be-closed variables in the main
> thread, but only if you call it from that main thread, and it does not
> do anything to other coroutines.
Having finalizers to back up __close solves all these cases, where an
object goes out of existence without going out of scope. The manual
hints about this, but it could be more emphatic.
The goal of <close> inside coroutines + coroutine.close is to allow
orderly kills of threads/coroutines in Lua.