delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2012/05/22/09:27:02

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-2.1 required=5.0 tests=AWL,BAYES_00,KHOP_RCVD_UNTRUST,KHOP_THREADED,RCVD_IN_DNSWL_LOW
X-Spam-Check-By: sourceware.org
Message-ID: <4FBB93D6.90408@sister-shadow.de>
Date: Tue, 22 May 2012 15:25:42 +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> <4FBA38A5 DOT 5030408 AT sister-shadow DOT de> <20120522110238 DOT GC15843 AT calimero DOT vinschen DOT de>
In-Reply-To: <20120522110238.GC15843@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

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

>> Testcase cancel deferred:
>> Works with 1.7.9 and 20120517 snapshot, fails (hangs) with 1.7.12-1
>> and 1.7.15-1.
> If that works in the snapshot anyway, I'm not going to look into that
> one.

It worked in the reduced testcase with sem_wait(). With read() it’s
still half-broken. See below.

>> Testcase cancel asynchronous:
>> Async cancel seems to have no effect with any tested version.
> I think I found a solution for this problem.  See the comment in the
> patch at
> http://sourceware.org/cgi-bin/cvsweb.cgi/src/winsup/cygwin/thread.cc.diff?cvsroot=src&r1=1.258&r2=1.259
> Please test the today's developer snapshot.

Asynchronous cancel seems to work as well as deferred cancel now. Thanks.

Both cancel types work with sem_wait() and pause() now, but for threads
blocked in read() they’re still unreliable. Only one of three blocked
threads is killed in the attached updated testcases.

>> Testcase signal/kill:
>> Signals may or may not reach the correct thread with 1.7.12-1 and newer.
> Confirmed. [...] This is cgf's domain so I leave it at that for now.

Okay, I’ll hope for him to respond then.

Otto

--------------070403050802080709040303
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

pthread_t tids[3];

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);
  char buffer[1] __attribute((unused));

  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 (read(STDIN_FILENO, buffer, 1) <= 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;

  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;
}

--------------070403050802080709040303
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

pthread_t tids[3];

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);
  char buffer[1] __attribute((unused));

  pthread_cleanup_push(&cleanup_handler, intptr);

  while (1) {
    if (read(STDIN_FILENO, buffer, 1) <= 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;

  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;
}


--------------070403050802080709040303
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
--------------070403050802080709040303--

- Raw text -


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