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

select function in "sys/select.h" randomly crashes the app #97

Open
cy33hc opened this issue Jul 7, 2023 · 0 comments
Open

select function in "sys/select.h" randomly crashes the app #97

cy33hc opened this issue Jul 7, 2023 · 0 comments

Comments

@cy33hc
Copy link

cy33hc commented Jul 7, 2023

I'm working on a homebrew that will read and download files from a http server. In the app I would check if the socket is open and still readable before trying to read from it. The app would randomly crash on the "select" function.

Here's the code sniplet of that function.

template <typename T> ssize_t handle_EINTR(T fn) {
  ssize_t res = false;
  while (true) {
    res = fn();
    if (res < 0 && errno == EINTR) { continue; }
    break;
  }
  return res;
}

ssize_t select_read(socket_t sock, time_t sec, time_t usec) {
  if (sock >= FD_SETSIZE) { return 1; }

  fd_set fds;
  FD_ZERO(&fds);
  FD_SET(sock, &fds);

  timeval tv;
  tv.tv_sec = static_cast<long>(sec);
  tv.tv_usec = static_cast<decltype(tv.tv_usec)>(usec);

  return handle_EINTR([&]() {
    return select(static_cast<int>(sock + 1), &fds, nullptr, nullptr, &tv);  <<< crash on this line randomly
  });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant