  
  
 --- a/src/loslib.c	2017-04-19 13:29:57.000000000 -0400
-+++ b/src/loslib.c	2020-05-24 14:28:49.259487829 -0400
++++ b/src/loslib.c	2020-06-22 19:16:23.952024597 -0400
 @@ -4,6 +4,15 @@
  ** See Copyright Notice in lua.h
  */

 
+/** Process exit cleanup function. */
+static void exited(PStream *p, int status) {
+#if _WIN32
+  close(p->pid);
+#endif
+  close(p->fstdin), close(p->fstdout), close(p->fstderr);
+  luaL_unref(p->L, LUA_REGISTRYINDEX, p->stdout_cb);
+  luaL_unref(p->L, LUA_REGISTRYINDEX, p->stderr_cb);
+  luaL_unref(p->L, LUA_REGISTRYINDEX, p->exit_cb);
+  luaL_unref(p->L, LUA_REGISTRYINDEX, p->ref); // allow proc to be collected
+  p->pid = 0, p->exit_status = status;
+}
+

   {"time",      os_time},
   {"tmpname",   os_tmpname},
   {NULL, NULL}
@@ -404,6 +419,643 @@
 
 LUAMOD_API int luaopen_os (lua_State *L) {
   luaL_newlib(L, syslib);
+#if (!GTK && !_WIN32 || __APPLE__)
+  // Need to keep track of running processes for monitoring fds and pids.
+  lua_newtable(L), lua_setfield(L, LUA_REGISTRYINDEX, "spawn_procs");
+#endif
