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

fix: when executing a child process, do not waitpid before reading #753

Merged
merged 1 commit into from
Nov 3, 2024
Merged
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
18 changes: 10 additions & 8 deletions src/optionals/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,14 @@ static Value executeReturnOutput(DictuVM* vm, ObjList* argList) {

close(fd[1]);

int status = 0;
waitpid(pid, &status, 0);

if (WIFEXITED(status) && (status = WEXITSTATUS(status)) != 0) {
ERROR_RESULT;
}

int size = 1024;
char* output = ALLOCATE(vm, char, size);
char buffer[1024];
int total = 0;
int numRead;

while ((numRead = read(fd[0], buffer, 1024)) != 0) {
if (total >= size) {
if (total + numRead >= size) {
output = GROW_ARRAY(vm, output, char, size, size * 3);
size *= 3;
}
Expand All @@ -224,6 +217,15 @@ static Value executeReturnOutput(DictuVM* vm, ObjList* argList) {
total += numRead;
}

close(fd[0]);

int status = 0;
waitpid(pid, &status, 0);

if (WIFEXITED(status) && (status = WEXITSTATUS(status)) != 0) {
ERROR_RESULT;
}

output = SHRINK_ARRAY(vm, output, char, size, total + 1);
output[total] = '\0';

Expand Down
Loading