From 494be307649f20aaed53113f3c1ff740980a5992 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Wed, 1 May 2024 01:17:10 +0200 Subject: [PATCH] js/environment: Add GLib spawn async overrides warning when using child setup Using child setup functions is unsafe in gjs code so let's warn if this happens so that extensions using a similar codepath are warned. This could be also part of gjs, but we provide a shell override since we have alternatives to log about. Part-of: --- js/ui/environment.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/js/ui/environment.js b/js/ui/environment.js index 4150542db..4ce70367e 100644 --- a/js/ui/environment.js +++ b/js/ui/environment.js @@ -345,6 +345,24 @@ if (slowdownEnv) { St.Settings.get().slow_down_factor = factor; } +function wrapSpawnFunction(func) { + const originalFunc = GLib[func]; + return function (workingDirectory, argv, envp, flags, childSetup, ...args) { + if (childSetup) { + logError(new Error(`Using child GLib.${func} with a GLib.SpawnChildSetupFunc ` + + 'is unsafe and may dead-lock, thus it should never be used from JavaScript. ' + + `Shell.${func} can be used to perform default actions or an ` + + 'async-signal-safe alternative should be used instead')); + } + + return originalFunc(workingDirectory, argv, envp, flags, childSetup, ...args); + }; +} +GLib.spawn_async = wrapSpawnFunction('spawn_async'); +GLib.spawn_async_with_pipes = wrapSpawnFunction('spawn_async_with_pipes'); +GLib.spawn_async_with_fds = wrapSpawnFunction('spawn_async_with_fds'); +GLib.spawn_async_with_pipes_and_fds = wrapSpawnFunction('spawn_async_with_pipes_and_fds'); + // OK, now things are initialized enough that we can import shell JS const Format = imports.format;