I've been using clamav's clamd under cygwin and noticed that over time the handle count as viewed with TaskManager seems to grow to arbitrary values. I used clamd's option IdleTimeout set to 600 seconds which dramatically reduced the growth rate of the Handle Count. Of course clamd has many things going on that could contribute to handle leakags, so I tried to write a simple program to demonstrate the problem.

The attached program demonstrates the problem when sockets are used and that things look pretty clean when they are not.

There seems to be both a thread leakage issue and a separate handle leakage issue.

Invoking the program as:
threadtest -sockets 0
creates groups of 5 threads simultaneously. Each thread merely prints something and sleeps, prints something else and exits. This is repeated 10 times displaying the process handle count between each iteration. While running and watching with Task Manager, the process thread counts seems to start at 2 and reach 7 at times and then return to 2. The handle count grows during the first iteration but stays flat thereafter.



Invoking the program as:
threadtest -sockets 3
creates groups of 5 threads simultaneously. Each thread merely prints something and sleeps, connects a socket to the main thread, passes a little data and closes the socket. This socket business is repeated 3 times after which the thread prints something else and exits. This is repeated 10 times displaying the process handle count between each iteration. While running and watching with Task Manager, the process thread counts seems to start at 2 and reach 14 during the each iteration and drops back to 9 between iterations. The handle count grows significantly during the first iteration but seems to grow by 10 or 11 between each subsequent iteration. The -sockets 3 argument controls the number of sockets each thread creates during its life. The amount of thread and handle leakage seems to be independent of the number of sockets the thread uses during its lifetime (as long as the number of sockets used is 1 or greater).


The number of threads created simultaneously can be controlled by specifying -threads n as command arguments. The number of threads leaked seems to be directly related to the number of thread using sockets concurrently. running the program with -sockets 3 and -threads 10 causes the thread count to jump to 24 during each iteration and drop back to 14 between iterations, while the handle count seems to increase by 10 or 11 between each iteration identical to the case described in the previous paragraph.

I hope this test can help someone familar enough with cygwin internals to help get this problem under control.

- Mark Pizzolato
#include <windows.h>

typedef enum _PROCESSINFOCLASS {
   ProcessHandleCount = 20,
} PROCESSINFOCLASS;
typedef LONG NTSTATUS;

int
GetHandleCount()
{
        static NTSTATUS
        (*lpNtQueryInformationProcess)(
                HANDLE           ProcessHandle,
                PROCESSINFOCLASS ProcessInformationClass,
                PVOID            ProcessInformation,
                ULONG            ProcessInformationLength,
                PULONG           ReturnLength ) = NULL;
        static int bInit = 0;
        int HandleCount = 0;

        if (FALSE == bInit) {
                lpNtQueryInformationProcess = (NTSTATUS ( *)(HANDLE, PROCESSINFOCLASS, PVOID, 
ULONG, PULONG))GetProcAddress(GetModuleHandle("ntdll.dll"), 
"NtQueryInformationProcess");
                bInit = 1;
        }
        if (NULL != lpNtQueryInformationProcess) {
                lpNtQueryInformationProcess( GetCurrentProcess(), 
ProcessHandleCount, &HandleCount, sizeof(HandleCount), NULL );
        }
        return HandleCount;
}



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_attr_t attr;
struct sockaddr_in server;
int sockets=2;
int threadgroup=5;

void *thread_code(void *arg)
{
        int threadnum = (int) arg;
        int i;
        int s;
        char buf[64];
        pthread_mutex_lock(&mutex);
        printf("Thread %d running...\n", threadnum);
        pthread_mutex_unlock(&mutex);
        for (i=0; i<sockets; ++i) {
                sleep(2);
                if((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
                        pthread_mutex_lock(&mutex);
                        printf("Thread %d socket() error\n", threadnum);
                        pthread_mutex_unlock(&mutex);
                        goto error;
                }
                if (-1 == connect(s, (struct sockaddr *)&server, 
sizeof(server))) {
                        pthread_mutex_lock(&mutex);
                        printf("Thread %d connect() error\n", threadnum);
                        pthread_mutex_unlock(&mutex);
                        goto error;
                }
                snprintf(buf, sizeof(buf)-1, "Data From Thread: %d, Socket:%d", 
threadnum, i);
                if (-1 == send(s, buf, strlen(buf), 0)) {
                        pthread_mutex_lock(&mutex);
                        printf("Thread %d send() error\n", threadnum);
                        pthread_mutex_unlock(&mutex);
                        goto error;
                }
                if (close(s)) {
                        pthread_mutex_lock(&mutex);
                        printf("Thread %d: close() error: %s\n", threadnum, 
strerror(errno));
                        pthread_mutex_unlock(&mutex);
                        goto error;
                }
        }
        sleep(2);
        pthread_mutex_lock(&mutex);
        printf("Thread %d done.\n", threadnum);
        pthread_mutex_unlock(&mutex);
        return;
        
error:  pthread_mutex_lock(&mutex);
        printf("Thread %d error %s.\n", threadnum, strerror(errno));
        pthread_mutex_unlock(&mutex);
}

main(int argc, char **argv)
{
        int i, j, s;
        pthread_t thr_id;
        int true = 1;
        int HandleCount;

while (--argc>0) {
++argv;
if (!strcmp("-sockets", *argv)) {
++argv; --argc;
sockets = atoi(*argv);
continue;
}
if (!strcmp("-threads", *argv)) {
++argv; --argc;
threadgroup = atoi(*argv);
continue;
}
printf("Unknown argument: %s\n", *argv);\
printf("Usage: threadtest -s n -t n\n");
printf("where:\n");
printf(" -sockets specifies the number of sockets that each thread should\n");
printf(" use during its life. Interesting values are 0, 1 and\n");
printf(" anything greater than 1. The default value is 2.\n");
printf(" -threads specifies the number of threads to be created simultaneously.\n");
printf(" The default value of 5 is sufficient to demonstrate thread\n");
printf(" and Handle leakage.\n");
exit(1);
}
printf("Starting... Creating %d threads at a time with each thread using %d sockets\n", threadgroup, sockets);


        if((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
                fprintf(stderr, "socket() error: %s\n", strerror(errno));
        }

        if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *) &true, 
sizeof(true)) == -1) {
                fprintf(stderr, "setsockopt(SO_REUSEADDR) error: %s\n", 
strerror(errno));
        }

        memset((char *) &server, 0, sizeof(server));
        inet_aton("127.0.0.1", &server.sin_addr);
        server.sin_family = AF_INET;
        server.sin_port = htons(12345);
        if(bind(s, (struct sockaddr *) &server, sizeof(server)) == -1) {
                fprintf(stderr, "bind() error: %s\n", strerror(errno));
        }
        if(listen(s, 2*threadgroup) == -1) {
                fprintf(stderr, "listen() error: %s\n", strerror(errno));
        }

        sleep(4);
        if (pthread_mutex_init(&mutex, NULL) != 0) {
                fprintf(stderr, "Error in pthread_mutex_init\n");
        }
        if (pthread_cond_init(&cond, NULL) != 0) {
                fprintf(stderr, "Error in pthread_cond_init\n");
        }
                
        if (pthread_attr_init(&attr) != 0) {
                fprintf(stderr, "Error in pthread_attr_init\n");
        }
        
        if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
                fprintf(stderr, "Error in pthread_attr_setdetachedstate\n");
        }

        printf("Initial Handle Count: %d\n", GetHandleCount());

        for (j=0; j<10; j++) {
                for (i=0; i<threadgroup; i++) {
                        if (pthread_create(&thr_id, &attr, thread_code, (void 
*)i) != 0) {
                                fprintf(stderr,"pthread_create failed\n");
                        }
                }
                for (i=0; i<sockets*threadgroup; i++) {
                        int c;
                        char buf[64];
                        if (-1 == (c=accept(s, NULL, NULL))) {
                                if (errno == EINTR) {
                                        --i;
                                        continue;
                                }
                                fprintf(stderr, "accept() error: %s\n", 
strerror(errno));
                        }
                        memset(buf, 0, sizeof(buf));
                        if (-1 == recv(c, buf, sizeof(buf)-1, 0)) {
                                fprintf(stderr, "recv() error: %s\n", 
strerror(errno));
                        }
                        pthread_mutex_lock(&mutex);
                        printf("Received '%s' from thread\n", buf);
                        pthread_mutex_unlock(&mutex);
                        if (close(c)) {
                                pthread_mutex_lock(&mutex);
                                printf("Main Thread: close() error: %s\n", 
strerror(errno));
                                pthread_mutex_unlock(&mutex);
                        }
                }
                sleep(10);
                pthread_mutex_lock(&mutex);
                printf("Thread group %d complete...Handle Count: %d\n", j, 
GetHandleCount());
                pthread_mutex_unlock(&mutex);
        }
        close(s);
        printf("All done...\n");
        sleep(30);
}

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

Reply via email to