Pcall/ypcall don't work with coroutine resume

This happens all the time in studio and in game

print'-'
pcall(function()
	local t=coroutine.running()
	delay(1,function()
		coroutine.resume(t)
	end)
	print'yielding'
	coroutine.yield()
	print'resumed'
end)
print'a'--never outputted, but should be

Replacing pcall with ypcall doesn’t work either
But the code runs with no problems if coroutines are replaced with wait() or BindableEvents

I think this problem is similar to a lot of other ones with coroutines also

1 Like

The only solution I can think of is:

local pcall=function(f,...)
	local t=coroutine.running()
	local ret
	local wk=setmetatable({},{__mode='v'})
	coroutine.wrap(function(...)
		local ret2=table.pack(pcall(function(...)
				local w=newproxy()
				wk[1]=w
				ret=table.pack(true,f(...))
				coroutine.resume(t)
			end,...))
		if not ret then
			ret=ret2
			coroutine.resume(t)
		end
	end)(...)
	_G'gc'(wk[1],function()
		if not ret then
			ret={false,'unknown bc pcall bug'}
			coroutine.resume(t)
		end
	end)
	if not ret then coroutine.yield()end
	return unpack(ret,1,ret.n)
end

but it unfortunately has the side effect that stack traces and the error messages are unknown when the function does coroutine yield, and the gc method which is https://devforum.roblox.com/t/--gc-mind-blowing/11312 means unnecessary yielding if the function coroutine yields

Yay that this one is fixed but there are still these 3 coroutine bugs:

1 Like

I wanted to note that delay() also doesnt work pcall

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.