Skip to content

Commit

Permalink
Use sigchld to clean up zombie process. (#1062)
Browse files Browse the repository at this point in the history
SIGKILL doesn't mean waitpid may immediate get any result.
  • Loading branch information
wengxt authored May 24, 2024
1 parent 7b29490 commit efb0bbc
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/lib/fcitx/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#include <cstdint>
#include <ctime>
#include <memory>
#include <stdexcept>
#include <unordered_map>
Expand All @@ -22,6 +24,7 @@
#include "fcitx-utils/eventdispatcher.h"
#include "fcitx-utils/i18n.h"
#include "fcitx-utils/log.h"
#include "fcitx-utils/macros.h"
#include "fcitx-utils/misc.h"
#include "fcitx-utils/standardpath.h"
#include "fcitx-utils/stringutils.h"
Expand Down Expand Up @@ -1361,6 +1364,9 @@ void Instance::handleSignal() {
exit();
} else if (signo == SIGUSR1) {
reloadConfig();
} else if (signo == SIGCHLD) {
d->zombieReaper_->setNextInterval(2000000);
d->zombieReaper_->setOneShot();
}
}
}
Expand Down Expand Up @@ -1411,6 +1417,15 @@ void Instance::initialize() {
}
return false;
});
d->zombieReaper_ = d->eventLoop_.addTimeEvent(
CLOCK_MONOTONIC, now(CLOCK_MONOTONIC), 0,
[](EventSourceTime *, uint64_t) {
pid_t res;
while ((res = waitpid(-1, nullptr, WNOHANG)) > 0) {
}
return false;
});
d->zombieReaper_->setEnabled(false);

d->exitEvent_ = d->eventLoop_.addExitEvent([this](EventSource *) {
FCITX_DEBUG() << "Running save...";
Expand Down
1 change: 1 addition & 0 deletions src/lib/fcitx/instance_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class InstancePrivate : public QPtrHolder<Instance> {
EventDispatcher eventDispatcher_;
std::unique_ptr<EventSourceIO> signalPipeEvent_;
std::unique_ptr<EventSourceTime> preloadInputMethodEvent_;
std::unique_ptr<EventSourceTime> zombieReaper_;
std::unique_ptr<EventSource> exitEvent_;
InputContextManager icManager_;
AddonManager addonManager_;
Expand Down
12 changes: 9 additions & 3 deletions src/server/errorhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <cstdint>
#include <string>

#include "fcitx-utils/fs.h"
#include "errorhandler.h"
Expand All @@ -31,12 +34,12 @@
extern int selfpipe[2];
extern std::string crashlog;

typedef struct _MinimalBuffer {
struct MinimalBuffer {
char buffer[MINIMAL_BUFFER_SIZE];
int offset;
} MinimalBuffer;
};

void SetMyExceptionHandler(void) {
void SetMyExceptionHandler() {
int signo;

for (signo = SIGHUP; signo < SIGUNUSED; signo++) {
Expand Down Expand Up @@ -111,6 +114,9 @@ static inline void _write_buffer(int fd, const MinimalBuffer *buffer) {

void OnException(int signo) {
if (signo == SIGCHLD) {
uint8_t sig = (signo & 0xff);
fcitx::fs::safeWrite(selfpipe[1], &sig, 1);
signal(signo, OnException);
return;
}

Expand Down
10 changes: 9 additions & 1 deletion src/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@

#include <locale.h>
#include <sys/stat.h>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include "fcitx-utils/fs.h"
#include "fcitx-utils/log.h"
#include "fcitx-utils/misc.h"
#include "fcitx-utils/misc_p.h"
#include "fcitx-utils/standardpath.h"
#include "fcitx-utils/stringutils.h"
#include "fcitx/addonfactory.h"
#include "fcitx/addonloader.h"
#include "fcitx/addonmanager.h"
#include "fcitx/instance.h"
#include "errorhandler.h"
Expand Down Expand Up @@ -78,7 +86,7 @@ int main(int argc, char *argv[]) {
canRestart = instance.canRestart();
} catch (const InstanceQuietQuit &) {
} catch (const std::exception &e) {
std::cerr << "Received exception: " << e.what() << std::endl;
std::cerr << "Received exception: " << e.what() << '\n';
return 1;
}

Expand Down
13 changes: 12 additions & 1 deletion src/ui/classic/plasmathemewatchdog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@
*
*/
#include "plasmathemewatchdog.h"
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <cassert>
#include <cerrno>
#include <csignal>
#include <cstdint>
#include <functional>
#include <stdexcept>
#include <string>
#include <utility>
#include <linux/prctl.h>
#include "fcitx-utils/event.h"
#include "fcitx-utils/misc_p.h"
#include "fcitx-utils/fs.h"
#include "fcitx-utils/standardpath.h"
#include "fcitx-utils/unixfd.h"
#include "common.h"

#if defined(__FreeBSD__)
Expand Down
3 changes: 3 additions & 0 deletions src/ui/classic/plasmathemewatchdog.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
#ifndef _FCITX5_UI_CLASSIC_PLASMATHEMEWATCHDOG_H_
#define _FCITX5_UI_CLASSIC_PLASMATHEMEWATCHDOG_H_

#include <sys/types.h>
#include <unistd.h>
#include <functional>
#include <memory>
#include "fcitx-utils/event.h"
#include "fcitx-utils/unixfd.h"

Expand Down

0 comments on commit efb0bbc

Please sign in to comment.