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

fixed mac os http server #992

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

troy4eg
Copy link
Contributor

@troy4eg troy4eg commented May 3, 2024

The problem was related to using epoll-shim, which relies on the kqueue() function. If you review the documentation for kqueue(), you will see that it states the following: "The kqueue() system call creates a new kernel event queue and returns a descriptor. The queue is not inherited by a child created with fork(2)". It is easy to verify this using the following code:

#include <iostream>
#include <sys/event.h>
#include <sys/socket.h>
#include <unistd.h>

int main() {
  int kfd = kqueue();
  std::cout << "kfd: " << kfd << std::endl;
  pid_t child = fork();
  if (child == 0) {
    int sfd;
    if (sfd = socket(AF_INET, SOCK_STREAM, 0); sfd < 0) {
      exit(-1);
    }
    std::cout << "from child kfd: " << kfd << ", sfd: " << sfd << std::endl;
  } else {
    int sfd;
    if (sfd = socket(AF_INET, SOCK_STREAM, 0); sfd < 0) {
      exit(-1);
    }
    std::cout << "from parent kfd: " << kfd << ", sfd: " << sfd << std::endl;
  }

  return 0;
}

Output:

kfd: 3
from parent kfd: 3, sfd: 4
from child kfd: 3, sfd: 3

It is worth noting that in KPHP, we use the prefork model. In this model, the main process, or master, first creates all necessary resources, such as file descriptors. Then, the process calls the fork function to create several child processes, controlled by the "-f" flag. The resulting file descriptor numbers are used as indexes for the "Targets" array, which stores references to event handler functions. This means that when the accept() call is made in the worker process, it cannot find the desired function in the "Targets" array because all workers have different file descriptor offsets.

I understand that this may not be the most elegant solution, but it is simple and effective.

@troy4eg troy4eg added the bug Something isn't working label May 3, 2024
@troy4eg troy4eg self-assigned this May 3, 2024
@troy4eg troy4eg requested a review from DrDet May 3, 2024 16:30
@troy4eg troy4eg linked an issue May 3, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

http does not work for mac os
1 participant