# Exit function sometimes not working

**URL:** <https://forum.codecrafters.io/t/exit-function-sometimes-not-working/15727>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [January 4, 2026, 2:31pm UTC](https://forum.codecrafters.io/t/exit-function-sometimes-not-working/15727 "2026-01-04T14:31:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![devnchill](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/devnchill/32/7291_2.png) [@devnchill](https://forum.codecrafters.io/u/devnchill)\
**Post date:** [January 4, 2026, 2:31pm UTC](https://forum.codecrafters.io/t/exit-function-sometimes-not-working/15727/1 "2026-01-04T14:31:10Z")

</div>

![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/4/6/46f1dbc6256b950d542462d95ecdca71c28ad769.png)

## main.c

```c
typedef struct parsed_command {
  int argc;
  char **argv;
} pc;

pc parse_command(char *command, size_t size) {
  pc p = {0};

  if (fgets(command, size, stdin) == NULL) {
    p.argv = NULL;
    return p;
  };
  command[strcspn(command, "\n")] = '\0';

  p.argv = malloc(128 * sizeof(char *));
  if (!p.argv)
    return p;

  char *token = strtok(command, " ");
  while (token && p.argc < 127) {
    p.argv[p.argc++] = token;
    token = strtok(NULL, " ");
  }
  p.argv[p.argc] = NULL;

  return p;
}

int main() {
  setbuf(stdout, NULL);

  add_builtins();

  char raw_command[1024];

  while (1) {

    printf("$ ");

    pc command = parse_command(raw_command, sizeof(raw_command));

    if (!command.argv) {
      printf("\n");
      break;
    }

    if (!command.argv[0])
      continue;

    shell_builtin *builtin = hashmap_get(command.argv[0]);
    if (builtin) {
      printf("builtin command detected\n");
      builtin->func(command.argc, command.argv);
      free(command.argv);
      continue;
      printf("after continue 1\n");
    }
    printf("after continue 2\n");

    if (run_program(command.argc, command.argv) == -1) {
      printf("%s: command not found\n", command.argv[0]);
    }
    printf("end of while loop . clearing argv\n");
    free(command.argv);
  }

  return 0;
}

```

## exit.c

```c
int builtin_exit(int argc, char **argv) { exit(0); }

```

### run\_program.c

```c
int run_program(int argc, char **argv) {
  pid_t pid = fork();
  switch (pid) {
  case -1: {
    perror("failed starting child process");
    return -1;
  };
  case 0: {
    execvp(argv[0], argv);
    return -1;
  };
  default: {
    waitpid(pid, NULL, 0);
    return 0;
  };
  }
}

```

seems like if i run any command which doesn’t calls `run_program` , exit works fine however if i run any command calling `run_program` where command is not a valid command  
, i need to run exit several times before my shell terminates.

My doubt is it’s somewhat related to child process however i’m using `waitpid` which should wait for child process to finish. hence i’m stuck here

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [January 4, 2026, 2:31pm UTC](https://forum.codecrafters.io/t/exit-function-sometimes-not-working/15727/2 "2026-01-04T14:31:11Z")

</div>

Hi, thanks for your post!

I’m currently out of the office for the holidays and will be back on January 5. I’ll get back to you as soon as possible once I return.

---

<div class="post-metadata">

**Author:** ![devnchill](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/devnchill/32/7291_2.png) [@devnchill](https://forum.codecrafters.io/u/devnchill)\
**Post date:** [January 5, 2026, 8:01am UTC](https://forum.codecrafters.io/t/exit-function-sometimes-not-working/15727/5 "2026-01-05T08:01:33Z")

</div>

ok i fixed it. for anyone wondering,issue was , if `execvp` failed,it would return back where i was returning -1 leading to another process. i added `_exit` to ensure that if it fails the child process gets terminated immediately

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [January 10, 2026, 8:24am UTC](https://forum.codecrafters.io/t/exit-function-sometimes-not-working/15727/7 "2026-01-10T08:24:17Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
