lua-users home
lua-l archive

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


04.07.2020 um 11:31 schrieb Francisco Olarte:
> Stefan , although it may work some times I do not think, given how
> easy it is to avoid it, resuming the main thread is a good idea. A BAD
> program does not need to malfunction in every situation, just in one.
> 
> In asymmetric coroutines y normally assume a coroutine can be resumed
> when it is suspended, and it is suspended when it is just created but
> not started or has called yield, neither of which is true in main, I
> see it as started from creation and not being able to yield because it
> has no one to yield to.
> 
> Given lua uses the same struct in the C API it may well be posible to
> call the functions and obtain some aparently correct bu tsubtly
> corrupted results. You can use them, but IMO you are inflicting
> problems in yourself.
> 
> Your exhaustive exploration of ( what I personally consider bizarre )
> ways of (ab)using the API is good, but I am not personally going to
> analyze it too much. Just seeing the difference in behaviours between
> versions makes me worry you are hitting some kind of what in C is
> called "undefined behaviour" at its finest.
> 
> To me it seems whenever you call lua_resume the API assumes you are
> doing it on a resumable coroutine, and acts accordingly without
> checking too much, so sometimes it does the right thing, sometimes it
> does not. Paraphrasing AST, it is a servant, not a nanny. And nearly
> something for yield.
> 
> Francisco Olarte.
> 

I'm sorry if this came across negative. The goal of this post is *NOT*
to exploit the C API, it is to find out how to use it safely and avoid
the "C API undefined behaviour". When this tiny script:

if coroutine.isyieldable() then
  coroutine.yield()
end

crashes, clearly something went wrong on the C side.
So far my conclusions are:

* Never run the main thread with anything other than lua_call or
  lua_pcall
* Never lua_yield anything other than the currently running coroutine

I.e., the answer to "Is it possible to yield/resume the main thread?" is
no.