This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

poll function don't release all windows handle / possible memory leak


Hello,

I have found that poll function don't release all windows handle (with
network socket) and generate memory leak.

I have made a simple application to reproduce the problem (server.c).

I compile the application :
$ gcc server.c -o serverleak
$ gcc -Dnoleak server.c -o servernoleak

--------------------------------
How to reproduce the problem

1) I execute the application :

$ ./serverleak

2) I execute the windows taskmanager and I add the column "Handle"
(see http://www.netsolux.ch/cyg/1.gif)

3) I execute on windows "telnet 127.0.0.1 8082" and I press a key on
the terminal

after I see the number of handles (see
http://www.netsolux.ch/cyg/2.gif). I have 137

4) I execute on windows "telnet 127.0.0.1 8082" and I press a key on
the terminal

after I see the number of handles (see
http://www.netsolux.ch/cyg/3.gif). I have 138

5) I execute on windows "telnet 127.0.0.1 8082" and I press a key on
the terminal

after I see the number of handles (see
http://www.netsolux.ch/cyg/4.gif). I have 139

---------------------------

After each call of poll function, I have 1 handle not released.

This problem don't occurr with servernoleak.

Regards,

Thomas

Attachment: cygcheck.out
Description: Binary data

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/un.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <poll.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>

#define port 8082
#define REQUIRED_STACK_SIZE 200*1024
#define OK	0
#define NOT_OK	(!OK)

int logger(const char *file, const char *function, int line, const char *fmt, ...)
{
        va_list arglist;
        fprintf(stderr, "%s (%s) - line %04d : ", file, function, line);
        va_start(arglist, fmt);
        vfprintf(stderr, fmt, arglist);
        va_end(arglist);
        fprintf(stderr, "\n");
        return 0;
}

#define _log(fmt, ...) logger(__FILE__, (char *)__FUNCTION__,  __LINE__, fmt, ## __VA_ARGS__)

int close_socket_fd(int fd) 
{
        int rc; 

        if(fd < 0)
                return OK; 

        rc = shutdown(fd, SHUT_RDWR);

        rc = close(fd);

        if(rc) {
                _log("close %d : %s", fd, strerror(errno));
        }   

        return OK; 
}

static void *serverthread(void *parm)
{
	struct pollfd pfd;
	char buff;
	int fd = (int)parm;
	int rc;

#ifdef noleak
	sleep(3);
	rc = 1;
#else
	pfd.fd = fd; 
	pfd.events = POLLIN | POLLPRI;
	pfd.revents = 0;
	rc = poll(&pfd, 1, 3000);
#endif

	if (rc == 1)
	{
		_log("ICI %d", fd);
		rc = read(fd, &buff, 1);
		_log("read %d bytes", rc);
	}

	close_socket_fd(fd);
        pthread_exit(0);
        return NULL;	
}

int create_thread(pthread_t * thread, void * thread_func, void * arg, unsigned char must_detach)
{
        int err = OK; 
        pthread_attr_t attr;
        pthread_t null_thread;
        /*  Initialize the attribute */
        err = pthread_attr_init(&attr);
        if(err) {
                _log("pthread_attr_init err=%d: %s", err, strerror(errno));
                return NOT_OK;
        }   
        err = pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
        if(err) {
                _log("pthread_attr_setinheritsched: %s", strerror(errno));
                pthread_attr_destroy(&attr);
                return NOT_OK;
        }   
        err = pthread_attr_setstacksize(&attr, REQUIRED_STACK_SIZE);
        if(err) {
                _log("pthread_attr_setstacksize: %s", strerror(errno));
                pthread_attr_destroy(&attr);
                return NOT_OK;
        }   
        if(must_detach) {
                err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
                if(err) {
                        _log("pthread_attr_setdetachstate: %s", strerror(errno));
                        pthread_attr_destroy(&attr);
                        return NOT_OK;
                }   
        }   
        /*  Create the thread with our attribute */
        err = pthread_create((thread != NULL) ? (thread) : (&null_thread), &attr, thread_func, arg);
        if(err) {
                _log("pthread_create: %s", strerror(errno));
                pthread_attr_destroy(&attr);
                return (err);
        }   
        pthread_attr_destroy(&attr);
        return OK; 
}

int main(void)
{
        struct sockaddr_in addr;
        int new_sock, on;
        struct pollfd pfd;
        int fd;
        socklen_t addrlen;
	int ret, rc;

        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = INADDR_ANY;
        memset(&(addr.sin_zero), '\0', 8);
        fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

        if(fd < 0) {
                _log("socket : %s", strerror(errno));
                return NOT_OK;
        } else {
                _log("Socket OK");
        }

        on = 1;
        ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

        if(ret < 0) {
                _log("setsockopt : %s", strerror(errno));
        }

        ret = -1;

        while(ret < 0) {
                ret = bind(fd, (struct sockaddr *) & addr, sizeof(struct sockaddr_in));

                if(ret < 0) {
                        _log("bind : %s", strerror(errno));
                        sleep(1);
                        //                      close_socket(listen_socket);
                } else {
                        _log("Bind OK on port %d", port);
                }
        }

        ret = listen(fd, 1);

        if(ret != 0) {
                _log("listen : %s", strerror(errno));
                close_socket_fd(fd);
                return NOT_OK;
        } else {
                _log("Listen OK on port %d", port);
        }

        while(1) {
                addrlen = sizeof(struct sockaddr_in);
                pfd.fd = fd;
                pfd.events = POLLIN | POLLPRI;
                pfd.revents = 0;
                rc = poll(&pfd, 1, 2000);

                if(rc == -1) {
                        _log("poll : %s", strerror(errno));
                        sleep(1);
                        continue;
                }
                if(pfd.revents == 0) {
                        continue;

                }

                new_sock = accept(fd, (struct sockaddr *) & addr, &addrlen);

                if(new_sock < 0) {
                        _log("accept : %s", strerror(errno));
                        close_socket_fd(new_sock);
                } else {
                        struct linger linger;
                        int option;
                        struct timeval wait_timeout;
                        _log("The client %s are  connected", inet_ntoa(addr.sin_addr));
			ret = create_thread(NULL,  serverthread, (void *) new_sock, 1);

			if(ret < 0) {
				_log("Impossible de demarrer le thread client sur le port %d", port);
				close_socket_fd(new_sock);
			}
                }
        }

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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]