task.delay/defer resume with unexpected arguments

Reproduction Steps
Reproduction script:

print("TEST 1")
local thread = coroutine.running()
task.delay(1, thread, "foo")
print("RESULT", coroutine.yield())

wait(2)

print("TEST 2")
local thread = coroutine.create(function(...)
	print("ONE", ...)
	print("TWO", coroutine.yield())
	print("THREE", coroutine.yield())
	print("FOUR", coroutine.yield())
end)
task.delay(1, thread, "one")
task.delay(1, thread, "two")
task.delay(1, thread, "three")
task.delay(1, thread, "four")

wait(2)

print("TEST 3")
local thread = coroutine.create(function(...)
	print("ONE", ...)
	print("TWO", coroutine.yield())
	print("THREE", coroutine.yield())
	print("FOUR", coroutine.yield())
end)
task.spawn(thread, "one")
task.delay(1, thread, "two")
task.delay(1, thread, "three")
task.delay(1, thread, "four")

wait(2)

print("DONE")

Expected Behavior
That the following output is printed:

TEST 1
RESULT foo
TEST 2
ONE one
TWO two
THREE three
FOUR four
TEST 3
ONE one
TWO two
THREE three
FOUR four
DONE

Actual Behavior
The following output is printed:

TEST 1
RESULT function: 0x725279fe6c969d97
TEST 2
attempt to call a string value
cannot resume non-suspended coroutine
cannot resume non-suspended coroutine
cannot resume non-suspended coroutine
TEST 3
ONE one
TWO four
THREE function: 0x725279fe6c969d97
FOUR function: 0x725279fe6c969d97
DONE

defer/delay seems to be mishandling the arguments passed to the thread when resumed.

  • In the first test, an unknown function is unexpectedly being passed to the thread.
  • In the second test, the argument passed to the last call is apparently being called as a function.
  • In the third test, it is demonstrated that arguments are being overridden by newer calls that involve the same thread. Whether or not this behavior is intentional, users will be expecting that arguments are associated with the call and not the thread.

Issue Area: Engine
Issue Type: Other
Impact: High
Frequency: Constantly
Date First Experienced: 2021-08-10 00:08:00 (+00:00)
Date Last Experienced: 2021-08-10 00:08:00 (+00:00)

4 Likes

Thanks for the report! We’ve filed a ticket to our internal database and we’ll follow up when we have an update for you.

2 Likes

Just bumping to indicate that this is still not fixed. The behavior appears to be similar to recent problems with the select function, which implies a mishandling of the Lua stack, so I feel it’s rather important.

1 Like