Fix process api detach bug (#1137)

* Added in conditional, as detaching will fail with "Operation not permitted" on linux; setsid sets a new process group anyway.

* Added in variable to check detached state on cleanup, so we don't send TERM.
This commit is contained in:
Adam 2022-10-04 20:02:22 -04:00 committed by GitHub
parent 1580d923d3
commit 293110feaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 4 deletions

View File

@ -29,7 +29,7 @@ typedef int process_handle;
#endif #endif
typedef struct { typedef struct {
bool running; bool running, detached;
int returncode, deadline; int returncode, deadline;
long pid; long pid;
#if _WIN32 #if _WIN32
@ -38,7 +38,7 @@ typedef struct {
bool reading[2]; bool reading[2];
char buffer[2][READ_BUF_SIZE]; char buffer[2][READ_BUF_SIZE];
#endif #endif
process_handle child_pipes[3][2]; process_handle child_pipes[3][2];
} process_t; } process_t;
typedef enum { typedef enum {
@ -183,6 +183,7 @@ static int process_start(lua_State* L) {
memset(self, 0, sizeof(process_t)); memset(self, 0, sizeof(process_t));
luaL_setmetatable(L, API_TYPE_PROCESS); luaL_setmetatable(L, API_TYPE_PROCESS);
self->deadline = deadline; self->deadline = deadline;
self->detached = detach;
#if _WIN32 #if _WIN32
for (int i = 0; i < 3; ++i) { for (int i = 0; i < 3; ++i) {
switch (new_fds[i]) { switch (new_fds[i]) {
@ -278,7 +279,8 @@ static int process_start(lua_State* L) {
retval = luaL_error(L, "Error running fork: %s.", strerror(errno)); retval = luaL_error(L, "Error running fork: %s.", strerror(errno));
goto cleanup; goto cleanup;
} else if (!self->pid) { } else if (!self->pid) {
setpgid(0,0); if (!detach)
setpgid(0,0);
for (int stream = 0; stream < 3; ++stream) { for (int stream = 0; stream < 3; ++stream) {
if (new_fds[stream] == REDIRECT_DISCARD) { // Close the stream if we don't want it. if (new_fds[stream] == REDIRECT_DISCARD) { // Close the stream if we don't want it.
close(self->child_pipes[stream][stream == STDIN_FD ? 0 : 1]); close(self->child_pipes[stream][stream == STDIN_FD ? 0 : 1]);
@ -454,7 +456,8 @@ static int f_kill(lua_State* L) { return self_signal(L, SIGNAL_KILL); }
static int f_interrupt(lua_State* L) { return self_signal(L, SIGNAL_INTERRUPT); } static int f_interrupt(lua_State* L) { return self_signal(L, SIGNAL_INTERRUPT); }
static int f_gc(lua_State* L) { static int f_gc(lua_State* L) {
process_t* self = (process_t*) luaL_checkudata(L, 1, API_TYPE_PROCESS); process_t* self = (process_t*) luaL_checkudata(L, 1, API_TYPE_PROCESS);
signal_process(self, SIGNAL_TERM); if (!self->detached)
signal_process(self, SIGNAL_TERM);
close_fd(&self->child_pipes[STDIN_FD ][1]); close_fd(&self->child_pipes[STDIN_FD ][1]);
close_fd(&self->child_pipes[STDOUT_FD][0]); close_fd(&self->child_pipes[STDOUT_FD][0]);
close_fd(&self->child_pipes[STDERR_FD][0]); close_fd(&self->child_pipes[STDERR_FD][0]);