garrysmod-issues icon indicating copy to clipboard operation
garrysmod-issues copied to clipboard

newproxy fails under strange circumstances

Open jorjic opened this issue 3 years ago • 4 comments
trafficstars

Details

newproxy breaks when used inside a coroutine, returning the given argument instead of creating a new userdata object.

] version
Protocol version 24
Exe version 2022.06.08 (garrysmod)
Exe build: 18:31:37 Jun  1 2022 (8607) (4000)
GMod version 2022.06.10, branch: unknown, multicore: 0
Windows 32bit

Steps to reproduce

-- expected behavior: creates a new userdata
print( newproxy( true ) )  -- userdata: 0x437b7338

-- unexpected behavior
local co = coroutine.create( function()
	print( newproxy( true ) )  -- true
	print( newproxy( false ) )  -- false
	print( newproxy( nil ) )  -- nil
 	print( newproxy( "string" ) )  -- no return value
end )
coroutine.resume( co )

jorjic avatar Aug 04 '22 03:08 jorjic

Oh, someone has already posted about this, and it was me.

This time it was really frustrating to troubleshoot because the consequential call stack was somewhat corrupted. It seems like frame switching is maybe not quite being handled correctly.

This is definitely not exclusive to coroutines. Calling print( coroutine.running() ) just before the offending line confirms that coroutines are not involved. Unfortunately, I'm not able to provide a simple proof of concept this time.

However, I lucked into a very strange workaround that causes the bug to not occur.

-- expected behavior: creates a new userdata
print( newproxy( true ) )  -- userdata: 0x437b7338

-- unexpected behavior
local co = coroutine.create( function()
	-- fails as usual inside coroutines
	print( newproxy( true ) )  -- true
	
	-- more magic
	type()
	
	-- creates a userdata correctly!
	print( newproxy( true ) )  -- userdata: 0xc1d8adea
end )
coroutine.resume( co )

It has to be type. I have not been able to find any other functions that work.

Of course, this is nothing that can be fixed at the low-level without forking Lua. The function is undocumented in 5.1 and removed in later versions of Lua.

jorjic avatar Aug 31 '24 00:08 jorjic

Creating userdata from lua has always been questionable in real use but this issue should be documented on the wiki.

garryspins avatar Aug 31 '24 00:08 garryspins

I just updated my previous comment to document a puzzling work-around.

@garryspins Would you kindly tag this issue on the wiki?

jorjic avatar Aug 31 '24 01:08 jorjic

done :3

garryspins avatar Aug 31 '24 04:08 garryspins