Public bug reported:

timer_create(), when told to run a function at time expiration, creates 
immediately an ancillary thread
(it could have created it when armed, or even better, when expired). When the 
timer is deleted, the
ancillary thread does not get cancelled. Not even if it was created as 
detached. That ancillary thread
must instead be terminated, otherwise it wastes resources (and prevents the 
process to terminate
normally).
Program that shows it:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include <dirent.h>
#include <stddef.h>

// enumerate all threads of this process
static void listthreads(char* str){
    printf("threads %s\n",str);
    char buf[100];
    sprintf(buf,"/proc/%d/task",getpid());
    DIR* dir = opendir(buf);
    int len = offsetof(struct dirent,d_name) +
        pathconf(buf,_PC_NAME_MAX) + 1;
    struct dirent* entryp;
    entryp = malloc(len);
    struct dirent* res;
    for (;;){
        readdir_r(dir,entryp,&res);
        if (res == NULL) break;
        if (entryp->d_name[0] == '.') continue;
        printf("    %s\n",entryp->d_name);
    }
    closedir(dir);
    printf("end of list\n");
}

void timerfunction(sigval_t sigval){
}

int main(int argc, char* argv[]){
    printf("main %d\n",getpid());
    struct sigevent event;
    event.sigev_notify = SIGEV_THREAD;
    event.sigev_notify_function = timerfunction;
    event.sigev_notify_attributes = NULL;
    event.sigev_value.sival_ptr = (void*)pthread_self();
    timer_t timer_id;
    if (timer_create(CLOCK_REALTIME,&event,&timer_id) < 0){
        perror("timer create");
    }
    listthreads("after timer creation");
    struct itimerspec itime;                     // arm timer
    itime.it_value.tv_sec = 10;
    itime.it_value.tv_nsec = 0;
    itime.it_interval.tv_sec = 0;
    itime.it_interval.tv_nsec = 0; 
    if (timer_settime(timer_id,0,&itime,NULL) < 0){
        perror("timer settime");
    }
    listthreads("after timer arming");

    timer_delete(timer_id);                      // disarm and delete timer
    listthreads("after timer delete");
}

$ gcc timerdelete.c -lrt
$ ./a.out
main 9958
threads after timer creation
    9958
    9959
end of list
threads after timer arming
    9958
    9959
end of list
threads after timer delete
    9958
    9959
end of list

ProblemType: Bug
Architecture: i386
AudioDevicesInUse:
 USER        PID ACCESS COMMAND
 /dev/snd/controlC0:  angelo     1643 F.... pulseaudio
CRDA: Error: [Errno 2] No such file or directory
Card0.Amixer.info:
 Card hw:0 'NVidia'/'HDA NVidia at 0xfe028000 irq 23'
   Mixer name   : 'Realtek ALC888'
   Components   : 'HDA:10ec0888,1631e601,00100001'
   Controls      : 37
   Simple ctrls  : 21
Date: Sat Nov 28 10:24:21 2009
DistroRelease: Ubuntu 9.10
HibernationDevice: RESUME=UUID=335bd2d5-504f-4410-a53a-96814ee336e8
InstallationMedia: Ubuntu 9.10 "Karmic Koala" - Release i386 (20091028.5)
Lsusb:
 Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
 Bus 002 Device 002: ID 046d:c315 Logitech, Inc. Classic New Touch Keyboard
 Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
MachineType: Packard Bell BV IMEDIA 8425
NonfreeKernelModules: nvidia
Package: linux-image-2.6.31-15-generic 2.6.31-15.50
ProcCmdLine: BOOT_IMAGE=/boot/vmlinuz-2.6.31-15-generic 
root=UUID=4643cae4-fc87-4dec-a73c-04ce2ccfc02a ro quiet splash
ProcEnviron:
 LANG=en_US.UTF-8
 SHELL=/bin/bash
ProcVersionSignature: Ubuntu 2.6.31-15.50-generic
RelatedPackageVersions:
 linux-backports-modules-2.6.31-15-generic N/A
 linux-firmware 1.25
RfKill:
 0: phy0: Wireless LAN
        Soft blocked: no
        Hard blocked: no
SourcePackage: linux
Uname: Linux 2.6.31-15-generic i686
dmi.bios.date: 11/01/2006
dmi.bios.vendor: Phoenix Technologies, LTD
dmi.bios.version: M2N-NM 0401
dmi.board.name: M2N-NM
dmi.board.vendor: Packard Bell BV
dmi.board.version: 1.XX
dmi.chassis.asset.tag: 123456789000
dmi.chassis.type: 3
dmi.chassis.vendor: Packard Bell BV
dmi.chassis.version: Chassis Version
dmi.modalias: 
dmi:bvnPhoenixTechnologies,LTD:bvrM2N-NM0401:bd11/01/2006:svnPackardBellBV:pnIMEDIA8425:pvrPB80107506:rvnPackardBellBV:rnM2N-NM:rvr1.XX:cvnPackardBellBV:ct3:cvrChassisVersion:
dmi.product.name: IMEDIA 8425
dmi.product.version: PB80107506
dmi.sys.vendor: Packard Bell BV

** Affects: linux (Ubuntu)
     Importance: Undecided
         Status: New


** Tags: apport-bug i386

-- 
timer_delete does not cancel the thread to run the function
https://bugs.launchpad.net/bugs/489497
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.

-- 
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to