When WirePlumber exits, it crashes with SIGABRT. The backtrace shows:
7 g_closure_invalidate
10 g_object_unref
15 g_value_unset
16 _wplua_gvalue_userdata___gc
26 close_state (lua_close)
27 wp_plugin_deactivate
33 wp_registry_clear
34 wp_core_dispose
Root cause: lua_State is referenced by both the main plugin and each
WpLuaScript object. During registry cleanup, scripts call lua_close again
on an already-destroyed state, causing use-after-free.
Fix:
- Add a prepare_shutdown vfunc in WpPlugin. Call it from wp_core_dispose
*before* clearing the registry.
- Lua plugin implements prepare_shutdown to close its lua_State early,
allowing all __gc finalizers to run while GObjects are still alive.
- Then clear the lua_State pointer from every WpLuaScript object via
wp_lua_script_clear_lua_state().
- Guard WpLuaScript finalize to skip if pointer is already NULL.
The intention is to make checks for enabled log topics faster.
Every topic has its own structure that is statically defined in the file
where the logs are printed from. The structure is initialized transparently
when it is first used and it contains all the log level flags for the levels
that this topic should print messages. It is then checked on the wp_log()
macro before printing the message.
Topics from SPA/PipeWire are also handled natively, so messages are printed
directly without checking if the topic is enabled, since the PipeWire and SPA
macros do the checking themselves.
Messages coming from GLib are checked inside the handler.
An internal WpLogFields object is used to manage the state of each log
message, populating all the fields appropriately from the place they
are coming from (wp_log, spa_log, glib log), formatting the message and
then printing it. For printing to the journald, we still use the glib
message handler, converting all the needed fields to GLogField on demand.
That message handler does not do any checks for the topic or the level, so
we can just call it to send the message.
This change completely refactors the way components are loaded in wireplumber:
- The module_init() function must return a GObject now. This object is either
a WpPlugin or a WpSiFactory in the current modules.
- When the component loader initializes a module, it automatically registers
the WpPlugin or WpSiFactory with their respective methods. There is no need
to register the WpPlugin or WpSiFactory in the module now.
- The wp_core_load_component() API has been refactored to be asynchronows. This
allows the component loader to automatically activate WpPlugin objects, and
therefore allows the application to directly get the WpPlugin without having
to find it. This simplifies a lot of things.
- The 'ifexists' and 'nofail' component flags now work even if the respective
WpPlugin could not be activated.
- The code that loads components in main.c has also been simplified a lot,
and the option to load dangling components has also been removed.
This allows scripts to declare when they have finished their loading,
so we can now also know when wireplumber is done loading and ready to
handle clients
Related to !313