lgi-devs / lgi-devs/lgi

Error when using UDP socket (libffi7+ specific?)

Open
#265 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lua
Stars
491
Forks
81
PR merge metrics
No merged PRs in 30d

Description

This is a weird error that seems to occur only with libffi version 7/8, but not version 6 (or could it be another library?). Here's an example that can reproduce it:

local lgi = require 'lgi'
local	GLib,		Gdk,		GObject,		Gio =
		lgi.GLib,	lgi.Gdk,	lgi.GObject,	lgi.Gio

local app = Gio.Application { application_id = 'org.v1993.udpcrash', flags = 'NON_UNIQUE' }

local socket = lgi.Gio.Socket.new('IPV4', 'DATAGRAM', 'UDP')
socket.blocking = false
local sa = lgi.Gio.InetSocketAddress.new(Gio.InetAddress.new_loopback('IPV4'), 3030)
assert(socket:bind(sa, true))

do
	-- To avoid extra allocations
	-- I assume this is long enough
	local buf = require("lgi.core").bytes.new(4096)
	local source = socket:create_source('IN')
	source:set_callback(function()
		print('This will print')
		local len, src = socket:receive_from(buf) -- CRASH!
		print('And this will not')
		if len > 0 then
			print(tostring(buf):sub(1, len))
		end
		return true
	end)

	source:attach(lgi.GLib.MainContext.default())
end

local function exitNormal()
	print('Exiting')
	app:release()
end

function app:on_activate()
	app:hold()
end

GLib.unix_signal_add(GLib.PRIORITY_HIGH, 1, exitNormal)
GLib.unix_signal_add(GLib.PRIORITY_HIGH, 2, exitNormal)
GLib.unix_signal_add(GLib.PRIORITY_HIGH, 15, exitNormal)

app:run({arg[0], ...})

Now, start this simple script and run in another window:

echo 'Hi' > /dev/udp/localhost/3030

This will cause script to crash with following output:

This will print
**
Lgi:ERROR:marshal.c:1197:lgi_marshal_2c_caller_alloc: assertion failed: (size > 0)
Bail out! Lgi:ERROR:marshal.c:1197:lgi_marshal_2c_caller_alloc: assertion failed: (size > 0)
Aborted (core dumped)

This actually causes a crash in my application: https://github.com/v1993/linuxmotehook/issues/5 (thread contains some limited digging into issue), which evidently worked with older versions of stuff. Any clue what's causing it?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Lua example with the UDP send command, then inspect marshal.c at lgi_marshal_2c_caller_alloc, the assertion site named in the crash. Compare the receive_from call and the libffi 7/8 behavior described in the issue and linked application report. Done means the example receives the datagram without aborting and preserves the existing output behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.