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

Handle SIGWINCH in main thread #2921

Merged
merged 2 commits into from Aug 28, 2019
Merged

Conversation

matthewbauer
Copy link
Member

For the SIGWINCH signal to be caught, it needs to be set in sigaction
on the main thread. Previously, this was broken, and updateWindowSize
was never being called. Tested on macOS 10.14.

For the SIGWINCH signal to be caught, it needs to be set in sigaction
on the main thread. Previously, this was broken, and updateWindowSize
was never being called. Tested on macOS 10.14.
@edolstra
Copy link
Member

edolstra commented Jun 5, 2019

How does this interact with the signal handler thread, which is supposed to handle SIGWINCH? It's also not clear to me how installing a signal handler that does nothing causes updateWindowSize to be called.

@matthewbauer
Copy link
Member Author

matthewbauer commented Jun 5, 2019

Ah it looks like this only matter in macOS. On Linux the behavior is correct. I'll need to investigate what is actually going on. I suspect something is broken on macOS pthread_sigmask.

@matthewbauer
Copy link
Member Author

matthewbauer commented Jun 6, 2019

This example program works on Linux but not macOS:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <errno.h>

static sigset_t signal_mask;

void *signal_thread(void *arg) {
  int sig_caught;

  while (1) {
    sigwait(&signal_mask, &sig_caught);
    if (sig_caught == SIGWINCH)
      fprintf(stdout, "SIGWINCH\n");
  }

  return NULL;
}

int main(int argc, char *argv[]) {
  pthread_t sig_thr_id;

  sigemptyset(&signal_mask);
  sigaddset(&signal_mask, SIGWINCH);
  sigprocmask(SIG_BLOCK, &signal_mask, NULL);

  pthread_create(&sig_thr_id, NULL, signal_thread, NULL);

  while(1);

  return 0;
}

while this works on macOS:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <errno.h>

static sigset_t signal_mask;

void *signal_thread(void *arg) {
  int sig_caught;

  while(1) {
    sigwait(&signal_mask, &sig_caught);
    if (sig_caught == SIGWINCH)
      fprintf(stdout, "SIGWINCH\n");
  }

  return NULL;
}

static void sigHandler(int signo) { }

int main(int argc, char *argv[]) {
  pthread_t sig_thr_id;

  struct sigaction sa;
  sa.sa_handler = sigHandler;
  sigaction(SIGWINCH, &sa, 0);

  sigemptyset(&signal_mask);
  sigaddset(&signal_mask, SIGWINCH);
  sigprocmask(SIG_BLOCK, &signal_mask, NULL);

  pthread_create(&sig_thr_id, NULL, signal_thread, NULL);

  while(1);

  return 0;
}

This is not needed on linux at all! Tried to explain as much as I
understand with the problem.
@matthewbauer
Copy link
Member Author

matthewbauer commented Jun 6, 2019

I've put an ifdef to just do it on macOS. it looks like it has nothing to do with pthread. This program also doesn't work:

#include <stdio.h>
#include <signal.h>

static sigset_t signal_mask;

int main (int argc, char *argv[]) {
  sigemptyset(&signal_mask);
  sigaddset(&signal_mask, SIGWINCH);
  sigprocmask(SIG_BLOCK, &signal_mask, NULL);

  int sig_caught;
  while (1) {
    sigwait(&signal_mask, &sig_caught);
    if (sig_caught == SIGWINCH)
      fprintf(stdout, "SIGWINCH\n");
  }

  return 0;
}

While this does:

#include <stdio.h>
#include <signal.h>

static sigset_t signal_mask;

static void sigHandler(int signo) { }

int main (int argc, char *argv[]) {
  struct sigaction sa;
  sa.sa_handler = sigHandler;
  sigaction(SIGWINCH, &sa, 0);

  sigemptyset(&signal_mask);
  sigaddset(&signal_mask, SIGWINCH);
  sigprocmask(SIG_BLOCK, &signal_mask, NULL);

  int sig_caught;
  while (1) {
    sigwait(&signal_mask, &sig_caught);
    if (sig_caught == SIGWINCH)
      fprintf(stdout, "SIGWINCH\n");
  }

  return 0;
}

@matthewbauer
Copy link
Member Author

@edolstra @grahamc Could you reconsider this one for nix-2.3? It solves a real problem on macOS right now.

@edolstra edolstra merged commit 7ef2645 into NixOS:master Aug 28, 2019
@edolstra
Copy link
Member

Thanks!

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

Successfully merging this pull request may close these issues.

None yet

2 participants