Motion-Project / motionplus

MotionPlus Home Page: https://motion-project.github.io/
GNU General Public License v3.0
319 stars 40 forks source link

Issue a SIG_IGN before forking exec_commands #78

Closed tonytw1 closed 1 year ago

tonytw1 commented 1 year ago

PR to document a work around which seems to have resolved a local issues with accumulating defunct sh processes (#77).

Please close immediately if this is not useful.

Seems to prevent defunct sh processes from accumulating by allowing the OS to clear down the wrapping sh processes as soon as the complete.

The OS was retaining these processes in case we were going to come back and read the return status. We are not interested so we can use SIG_IGN to tell the OS that it can release these processes immediately.

davedoesdev commented 1 year ago

@tonytw1 thanks. This also works since the shell runs the commands in the background:

diff --git a/src/util.cpp b/src/util.cpp
index 838cb20..69e2cdb 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -1281,6 +1281,7 @@ void util_exec_command(ctx_dev *cam, const char *command, char *filename, int fi
 {
     char stamp[PATH_MAX];
     timespec tmpts;
+    int pid;

     if (cam->current_image == NULL) {
         clock_gettime(CLOCK_REALTIME, &tmpts);
@@ -1289,7 +1290,8 @@ void util_exec_command(ctx_dev *cam, const char *command, char *filename, int fi
         mystrftime(cam, stamp, sizeof(stamp), command, &cam->current_image->imgts, filename, filetype);
     }

-    if (!fork()) {
+    pid = fork();
+    if (!pid) {
         /* Detach from parent */
         setsid();

@@ -1302,6 +1304,13 @@ void util_exec_command(ctx_dev *cam, const char *command, char *filename, int fi
         exit(1);
     }

+    if (pid > 0) {
+        waitpid(pid, NULL, 0);
+    } else {
+        MOTPLS_LOG(ALR, TYPE_EVENTS, SHOW_ERRNO
+            ,_("Unable to start external command '%s'"), stamp);
+    }
+
     MOTPLS_LOG(DBG, TYPE_EVENTS, NO_ERRNO
         ,_("Executing external command '%s'"), stamp);
 }
Mr-Dave commented 1 year ago

Thanks @davedoesdev . Your revision looks like more of the way to go.