Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't yield from an orphan coroutine #3423

Closed
Fingercomp opened this issue Apr 14, 2021 · 3 comments
Closed

Can't yield from an orphan coroutine #3423

Fingercomp opened this issue Apr 14, 2021 · 3 comments
Labels
bug OC feature is broken

Comments

@Fingercomp
Copy link
Contributor

I'll start with a repro:

local thread = require("thread")

local co = coroutine.create(function()
  coroutine.yield(1)
end)

local function threadBody()
  -- to allow the parent process to exit
  os.sleep(1)

  print(coroutine.resume(co))
end

thread.create(threadBody):detach()

The coroutine is created in a process (I'll refer to it as the Process), and I think /boot/01_process.lua registers it in the instances table of the Process. The rest of the code just makes it so that I resume the coroutine after the Process dies a peaceful death. When it does, its list of instances goes poof. So when I finally do a coroutine.resume, the coroutine isn't listed anywhere. And then assert(process.info(_coroutine.running()), "thread has no proc") in /boot/01_process.lua throws a tantrum because it can't find the associated process info.

The same thing happens if I try resuming an orphaned coroutine in an event listener, etc. (My workaround in the meanwhile is to create the coroutine inside a thread body and detach the thread.)

Since coroutine.resume has an exception for orphans, I believe the other coroutine. functions, like yield, should also have such an exception.

@payonel
Copy link
Member

payonel commented May 11, 2021

this isn't how to manage coroutines, this is not a bug
you have no code that calls coroutine.resume(co) a 2nd time.

  1. oc.resume in print is the first time it runs
  2. the, in the co function, you coroutine.yield
    There is no intention to change this behavior, this is exactly how coroutines are supposed to behave
    If you want "threads" that wake themselves back up, you want the thread library.

@payonel payonel closed this as completed May 11, 2021
@Fingercomp
Copy link
Contributor Author

With all due respect, I'm aware of how coroutines are supposed to work. And please note that I didn't complain how the coroutine is not resumed the second time. The code above doesn't resume it twice because it doesn't need that to reproduce the bug. Moreover, the code won't even be able to do so, as the coroutine dies because of the bug — and you cannot resume a dead coroutine.

The issue is about being unable to call coroutine.yield from within a coroutine that is run inside a detached thread.

Therefore I respectfully ask you to reconsider your decision to close this issue. The bug is still present.

@payonel
Copy link
Member

payonel commented May 11, 2021

AH, this is very different than what I thought you were saying, sorry @Fingercomp
you are correct, this is a bug

@payonel payonel reopened this May 11, 2021
@payonel payonel added the bug OC feature is broken label May 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug OC feature is broken
Projects
None yet
Development

No branches or pull requests

2 participants