Skip to content

Instantly share code, notes, and snippets.

@skaji
Created January 28, 2022 11:22
Show Gist options
  • Save skaji/84a4ea75480298f839f7cf4adcc109c9 to your computer and use it in GitHub Desktop.
Save skaji/84a4ea75480298f839f7cf4adcc109c9 to your computer and use it in GitHub Desktop.
I reported this issue to https://feedbackassistant.apple.com/ at 2022-01-26T15:00:00+09:00

Summary

stat("../bin") on /Users directory sometimes fails with errno=2 (ENOENT)

Environment

I saw this failure on several Mac machines. Especially:

  • macOS Monterey 12.1, MacBook Pro 13-inch, 2020, M1
  • macOS Monterey 12.1, MacBook Pro 16-inch, 2019, Intel
  • macOS Catalina 10.15.7, MacBook Pro 15-inch, 2013, Intel

Steps to reproduce the problem

Step1: Create a simple C-program ~/a.c that calls stat forever

❯ cd ~
❯ cat a.c
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>

extern int errno;

int main() {
  struct stat buf;
  long i = 0;
  for (;;) {
    ++i;
    if (stat("../bin", &buf)) {
      printf("%ld: stat(\"../bin\"): errno=%d\n", i, errno);
    }
  }
}
❯ cc a.c
❯ ls a.out
a.out

Step2: Execute ~/a.out in /Users directory

❯ cd /Users
❯ ~/a.out

Step3: Then you'll see some failures of ~/a.out

❯ ~/a.out
2091386: stat("../bin"): errno=2
2092797: stat("../bin"): errno=2
2094995: stat("../bin"): errno=2
...

Please note that the failures may increase if we execute some programs in other terminal, especially:

  • cd /Users
  • /usr/local/bin/gls /Users (where gls comes from homebrew)
  • /opt/homebrew/bin/gls /Users (where gls comes from homebrew)

Note

  • The "dot-dot" notation (../bin) seems important; if we call stat("/bin"), then it doesn't fail very much.
  • Calling stat from /Users directory also seems important; if we call stat("../bin") from /Applications directory, then it doesn't fail very much.
  • I only mentioned stat function; but open("../bin/ls", O_RDONLY) also sometimes fails with errno=2 (ENOENT)
@skaji
Copy link
Author

skaji commented Mar 19, 2022

Fixed in macOS 12.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment