delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2012/05/21/08:44:59

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-1.9 required=5.0 tests=AWL,BAYES_00,KHOP_RCVD_UNTRUST,KHOP_THREADED,RCVD_IN_DNSWL_LOW,RCVD_IN_HOSTKARMA_YE
X-Spam-Check-By: sourceware.org
Message-ID: <4FBA38A5.5030408@sister-shadow.de>
Date: Mon, 21 May 2012 14:44:21 +0200
From: Otto Meta <otto DOT meta AT sister-shadow DOT de>
Reply-To: cygwin AT cygwin DOT com
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20120430 Thunderbird/12.0.1
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Re: 1.7.15-1: pthread_cancel and pthread_kill not working as expected
References: <4FAAAE25 DOT 40204 AT sister-shadow DOT de> <4FB62304 DOT 2000100 AT sister-shadow DOT de> <CA+sc5mkqAoFoO=Jh19WMFe7FDq0337nz5r3-o6ogvGMvt3qPYw AT mail DOT gmail DOT com> <4FBA1846 DOT 6020005 AT sister-shadow DOT de> <20120521104717 DOT GC7763 AT calimero DOT vinschen DOT de>
In-Reply-To: <20120521104717.GC7763@calimero.vinschen.de>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

--------------000304030209070007060307
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

> Would you mind to provide *simple* testcases to allow easy debugging
> of your observations?

I reduced the various tests to three rather simple individual testcases
because those show possibly different bugs.

Testcase cancel deferred:
Works with 1.7.9 and 20120517 snapshot, fails (hangs) with 1.7.12-1
and 1.7.15-1.

Testcase cancel asynchronous:
Async cancel seems to have no effect with any tested version.

Testcase signal/kill:
Signals may or may not reach the correct thread with 1.7.12-1 and newer.

Otto

--------------000304030209070007060307
Content-Type: text/x-csrc;
 name="testcase_cancel_deferred.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="testcase_cancel_deferred.c"

#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

pthread_t tids[3];
sem_t semaphore;

static void cleanup_handler(void *arg) {
  int *intptr = (int*)arg;
  pthread_t self = pthread_self();
  fprintf(stderr, "Thread %i exiting (%p)\n", *intptr, self);
}

static void* simplethread(void *arg) {
  int *intptr = (int*)arg;
  pthread_t self = pthread_self();
  fprintf(stderr, "Thread %i starting (%p)\n", *intptr, self);

  pthread_cleanup_push(&cleanup_handler, intptr);

  while (1) {
    if (sem_wait(&semaphore) != 0) {
      fprintf(stderr, "Thread %i encountered an error: %s (%p)\n",
          *intptr, strerror(errno), self);
    } else {
      fprintf(stderr, "Thread %i woke up just fine\n", *intptr);
    }
  }

  pthread_cleanup_pop(1);
  return NULL;
}

int main() {
  fprintf(stderr, "Testing deferred pthread_cancel()\n\n");

  int i;
  int result;

  sem_init(&semaphore, 0, 0);

  for (i=0; i<3; i++) {
    int *intptr = (int*)malloc(sizeof(int));
    *intptr = i;
    result = pthread_create(tids+i, NULL, &simplethread, intptr);
    if (result != 0) {
      fprintf(stderr, "Can't create thread: %s\n", strerror(result));
      return 1;
    }
  }

  sleep(1);
  fprintf(stderr, "\n");

  int mainint = 42;
  pthread_cleanup_push(&cleanup_handler, &mainint);

  for (i=2; i>=0; i--) {
    fprintf(stderr, "Cancelling thread %i (%p)\n", i, tids[i]);
    result = pthread_cancel(tids[i]);
    if (result != 0) {
      fprintf(stderr, "Error during pthread_cancel: %s\n", strerror(result));
    }
    sleep(1);
  }

  fprintf(stderr, "\n");
  for (i=0; i<3; i++) {
    result = pthread_kill(tids[i], 0);
    if (result == 0) {
      fprintf(stderr, "Thread %i is still there (%p)\n", i, tids[i]);
    } else {
      fprintf(stderr, "Thread %i is gone (%p)\n", i, tids[i]);
    }
  }

  pthread_cleanup_pop(0);
  return 0;
}

--------------000304030209070007060307
Content-Type: text/x-csrc;
 name="testcase_cancel_asynchronous.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="testcase_cancel_asynchronous.c"

#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

pthread_t tids[3];
sem_t semaphore;

static void cleanup_handler(void *arg) {
  int *intptr = (int*)arg;
  pthread_t self = pthread_self();
  fprintf(stderr, "Thread %i exiting (%p)\n", *intptr, self);
}

static void* simplethread(void *arg) {
  int *intptr = (int*)arg;
  pthread_t self = pthread_self();
  fprintf(stderr, "Thread %i starting (%p)\n", *intptr, self);

  pthread_cleanup_push(&cleanup_handler, intptr);

  int oldtype;
  pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
  fprintf(stderr, "Changing canceltype from %i to %i\n", oldtype, PTHREAD_CANCEL_ASYNCHRONOUS);

  while (1) {
    if (sem_wait(&semaphore) != 0) {
      fprintf(stderr, "Thread %i encountered an error: %s (%p)\n",
          *intptr, strerror(errno), self);
    } else {
      fprintf(stderr, "Thread %i woke up just fine\n", *intptr);
    }
  }

  pthread_cleanup_pop(1);
  return NULL;
}

int main() {
  fprintf(stderr, "Testing asynchronous pthread_cancel()\n\n");

  int i;
  int result;

  sem_init(&semaphore, 0, 0);

  for (i=0; i<3; i++) {
    int *intptr = (int*)malloc(sizeof(int));
    *intptr = i;
    result = pthread_create(tids+i, NULL, &simplethread, intptr);
    if (result != 0) {
      fprintf(stderr, "Can't create thread: %s\n", strerror(result));
      return 1;
    }
  }

  sleep(1);
  fprintf(stderr, "\n");

  int mainint = 42;
  pthread_cleanup_push(&cleanup_handler, &mainint);

  for (i=2; i>=0; i--) {
    fprintf(stderr, "Cancelling thread %i (%p)\n", i, tids[i]);
    result = pthread_cancel(tids[i]);
    if (result != 0) {
      fprintf(stderr, "Error during pthread_cancel: %s\n", strerror(result));
    }
    sleep(1);
  }

  fprintf(stderr, "\n");
  for (i=0; i<3; i++) {
    result = pthread_kill(tids[i], 0);
    if (result == 0) {
      fprintf(stderr, "Thread %i is still there (%p)\n", i, tids[i]);
    } else {
      fprintf(stderr, "Thread %i is gone (%p)\n", i, tids[i]);
    }
  }

  pthread_cleanup_pop(0);
  return 0;
}

--------------000304030209070007060307
Content-Type: text/x-csrc;
 name="testcase_signal.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="testcase_signal.c"

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

pthread_t tids[3];
sem_t semaphore;

static void cleanup_handler(void *arg) {
  int *intptr = (int*)arg;
  pthread_t self = pthread_self();
  fprintf(stderr, "Thread %i exiting (%p)\n", *intptr, self);
}

static void* simplethread(void *arg) {
  int *intptr = (int*)arg;
  pthread_t self = pthread_self();
  fprintf(stderr, "Thread %i starting (%p)\n", *intptr, self);

  pthread_cleanup_push(&cleanup_handler, intptr);

  while (1) {
    if (sem_wait(&semaphore) != 0) {
      fprintf(stderr, "Thread %i encountered an error: %s (%p)\n",
          *intptr, strerror(errno), self);
    } else {
      fprintf(stderr, "Thread %i woke up just fine\n", *intptr);
    }
  }

  pthread_cleanup_pop(1);
  return NULL;
}

static void sigusr1_handler(int signal __attribute((unused))) {
  pthread_t self = pthread_self();
  int tnum = 0;
  while (tnum < 3) {
    if (tids[tnum] == self) {
      break;
    }
    tnum++;
  }

  fprintf(stderr, "Thread %i executes signal handler (%p)\n", tnum, self);
}

static void install_handler(void) {
  struct sigaction act;
  act.sa_handler = &sigusr1_handler;
  sigemptyset(&(act.sa_mask));
  act.sa_flags = 0;

  if (sigaction(SIGUSR1, &act, NULL) != 0) {
    fprintf(stderr, "Can't set signal handler: %s\n", strerror(errno));
    exit(1);
  }

  sigset_t sset;
  sigemptyset(&sset);
  sigaddset(&sset, SIGUSR1);
  if (sigprocmask(SIG_UNBLOCK, &sset, NULL) != 0) {
    fprintf(stderr, "Can't unblock SIGUSR1: %s\n", strerror(errno));
  }
}

int main() {
  fprintf(stderr, "Testing pthread_kill()\n\n");

  int i;
  int result;

  sem_init(&semaphore, 0, 0);
  install_handler();

  for (i=0; i<3; i++) {
    int *intptr = (int*)malloc(sizeof(int));
    *intptr = i;
    result = pthread_create(tids+i, NULL, &simplethread, intptr);
    if (result != 0) {
      fprintf(stderr, "Can't create thread: %s\n", strerror(result));
      return 1;
    }
  }

  sleep(1);
  install_handler();
  fprintf(stderr, "\n");

  int mainint = 42;
  pthread_cleanup_push(&cleanup_handler, &mainint);

  for (i=2; i>=0; i--) {
    fprintf(stderr, "Sending SIGUSR1 to thread %i (%p)\n", i, tids[i]);
    result = pthread_kill(tids[i], SIGUSR1);
    if (result != 0) {
      fprintf(stderr, "Error during pthread_kill: %s\n", strerror(result));
    }
    sleep(1);
  }

  pthread_cleanup_pop(0);
  return 0;
}


--------------000304030209070007060307
Content-Type: text/plain; charset=us-ascii

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
--------------000304030209070007060307--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019