Coroutine.status(thread) continues to return "suspended" if a value is returned in the function

I want to know if this is an intended feature or is it a bug.

For some reason if you return any value in the body function of task.spawn / task.defer / coroutine.spawn after a task.wait(), then printing the status of that thread using coroutine.status will always return “suspended”.

This will continue to print “suspended”

local thread = coroutine.create(function()
	task.wait() -- this is needed to replicate this
	return nil -- (any value) end of function, thread should be dead but no
end)
coroutine.resume(thread)

while task.wait(1) do
	print(coroutine.status(thread)) -- prints suspended nonetheless
end

This will print “dead”

local thread = coroutine.create(function()
	task.wait()
	return
end)
coroutine.resume(thread)

while task.wait(1) do
	print(coroutine.status(thread)) -- prints dead
end

Well, seems that anything that yields (as this task.wait() or RunService.Heartbeat:Wait() or NumberValue.Changed:Wait(), add more samples here) creates own coroutine.
and when you call coroutine.resume you get result of their coroutine.yield.

I did a little digging and it turns out Roblox treats a coroutine as dead when it returns absolutely nothing. A nil value is still a value. It’s a value to represent nothing, but it’s not nothing itself.

As long as something is returned, the coroutine is interpreted as one that can still be resumed.

The issue is, even if you resume the coroutine after a value is returned, the status of the coroutine will get updated to “dead” but the rest of the codes in the body function will not be executed. Technically speaking, the coroutine should’ve been dead as soon as the entire body function has no uses anymore, no matter the values returned. It could just be a bug or design flaw in roblox’s backend, but still annoying.

This currently affects an automated task module I’ve created, causing certain tasks to infinitely yield, although they can be fixed easily by manually resuming the task or canceling it or just modify the body function.

Currently, I just want to hear other developers’ insights first to see if this is worth submitting a bug report for.

I do agree with you; it doesn’t make sense for the coroutine to be suspended if it has already returned, since the code is guaranteed to be halted.

The only reasonable explanation I can think of is that Roblox marks a coroutine as “dead” only when absolutely nothing (not even nil) is “returned”. This probably stems from the fact that coroutine.yield() is sort of like a return statement, except with the difference that it doesn’t guarantee the end of possible execution. My guess is that return is similar to coroutine.yield() in this regard.

This might be a bug, or this might be intentional, but it is odd behavior nontheless.