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

Reap zombie processes left behind by openURL #1928

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/modules/system/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,43 @@
#endif
#endif

#if defined(LOVE_LINUX)
#include <unordered_set>

static std::unordered_set<pid_t>& getSpawnedPids()
{
static std::unordered_set<pid_t> spawnedPids;
return spawnedPids;
}

static void sigchldHandler(int signo, siginfo_t* info, void* /*context*/)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm very unfamiliar with these APIs so this might be a silly question: is it possible for this callback to be called on other threads, or is it guaranteed to only be called on the main thread?

If it can be called on other threads there'll need to be some extra thread safety (maybe a mutex) when modifying spawnedPids.

{
// We need to be selective when reaping child processes, because simply calling
// `waitpid` on every child process that terminated will break any other calls
// to `waitpid`.
// Particularly `os.execute` (internally: `system`) will always return -1, because
// their call to `waitpid` returned `ECHILD`, since we already reaped the process.
// This was reported in #1047 and fixed with a similar approach in 30ff7d4 and f7d226d
// and then later removed in a4a62f3.
if (getSpawnedPids().erase(info->si_pid) > 0)
::waitpid(info->si_pid, nullptr, 0);
}
#endif

namespace love
{
namespace system
{

System::System()
{
#if defined(LOVE_LINUX)
struct sigaction sa;
sa.sa_sigaction = sigchldHandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART | SA_SIGINFO;
sigaction(SIGCHLD, &sa, nullptr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this get cleaned up / reset when the System class is destroyed? e.g. what happens when love.event.quit("restart") is called (which keeps the process running but starts new instances of love's modules)?

#endif
}

std::string System::getOS() const
Expand Down Expand Up @@ -131,6 +161,8 @@ bool System::openURL(const std::string &url) const
}
#endif

getSpawnedPids().insert(pid);

// Check if xdg-open already completed (or failed.)
int status = 0;
if (waitpid(pid, &status, WNOHANG) > 0)
Expand Down