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

Advice for running of LuaJIT version < 2.0.1 #1

Open
GiuseppeIII opened this issue Mar 2, 2022 · 7 comments
Open

Advice for running of LuaJIT version < 2.0.1 #1

GiuseppeIII opened this issue Mar 2, 2022 · 7 comments

Comments

@GiuseppeIII
Copy link

Hello, I am trying to run this code on LuaJIT version 2.0.0-beta 10. I am having toruble as parameterized types were only added to LuaJIT in version 2.0.1. They are used here: local cbuf = ffi.new(ffi.typeof('$[1]', ffi.typeof(func_ct))) and here: local overlapped_ptr_ct = ffi.typeof('$ *', overlapped_ct). I was wodnering if you knew any way of getting around using parameterized types here? Thanks!

@GiuseppeIII
Copy link
Author

GiuseppeIII commented Mar 3, 2022

For those interested I think (?) these two issues can be resolved by using

local cbuf
		if func_ct == 'LPFN_CONNECTEX' then
			cbuf = ffi.new(ffi.typeof('LPFN_CONNECTEX'..'[1]'))
		elseif func_ct == 'LPFN_ACCEPTEX' then
			cbuf = ffi.new(ffi.typeof('LPFN_ACCEPTEX'..'[1]'))
		end

and

local overlapped_ptr_ct = ffi.typeof([[struct {
		OVERLAPPED overlapped;
		int job_index;
	}
]].."*")

respectively.

Still getting an error table index is nil at waiting[thread] = true

do
local function pass(thread, ...)
	wait_count = wait_count - 1
	waiting[thread] = nil
	return ...
end
--[[local]] function wait(register)
	local thread, is_main = currentthread()
	assert(not is_main, 'trying to perform I/O from the main thread')
	wait_count = wait_count + 1
	if register ~= false then
		waiting[thread] = true
	end
	return pass(thread, restore(transfer(poll_thread)))
end
end

but think that might be to using lua 5.1 (instead of 5.2) (and due to this using the May 7th build of coro, where the main thread is nil) and not due to the older version of LuaJIT...

@GiuseppeIII
Copy link
Author

GiuseppeIII commented Mar 3, 2022

For now will try using the May 22nd version of sock with the above changes and additionally changing local function

local function make_async(for_writing, f, wait_errno)
	return function(self, expires, ...)
		::again::
		local ret = f(self, ...)
		if ret == 0 then return nil, 'closed' end
		if ret > 0 then return ret end
		if ffi.errno() == wait_errno then
			if for_writing then
				self.send_expires = expires
				if expires then
					send_expires_heap:push(self)
				end
				self.send_thread = coro.running()
			else
				self.recv_expires = expires
				if expires then
					recv_expires_heap:push(self)
				end
				self.recv_thread = coro.running()
			end
			local ok, err = wait()
			if not ok then
				if err == 'timeout' then
					self:close()
				end
				return nil, err
			end
			goto again
		end
		return check()
	end
end

with

local function make_async(for_writing, f, wait_errno)
	return function(self, expires, ...)
		local test = true
		while(test) do
		-- ::again::
			local ret = f(self, ...)
			if ret == 0 then return nil, 'closed' end
			if ret > 0 then return ret end
			if ffi.errno() == wait_errno then
				if for_writing then
					self.send_expires = expires
					if expires then
						send_expires_heap:push(self)
					end
					self.send_thread = coro.running()
				else
					self.recv_expires = expires
					if expires then
						recv_expires_heap:push(self)
					end
					self.recv_thread = coro.running()
				end
				local ok, err = wait()
				if not ok then
					if err == 'timeout' then
						self:close()
					end
					return nil, err
				end
				-- goto again
			else
				test = false
			end
		end
		return check()
	end
end

and

function socket:addr(host, port, flags)
	return M.addr(host, port, self._st, self._af, self._pr, addr_flags)
end

to

function socket:addr(host, port, addr_flags)
	return M.addr(host, port, self._st, self._af, self._pr, addr_flags)
end

to maintain compatibility with Lua 5.1. Hopefully this doesn't cause any issues with the mysql module.

@GiuseppeIII
Copy link
Author

Hmm... with this still getting error 'trying to I/O from the main thread' from line 1528 assert(thread ~= poll_thread, 'trying to I/O from the main thread')

@capr
Copy link
Member

capr commented Mar 4, 2022

You showed my how you modified the code but you forgot to show the code that actually tries to connect to mysql. That code should be wrapped in sock.run() or sock.thread(). I suggest you look at tests/demos and make those work first.

@GiuseppeIII
Copy link
Author

GiuseppeIII commented Mar 5, 2022

Ah that makes sense, sometimes you overlook the most simple things, thanks for the help. Sadly I am still getting errors, but later on now:

[string "--Portable socket API with IOCP, epoll and kq..."]:1917: [string "--symmetric coroutines from the paper at..."]:45: 
[string "--Portable socket API with IOCP, epoll and kq..."]:542: attempt to call field 'bor' (a nil value)
[thread] stack traceback:
	[string "--Portable socket API with IOCP, epoll and kq..."]:542: in function 'require'
	[string "--MySQL client protocol in Lua...."]:1029: in function 'connect'
	[string "local mysql = require'mysql'..."]:6: in function 'f'
	[string "--Portable socket API with IOCP, epoll and kq..."]:1974: in function <[string "--Portable socket API with IOCP, epoll and kq..."]:1973>
	[C]: in function 'pcall'
	[string "--Portable socket API with IOCP, epoll and kq..."]:1891: in function 'f'
	[string "--symmetric coroutines from the paper at..."]:45: in function <[string "--symmetric coroutines from the paper at..."]:44>
[thread] stack traceback:
	[C]: in function 'error'
	[string "--Portable socket API with IOCP, epoll and kq..."]:1901: in function 'f'
	[string "--symmetric coroutines from the paper at..."]:45: in function <[string "--symmetric coroutines from the paper at..."]:44>
stack traceback:
  [1] [C]: in function error
  [2] [string "--symmetric coroutines from the paper at..."]:28: in function transfer
  [3] [string "--Portable socket API with IOCP, epoll and kq..."]:1917: in function transfer
  [4] [string "--Portable socket API with IOCP, epoll and kq..."]:1933: in function thread
  [5] [string "--Portable socket API with IOCP, epoll and kq..."]:1976: in function run
  [6] [string "local mysql = require'mysql'..."]:4: in function require
  [7] [string "require("scripts/settings/menu/menu_controlle..."]:3: in function require
  [8] scripts/boot/boot_common.lua:117: in function boot_game_require
  [9] scripts/boot/boot_common.lua:142: in function game_require_callback
  [10] foundation/scripts/boot/foundation_setup.lua:30: in function setup
  [11] scripts/boot/boot_common.lua:32: in function init_common
  [12] scripts/boot/boot.lua:62:in function <scripts/boot/boot.lua:55>

local_variables:
  [2] thread = [thread], ok = false, s = [string "--symmetric coroutines from the paper at..."]:45: [string "--Portable socket API with IOCP, epoll and kq..."]:542: attempt to call field 'bor' (a nil value) [thread] stack traceback: [string "--Portable socket API with IOCP, epoll and kq..."]:542: in function 'require' [string "--MySQL client protocol in Lua...."]:1029: in function 'connect' [string "local mysql = require'mysql'..."]:6: in function 'f' [string "--Portable socket API with IOCP, epoll and kq..."]:1974: in function <[string "--Portable socket API with IOCP, epoll and kq..."]:1973> [C]: in function 'pcall' [string "--Portable socket API with IOCP, epoll and kq..."]:1891: in function 'f' [string "--symmetric coroutines from the paper at..."]:45: in function <[string "--symmetric coroutines from the paper at..."]:44> [thread] stack traceback: [C]: in function 'error' [string "--Portable socket API with IOCP, epoll and kq..."]:1901: in function 'f' [string "--symmetric coroutines from the paper at..."]:45: in function <[string "--symmetric coroutines from the paper at..."]:44>
  [3] thread = [thread]
  [4] thread = [thread], real_poll_thread = nil
  [5] f = [function], ret = nil, wrapper = [function]
  [6] mysql = [table], sock = [table]
  [8] path = game_state, _ = 1, s = game_state_menu, game_file = scripts/game_state/game_state_menu
  [9] package_manager = [table], boot_game_require = [function], force_flush = true, script_package = resource_packages/script_files_game, base_game_resources = resource_packages/base_game_resources, cursors = resource_packages/cursors, language_resources = resource_packages/language
  [10] self = [table], game_require_callback = [function]
  [11] use_strict = true
  [12] res = [table]

Wasn't able to fully decrypt this. Do you have any idea what could be going on here?

@capr
Copy link
Member

capr commented Mar 6, 2022

attempt to call field 'bor' is pretty clear to me

@GiuseppeIII
Copy link
Author

GiuseppeIII commented Mar 6, 2022

Yeah that was a part of why I was confused, I probably I should have explained more. the bit.bor field definetly works. It also never actually hits that part of the code when running, it seems the error function (?) is trying to post-hoc figure out what could be wrong (?). If I just comment out the line local SIO_GET_EXTENSION_FUNCTION_POINTER = bit.bor(IOC_IN, IOC_OUT, IOC_WS2, 6), it jumps to 1513: attempt to call field 'bnot' (a nil value) and then commenting on that line it goes to 1516: attempt to call field 'bnot' (a nil value) and commenting out that line it goes to 557: assertion failed!. The real issue seems to be that that at the coro function local function unprotect(thread, ok, ...) ok is false causing it to hit

local s = debug.traceback(thread, (...))
s = s:gsub('stack traceback:', tostring(thread)..' stack traceback:')
error(s, 2)

If I force an error (attempt to compare string with number) there instead of going through the normal error routine, I get this:

[string "--symmetric coroutines from the paper at..."]:26: attempt to compare string with number
stack traceback:
  [1] [string "--symmetric coroutines from the paper at..."]:26: in function transfer
  [2] [string "--Portable socket API with IOCP, epoll and kq..."]:1917: in function transfer
  [3] [string "--Portable socket API with IOCP, epoll and kq..."]:1933: in function thread
  [4] [string "--Portable socket API with IOCP, epoll and kq..."]:1976: in function run
  [5] [string "local mysql = require'mysql'..."]:4: in function require
  [6] [string "require("scripts/settings/menu/menu_controlle..."]:3: in function require
  [7] scripts/boot/boot_common.lua:117: in function boot_game_require
  [8] scripts/boot/boot_common.lua:142: in function game_require_callback
  [9] foundation/scripts/boot/foundation_setup.lua:30: in function setup
  [10] scripts/boot/boot_common.lua:32: in function init_common
  [11] scripts/boot/boot.lua:62:in function <scripts/boot/boot.lua:55>

local_variables:
  [1] thread = [thread], ok = false
  [2] thread = [thread]
  [3] thread = [thread], real_poll_thread = nil
  [4] f = [function], ret = nil, wrapper = [function]
  [5] mysql = [table], sock = [table]
  [7] path = game_state, _ = 1, s = game_state_menu, game_file = scripts/game_state/game_state_menu
  [8] package_manager = [table], boot_game_require = [function], force_flush = true, script_package = resource_packages/script_files_game, base_game_resources = resource_packages/base_game_resources, cursors = resource_packages/cursors, language_resources = resource_packages/language
  [9] self = [table], game_require_callback = [function]
  [10] use_strict = true
  [11] res = [table]

(Also if useful this is the initial code I am using to test:

local mysql = require'mysql'
local sock = require'sock'

sock.run(function()

	local conn = assert(mysql.connect{
		host = '127.0.0.1',
		port = 13306,
		user = 'root',
		db = 'tser',
		charset = 'utf8mb4',
		collation = 'utf8mb4_unicode_ci'
	})

	conn.close()
end)

I also get the same error using sock.thread or sock.resume(sock.newthread instead of sock.run.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants