All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] close.2: Mention a need of shutdown before closing socket
@ 2011-08-29 12:26 Lukas Czerner
       [not found] ` <1314620800-15587-1-git-send-email-lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
  0 siblings, 1 reply; 11+ messages in thread
From: Lukas Czerner @ 2011-08-29 12:26 UTC (permalink / raw)
  To: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w
  Cc: linux-man-u79uwXL29TY76Z2rM5mHXA, Lukas Czerner

When dealing with sockets, we have to be sure that there is no recv
still blocking on it on another thread, otherwise it might block forever,
since no more messages will be send via the socket. We should advice to
use shutdown before closing socket.

Signed-off-by: Lukas Czerner <lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
---
 man2/close.2 |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)

diff --git a/man2/close.2 b/man2/close.2
index a2a2961..02472f3 100644
--- a/man2/close.2
+++ b/man2/close.2
@@ -114,6 +114,13 @@ other threads in the same process.
 Since a file descriptor may be reused,
 there are some obscure race conditions
 that may cause unintended side effects.
+.PP
+When dealing with sockets, you have to be sure that there is no
+.BR recv (2)
+still blocking on it on another thread, otherwise it might block forever,
+since no more messages will be send via the socket. Be sure to use
+.BR shutdown (2)
+to shut down all parts the connection before closing the socket.
 .\" Date: Tue, 4 Sep 2007 13:57:35 +0200
 .\" From: Fredrik Noring <noring-zgYzP9v7iJcdnm+yROfE0A@public.gmane.org>
 .\" One such race involves signals and ERESTARTSYS. If a file descriptor
-- 
1.7.4.4

--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] close.2: Mention a need of shutdown before closing socket
       [not found] ` <1314620800-15587-1-git-send-email-lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
@ 2011-09-01  8:43   ` Lukas Czerner
       [not found]     ` <alpine.LFD.2.00.1109011043050.3665-D5fXUWdM4RC5eBp5B7zZTR/sF2h8X+2i0E9HWUfgJXw@public.gmane.org>
  2011-09-10 13:59   ` Michael Kerrisk
  2011-10-07  6:28   ` Michael Kerrisk
  2 siblings, 1 reply; 11+ messages in thread
From: Lukas Czerner @ 2011-09-01  8:43 UTC (permalink / raw)
  To: Lukas Czerner
  Cc: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w, linux-man-u79uwXL29TY76Z2rM5mHXA

On Mon, 29 Aug 2011, Lukas Czerner wrote:

> When dealing with sockets, we have to be sure that there is no recv
> still blocking on it on another thread, otherwise it might block forever,
> since no more messages will be send via the socket. We should advice to
> use shutdown before closing socket.

Hello guys,

I am not subscribed to this ML, but do you have any comments on this one
? Could you take it in ?

Thanks!
-Lukas

> 
> Signed-off-by: Lukas Czerner <lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
> ---
>  man2/close.2 |    7 +++++++
>  1 files changed, 7 insertions(+), 0 deletions(-)
> 
> diff --git a/man2/close.2 b/man2/close.2
> index a2a2961..02472f3 100644
> --- a/man2/close.2
> +++ b/man2/close.2
> @@ -114,6 +114,13 @@ other threads in the same process.
>  Since a file descriptor may be reused,
>  there are some obscure race conditions
>  that may cause unintended side effects.
> +.PP
> +When dealing with sockets, you have to be sure that there is no
> +.BR recv (2)
> +still blocking on it on another thread, otherwise it might block forever,
> +since no more messages will be send via the socket. Be sure to use
> +.BR shutdown (2)
> +to shut down all parts the connection before closing the socket.
>  .\" Date: Tue, 4 Sep 2007 13:57:35 +0200
>  .\" From: Fredrik Noring <noring-zgYzP9v7iJcdnm+yROfE0A@public.gmane.org>
>  .\" One such race involves signals and ERESTARTSYS. If a file descriptor
> 

-- 
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] close.2: Mention a need of shutdown before closing socket
       [not found]     ` <alpine.LFD.2.00.1109011043050.3665-D5fXUWdM4RC5eBp5B7zZTR/sF2h8X+2i0E9HWUfgJXw@public.gmane.org>
@ 2011-09-07 15:54       ` Lukas Czerner
  0 siblings, 0 replies; 11+ messages in thread
From: Lukas Czerner @ 2011-09-07 15:54 UTC (permalink / raw)
  To: Lukas Czerner
  Cc: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w, linux-man-u79uwXL29TY76Z2rM5mHXA

On Thu, 1 Sep 2011, Lukas Czerner wrote:

> On Mon, 29 Aug 2011, Lukas Czerner wrote:
> 
> > When dealing with sockets, we have to be sure that there is no recv
> > still blocking on it on another thread, otherwise it might block forever,
> > since no more messages will be send via the socket. We should advice to
> > use shutdown before closing socket.
> 
> Hello guys,
> 
> I am not subscribed to this ML, but do you have any comments on this one
> ? Could you take it in ?
> 
> Thanks!
> -Lukas

Still no response ?

-Lukas

> 
> > 
> > Signed-off-by: Lukas Czerner <lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
> > ---
> >  man2/close.2 |    7 +++++++
> >  1 files changed, 7 insertions(+), 0 deletions(-)
> > 
> > diff --git a/man2/close.2 b/man2/close.2
> > index a2a2961..02472f3 100644
> > --- a/man2/close.2
> > +++ b/man2/close.2
> > @@ -114,6 +114,13 @@ other threads in the same process.
> >  Since a file descriptor may be reused,
> >  there are some obscure race conditions
> >  that may cause unintended side effects.
> > +.PP
> > +When dealing with sockets, you have to be sure that there is no
> > +.BR recv (2)
> > +still blocking on it on another thread, otherwise it might block forever,
> > +since no more messages will be send via the socket. Be sure to use
> > +.BR shutdown (2)
> > +to shut down all parts the connection before closing the socket.
> >  .\" Date: Tue, 4 Sep 2007 13:57:35 +0200
> >  .\" From: Fredrik Noring <noring-zgYzP9v7iJcdnm+yROfE0A@public.gmane.org>
> >  .\" One such race involves signals and ERESTARTSYS. If a file descriptor
> > 
> 
> 

-- 
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] close.2: Mention a need of shutdown before closing socket
       [not found] ` <1314620800-15587-1-git-send-email-lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
  2011-09-01  8:43   ` Lukas Czerner
@ 2011-09-10 13:59   ` Michael Kerrisk
       [not found]     ` <CAKgNAkj_+h9njO6ffVg_1ckpBLdyHM=6iZyyU1jpL5sY6Zrs2Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  2011-10-07  6:28   ` Michael Kerrisk
  2 siblings, 1 reply; 11+ messages in thread
From: Michael Kerrisk @ 2011-09-10 13:59 UTC (permalink / raw)
  To: Lukas Czerner; +Cc: linux-man-u79uwXL29TY76Z2rM5mHXA

Hi Lukas,

On Mon, Aug 29, 2011 at 2:26 PM, Lukas Czerner <lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> wrote:
> When dealing with sockets, we have to be sure that there is no recv
> still blocking on it on another thread, otherwise it might block forever,
> since no more messages will be send via the socket. We should advice to
> use shutdown before closing socket.

As far as I know, in the usual case, a simple close() is sufficient.
Can you say some more about how you determined the above statement and
in what circumstances it applies?

Thanks,

Michael


> Signed-off-by: Lukas Czerner <lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
> ---
>  man2/close.2 |    7 +++++++
>  1 files changed, 7 insertions(+), 0 deletions(-)
>
> diff --git a/man2/close.2 b/man2/close.2
> index a2a2961..02472f3 100644
> --- a/man2/close.2
> +++ b/man2/close.2
> @@ -114,6 +114,13 @@ other threads in the same process.
>  Since a file descriptor may be reused,
>  there are some obscure race conditions
>  that may cause unintended side effects.
> +.PP
> +When dealing with sockets, you have to be sure that there is no
> +.BR recv (2)
> +still blocking on it on another thread, otherwise it might block forever,
> +since no more messages will be send via the socket. Be sure to use
> +.BR shutdown (2)
> +to shut down all parts the connection before closing the socket.
>  .\" Date: Tue, 4 Sep 2007 13:57:35 +0200
>  .\" From: Fredrik Noring <noring-zgYzP9v7iJcdnm+yROfE0A@public.gmane.org>
>  .\" One such race involves signals and ERESTARTSYS. If a file descriptor
> --
> 1.7.4.4
>
>



-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Author of "The Linux Programming Interface"; http://man7.org/tlpi/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] close.2: Mention a need of shutdown before closing socket
       [not found]     ` <CAKgNAkj_+h9njO6ffVg_1ckpBLdyHM=6iZyyU1jpL5sY6Zrs2Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2011-09-12 10:25       ` Lukas Czerner
       [not found]         ` <alpine.LFD.2.00.1109121215310.3888-D5fXUWdM4RC5eBp5B7zZTR/sF2h8X+2i0E9HWUfgJXw@public.gmane.org>
  0 siblings, 1 reply; 11+ messages in thread
From: Lukas Czerner @ 2011-09-12 10:25 UTC (permalink / raw)
  To: Michael Kerrisk; +Cc: Lukas Czerner, linux-man-u79uwXL29TY76Z2rM5mHXA

On Sat, 10 Sep 2011, Michael Kerrisk wrote:

> Hi Lukas,
> 
> On Mon, Aug 29, 2011 at 2:26 PM, Lukas Czerner <lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> wrote:
> > When dealing with sockets, we have to be sure that there is no recv
> > still blocking on it on another thread, otherwise it might block forever,
> > since no more messages will be send via the socket. We should advice to
> > use shutdown before closing socket.
> 
> As far as I know, in the usual case, a simple close() is sufficient.
> Can you say some more about how you determined the above statement and
> in what circumstances it applies?
> 
> Thanks,
> 
> Michael

Simply calling close() is not sufficient if the recv, or read is
blocking on another thread. That is because the recv or read will not be
notified that the descriptor has been closed. This can only be done via
shutdown.

We actually have an customer complaining about this behaviour being
different than on Solaris where simple close() is sufficient to kill
recv() or read() on another thread. It is simple the way it is in Linux,
but it should at least be documented.

I am not sure how to describe the problem more, so I have written really
simple (and stupid) example, where shutdown should be used.

Thanks!
-Lukas


#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/un.h>
#include <unistd.h>
#include <pthread.h>

#define BUFSIZE 1024

void *close_socket(void *arg) {
	int sockfd = *(int *)arg;

	sleep(3);
	printf("Thread: closing socket %d\n", sockfd);

/*
 * Shutdown should be used before close, otherwise the recv() in
 * client() will block forever.
 */

/*	shutdown(sockfd, SHUT_RDWR); */
	close(sockfd);
}

int client(void) {
	int sockfd;
	int len;
	struct sockaddr_un address;
	int ret;
	char *buffer=malloc(BUFSIZE);
	pthread_t thread;

	sockfd = socket(AF_UNIX, SOCK_STREAM, 0);

	address.sun_family = AF_UNIX;
	strcpy(address.sun_path, "server_socket");
	len = sizeof(address);

	ret = connect(sockfd, (struct sockaddr *)&address, len);
	if (ret == -1) {
		perror("connect");
		return 1;
	}
	printf("client connected\n");

	ret = pthread_create(&thread, NULL, close_socket, (void *)&sockfd);
	if (ret != 0) {
		perror("Creating thread failed");
		return 1;
	}

	while (1) {
		ret = recv(sockfd,buffer,BUFSIZE,0);
		if (ret < 0) {
			perror("recv");
			return 1;
		}
		printf("Data received: %s\n", buffer);
		sleep(1);
	}

	close(sockfd);
	return 0;
}

int server(void) {
	char *message="This is the message I am sending to you";
	struct sockaddr_un server_addr, client_addr;
	int server_sockfd, client_sockfd;
	int server_len, client_len;
	int ret;

	unlink("server_socket");
	server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);

	server_addr.sun_family = AF_UNIX;
	strcpy(server_addr.sun_path, "server_socket");
	server_len = sizeof(server_addr);
	bind(server_sockfd, (struct sockaddr *)&server_addr, server_len);

	listen(server_sockfd, 5);

	client_len = sizeof(client_addr);
	client_sockfd = accept(server_sockfd,
		(struct sockaddr *)&client_addr, &client_len);

	printf("Server: sending data...\n");
	ret = send(client_sockfd ,message,strlen(message),0);
	if (ret < 0) {
		perror("send");
		return 1;
	}

	/* simulate running server by not closing the client_socket socket */
	return 0;
}

int main() {
	pid_t pid;
	int n;
	
	pid = fork();
	if (pid < 0) {
		perror("fork failed");
		exit(1);
	}
	if (pid > 0) {
		printf(" - starting server\n");
		server();
		printf(" - exiting server\n");
		wait();
	} else {
		sleep(1);
		printf(" - starting client\n");
		client();
		printf(" - exiting client\n");
	}
}
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] close.2: Mention a need of shutdown before closing socket
       [not found]         ` <alpine.LFD.2.00.1109121215310.3888-D5fXUWdM4RC5eBp5B7zZTR/sF2h8X+2i0E9HWUfgJXw@public.gmane.org>
@ 2011-09-22  9:36           ` Lukas Czerner
  0 siblings, 0 replies; 11+ messages in thread
From: Lukas Czerner @ 2011-09-22  9:36 UTC (permalink / raw)
  To: Lukas Czerner; +Cc: Michael Kerrisk, linux-man-u79uwXL29TY76Z2rM5mHXA

On Mon, 12 Sep 2011, Lukas Czerner wrote:

> On Sat, 10 Sep 2011, Michael Kerrisk wrote:
> 
> > Hi Lukas,
> > 
> > On Mon, Aug 29, 2011 at 2:26 PM, Lukas Czerner <lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> wrote:
> > > When dealing with sockets, we have to be sure that there is no recv
> > > still blocking on it on another thread, otherwise it might block forever,
> > > since no more messages will be send via the socket. We should advice to
> > > use shutdown before closing socket.
> > 
> > As far as I know, in the usual case, a simple close() is sufficient.
> > Can you say some more about how you determined the above statement and
> > in what circumstances it applies?
> > 
> > Thanks,
> > 
> > Michael
> 
> Simply calling close() is not sufficient if the recv, or read is
> blocking on another thread. That is because the recv or read will not be
> notified that the descriptor has been closed. This can only be done via
> shutdown.
> 
> We actually have an customer complaining about this behaviour being
> different than on Solaris where simple close() is sufficient to kill
> recv() or read() on another thread. It is simple the way it is in Linux,
> but it should at least be documented.
> 
> I am not sure how to describe the problem more, so I have written really
> simple (and stupid) example, where shutdown should be used.
> 
> Thanks!
> -Lukas

ping

> 
> 
> #include <sys/types.h>
> #include <sys/socket.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/un.h>
> #include <unistd.h>
> #include <pthread.h>
> 
> #define BUFSIZE 1024
> 
> void *close_socket(void *arg) {
> 	int sockfd = *(int *)arg;
> 
> 	sleep(3);
> 	printf("Thread: closing socket %d\n", sockfd);
> 
> /*
>  * Shutdown should be used before close, otherwise the recv() in
>  * client() will block forever.
>  */
> 
> /*	shutdown(sockfd, SHUT_RDWR); */
> 	close(sockfd);
> }
> 
> int client(void) {
> 	int sockfd;
> 	int len;
> 	struct sockaddr_un address;
> 	int ret;
> 	char *buffer=malloc(BUFSIZE);
> 	pthread_t thread;
> 
> 	sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
> 
> 	address.sun_family = AF_UNIX;
> 	strcpy(address.sun_path, "server_socket");
> 	len = sizeof(address);
> 
> 	ret = connect(sockfd, (struct sockaddr *)&address, len);
> 	if (ret == -1) {
> 		perror("connect");
> 		return 1;
> 	}
> 	printf("client connected\n");
> 
> 	ret = pthread_create(&thread, NULL, close_socket, (void *)&sockfd);
> 	if (ret != 0) {
> 		perror("Creating thread failed");
> 		return 1;
> 	}
> 
> 	while (1) {
> 		ret = recv(sockfd,buffer,BUFSIZE,0);
> 		if (ret < 0) {
> 			perror("recv");
> 			return 1;
> 		}
> 		printf("Data received: %s\n", buffer);
> 		sleep(1);
> 	}
> 
> 	close(sockfd);
> 	return 0;
> }
> 
> int server(void) {
> 	char *message="This is the message I am sending to you";
> 	struct sockaddr_un server_addr, client_addr;
> 	int server_sockfd, client_sockfd;
> 	int server_len, client_len;
> 	int ret;
> 
> 	unlink("server_socket");
> 	server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
> 
> 	server_addr.sun_family = AF_UNIX;
> 	strcpy(server_addr.sun_path, "server_socket");
> 	server_len = sizeof(server_addr);
> 	bind(server_sockfd, (struct sockaddr *)&server_addr, server_len);
> 
> 	listen(server_sockfd, 5);
> 
> 	client_len = sizeof(client_addr);
> 	client_sockfd = accept(server_sockfd,
> 		(struct sockaddr *)&client_addr, &client_len);
> 
> 	printf("Server: sending data...\n");
> 	ret = send(client_sockfd ,message,strlen(message),0);
> 	if (ret < 0) {
> 		perror("send");
> 		return 1;
> 	}
> 
> 	/* simulate running server by not closing the client_socket socket */
> 	return 0;
> }
> 
> int main() {
> 	pid_t pid;
> 	int n;
> 	
> 	pid = fork();
> 	if (pid < 0) {
> 		perror("fork failed");
> 		exit(1);
> 	}
> 	if (pid > 0) {
> 		printf(" - starting server\n");
> 		server();
> 		printf(" - exiting server\n");
> 		wait();
> 	} else {
> 		sleep(1);
> 		printf(" - starting client\n");
> 		client();
> 		printf(" - exiting client\n");
> 	}
> }
> 

-- 
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] close.2: Mention a need of shutdown before closing socket
       [not found] ` <1314620800-15587-1-git-send-email-lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
  2011-09-01  8:43   ` Lukas Czerner
  2011-09-10 13:59   ` Michael Kerrisk
@ 2011-10-07  6:28   ` Michael Kerrisk
       [not found]     ` <CAKgNAkjDZufWp-H7NeSRT40FGBRCkMuisesbGcdJnD94OZucUA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  2 siblings, 1 reply; 11+ messages in thread
From: Michael Kerrisk @ 2011-10-07  6:28 UTC (permalink / raw)
  To: Lukas Czerner; +Cc: linux-man-u79uwXL29TY76Z2rM5mHXA

Lukas,

On Mon, Aug 29, 2011 at 2:26 PM, Lukas Czerner <lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> wrote:
> When dealing with sockets, we have to be sure that there is no recv
> still blocking on it on another thread, otherwise it might block forever,
> since no more messages will be send via the socket. We should advice to
> use shutdown before closing socket.

I'm looking into this now. The picture may be more complex than this.
Tell me, so you have a Solaris system available for testing?

Thanks,

Michael


> Signed-off-by: Lukas Czerner <lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
> ---
>  man2/close.2 |    7 +++++++
>  1 files changed, 7 insertions(+), 0 deletions(-)
>
> diff --git a/man2/close.2 b/man2/close.2
> index a2a2961..02472f3 100644
> --- a/man2/close.2
> +++ b/man2/close.2
> @@ -114,6 +114,13 @@ other threads in the same process.
>  Since a file descriptor may be reused,
>  there are some obscure race conditions
>  that may cause unintended side effects.
> +.PP
> +When dealing with sockets, you have to be sure that there is no
> +.BR recv (2)
> +still blocking on it on another thread, otherwise it might block forever,
> +since no more messages will be send via the socket. Be sure to use
> +.BR shutdown (2)
> +to shut down all parts the connection before closing the socket.
>  .\" Date: Tue, 4 Sep 2007 13:57:35 +0200
>  .\" From: Fredrik Noring <noring-zgYzP9v7iJcdnm+yROfE0A@public.gmane.org>
>  .\" One such race involves signals and ERESTARTSYS. If a file descriptor
> --
> 1.7.4.4
>
>



-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Author of "The Linux Programming Interface"; http://man7.org/tlpi/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] close.2: Mention a need of shutdown before closing socket
       [not found]     ` <CAKgNAkjDZufWp-H7NeSRT40FGBRCkMuisesbGcdJnD94OZucUA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2011-10-07  7:09       ` Lukas Czerner
       [not found]         ` <alpine.LFD.2.00.1110070906440.7183-D5fXUWdM4RC5eBp5B7zZTR/sF2h8X+2i0E9HWUfgJXw@public.gmane.org>
  0 siblings, 1 reply; 11+ messages in thread
From: Lukas Czerner @ 2011-10-07  7:09 UTC (permalink / raw)
  To: Michael Kerrisk; +Cc: Lukas Czerner, linux-man-u79uwXL29TY76Z2rM5mHXA

[-- Attachment #1: Type: TEXT/PLAIN, Size: 1949 bytes --]

On Fri, 7 Oct 2011, Michael Kerrisk wrote:

> Lukas,
> 
> On Mon, Aug 29, 2011 at 2:26 PM, Lukas Czerner <lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> wrote:
> > When dealing with sockets, we have to be sure that there is no recv
> > still blocking on it on another thread, otherwise it might block forever,
> > since no more messages will be send via the socket. We should advice to
> > use shutdown before closing socket.
> 
> I'm looking into this now. The picture may be more complex than this.
> Tell me, so you have a Solaris system available for testing?
> 
> Thanks,

Hi Michael,

Thanks for looking into this. Unfortunately I do not have any Solaris system
for testing, that was a customer report. His complaint was mainly not because
the behaviour on Linux differs, but because this was not documented anywhere.

Thanks!
-Lukas

> 
> Michael
> 
> 
> > Signed-off-by: Lukas Czerner <lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
> > ---
> >  man2/close.2 |    7 +++++++
> >  1 files changed, 7 insertions(+), 0 deletions(-)
> >
> > diff --git a/man2/close.2 b/man2/close.2
> > index a2a2961..02472f3 100644
> > --- a/man2/close.2
> > +++ b/man2/close.2
> > @@ -114,6 +114,13 @@ other threads in the same process.
> >  Since a file descriptor may be reused,
> >  there are some obscure race conditions
> >  that may cause unintended side effects.
> > +.PP
> > +When dealing with sockets, you have to be sure that there is no
> > +.BR recv (2)
> > +still blocking on it on another thread, otherwise it might block forever,
> > +since no more messages will be send via the socket. Be sure to use
> > +.BR shutdown (2)
> > +to shut down all parts the connection before closing the socket.
> >  .\" Date: Tue, 4 Sep 2007 13:57:35 +0200
> >  .\" From: Fredrik Noring <noring-zgYzP9v7iJcdnm+yROfE0A@public.gmane.org>
> >  .\" One such race involves signals and ERESTARTSYS. If a file descriptor
> > --
> > 1.7.4.4
> >
> >
> 
> 
> 
> 

-- 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] close.2: Mention a need of shutdown before closing socket
       [not found]         ` <alpine.LFD.2.00.1110070906440.7183-D5fXUWdM4RC5eBp5B7zZTR/sF2h8X+2i0E9HWUfgJXw@public.gmane.org>
@ 2011-10-07 10:34           ` Michael Kerrisk
       [not found]             ` <CAKgNAki_fBFgoOHwsBbhocpzRt3_Zges49mLQwpkHfA+NYcxHA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  0 siblings, 1 reply; 11+ messages in thread
From: Michael Kerrisk @ 2011-10-07 10:34 UTC (permalink / raw)
  To: Lukas Czerner; +Cc: linux-man-u79uwXL29TY76Z2rM5mHXA

Hi Lukas

On Fri, Oct 7, 2011 at 9:09 AM, Lukas Czerner <lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> wrote:
> On Fri, 7 Oct 2011, Michael Kerrisk wrote:
>
>> Lukas,
>>
>> On Mon, Aug 29, 2011 at 2:26 PM, Lukas Czerner <lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> wrote:
>> > When dealing with sockets, we have to be sure that there is no recv
>> > still blocking on it on another thread, otherwise it might block forever,
>> > since no more messages will be send via the socket. We should advice to
>> > use shutdown before closing socket.
>>
>> I'm looking into this now. The picture may be more complex than this.
>> Tell me, so you have a Solaris system available for testing?
>>
>> Thanks,
>
> Hi Michael,
>
> Thanks for looking into this. Unfortunately I do not have any Solaris system
> for testing, that was a customer report. His complaint was mainly not because
> the behaviour on Linux differs, but because this was not documented anywhere.

Okay. I will see if I can find a test system somewhere. (The system I
used to use seems to have gone away.)

Just FYI: I confirmed what you are seeing, but the issue seems more
general: basically, closing a file descriptor in one thread while
reading in another thread does not cause the read operation to
terminate (it will still read data if/when it becomes available).

By the way, I'm wondering about creating a small repo of test
programs. I might like to add a modified version of your test program
to that repo. Can you put that code under a Free License and supply a
copyright?

Thanks,

Michael



>> > Signed-off-by: Lukas Czerner <lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
>> > ---
>> >  man2/close.2 |    7 +++++++
>> >  1 files changed, 7 insertions(+), 0 deletions(-)
>> >
>> > diff --git a/man2/close.2 b/man2/close.2
>> > index a2a2961..02472f3 100644
>> > --- a/man2/close.2
>> > +++ b/man2/close.2
>> > @@ -114,6 +114,13 @@ other threads in the same process.
>> >  Since a file descriptor may be reused,
>> >  there are some obscure race conditions
>> >  that may cause unintended side effects.
>> > +.PP
>> > +When dealing with sockets, you have to be sure that there is no
>> > +.BR recv (2)
>> > +still blocking on it on another thread, otherwise it might block forever,
>> > +since no more messages will be send via the socket. Be sure to use
>> > +.BR shutdown (2)
>> > +to shut down all parts the connection before closing the socket.
>> >  .\" Date: Tue, 4 Sep 2007 13:57:35 +0200
>> >  .\" From: Fredrik Noring <noring-zgYzP9v7iJcdnm+yROfE0A@public.gmane.org>
>> >  .\" One such race involves signals and ERESTARTSYS. If a file descriptor
>> > --
>> > 1.7.4.4
>> >
>> >
>>
>>
>>
>>
>
> --



-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Author of "The Linux Programming Interface"; http://man7.org/tlpi/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] close.2: Mention a need of shutdown before closing socket
       [not found]             ` <CAKgNAki_fBFgoOHwsBbhocpzRt3_Zges49mLQwpkHfA+NYcxHA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2011-10-07 11:03               ` Lukas Czerner
  0 siblings, 0 replies; 11+ messages in thread
From: Lukas Czerner @ 2011-10-07 11:03 UTC (permalink / raw)
  To: Michael Kerrisk; +Cc: Lukas Czerner, linux-man-u79uwXL29TY76Z2rM5mHXA

On Fri, 7 Oct 2011, Michael Kerrisk wrote:

> Hi Lukas
> 
> On Fri, Oct 7, 2011 at 9:09 AM, Lukas Czerner <lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> wrote:
> > On Fri, 7 Oct 2011, Michael Kerrisk wrote:
> >
> >> Lukas,
> >>
> >> On Mon, Aug 29, 2011 at 2:26 PM, Lukas Czerner <lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> wrote:
> >> > When dealing with sockets, we have to be sure that there is no recv
> >> > still blocking on it on another thread, otherwise it might block forever,
> >> > since no more messages will be send via the socket. We should advice to
> >> > use shutdown before closing socket.
> >>
> >> I'm looking into this now. The picture may be more complex than this.
> >> Tell me, so you have a Solaris system available for testing?
> >>
> >> Thanks,
> >
> > Hi Michael,
> >
> > Thanks for looking into this. Unfortunately I do not have any Solaris system
> > for testing, that was a customer report. His complaint was mainly not because
> > the behaviour on Linux differs, but because this was not documented anywhere.
> 
> Okay. I will see if I can find a test system somewhere. (The system I
> used to use seems to have gone away.)
> 
> Just FYI: I confirmed what you are seeing, but the issue seems more
> general: basically, closing a file descriptor in one thread while
> reading in another thread does not cause the read operation to
> terminate (it will still read data if/when it becomes available).
> 
> By the way, I'm wondering about creating a small repo of test
> programs. I might like to add a modified version of your test program
> to that repo. Can you put that code under a Free License and supply a
> copyright?
> 
> Thanks,
> 
> Michael

Hi Michael,

no problem, here is the program, feel free to update it as you like.
Thanks for looking into this.

-Lukas

---

/**
 * Copyright 2011 (C) Red Hat, Inc., Lukas Czerner <lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/un.h>
#include <unistd.h>
#include <pthread.h>

#define BUFSIZE 1024

void *close_socket(void *arg) {
	int sockfd = *(int *)arg;

	sleep(3);
	printf("Thread: closing socket %d\n", sockfd);
//	shutdown(sockfd, SHUT_RDWR);
	close(sockfd);
}

int client(void) {
	int sockfd;
	int len;
	struct sockaddr_un address;
	int ret;
	char *buffer=malloc(BUFSIZE);
	pthread_t thread;

	sockfd = socket(AF_UNIX, SOCK_STREAM, 0);

	address.sun_family = AF_UNIX;
	strcpy(address.sun_path, "server_socket");
	len = sizeof(address);

	ret = connect(sockfd, (struct sockaddr *)&address, len);
	if (ret == -1) {
		perror("connect");
		return 1;
	}
	printf("client connected\n");

	ret = pthread_create(&thread, NULL, close_socket, (void *)&sockfd);
	if (ret != 0) {
		perror("Creating thread failed");
		return 1;
	}

	while (1) {
		ret = recv(sockfd,buffer,BUFSIZE,0);
		if (ret < 0) {
			perror("recv");
			return 1;
		}
		printf("Data received: %s\n", buffer);
		sleep(1);
	}

	close(sockfd);
	return 0;
}

int server(void) {
	char *message="This is the message I am sending to you";
	struct sockaddr_un server_addr, client_addr;
	int server_sockfd, client_sockfd;
	int server_len, client_len;
	int ret;

	unlink("server_socket");
	server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);

	server_addr.sun_family = AF_UNIX;
	strcpy(server_addr.sun_path, "server_socket");
	server_len = sizeof(server_addr);
	bind(server_sockfd, (struct sockaddr *)&server_addr, server_len);

	listen(server_sockfd, 5);

	client_len = sizeof(client_addr);
	client_sockfd = accept(server_sockfd,
		(struct sockaddr *)&client_addr, &client_len);

	printf("Server: sending data...\n");
	ret = send(client_sockfd ,message,strlen(message),0);
	if (ret < 0) {
		perror("send");
		return 1;
	}

	/* simulate running server by not closing the client_socket socket */
	return 0;
}

int main() {
	pid_t pid;
	int n;
	
	pid = fork();
	if (pid < 0) {
		perror("fork failed");
		exit(1);
	}
	if (pid > 0) {
		printf(" - starting server\n");
		server();
		printf(" - exiting server\n");
		wait();
	} else {
		sleep(1);
		printf(" - starting client\n");
		client();
		printf(" - exiting client\n");
	}
}
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] close.2: Mention a need of shutdown before closing socket
@ 2013-02-12 18:41 Peter Schiffer
  0 siblings, 0 replies; 11+ messages in thread
From: Peter Schiffer @ 2013-02-12 18:41 UTC (permalink / raw)
  To: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w
  Cc: linux-man-u79uwXL29TY76Z2rM5mHXA, Lukas Czerner

ping Michael

On Fri, 7 Oct 2011, Michael Kerrisk wrote:
 > Hi Lukas
 >
 > On Fri, Oct 7, 2011 at 9:09 AM, Lukas Czerner <lczerner@> wrote:
 >> On Fri, 7 Oct 2011, Michael Kerrisk wrote:
 >>
 >>> Lukas,
 >>>
 >>> On Mon, Aug 29, 2011 at 2:26 PM, Lukas Czerner <lczerner@...> wrote:
 >>>> When dealing with sockets, we have to be sure that there is no recv
 >>>> still blocking on it on another thread, otherwise it might block
 >>>> forever, since no more messages will be send via the socket. We
 >>>> should advice to use shutdown before closing socket.
 >>>
 >>> I'm looking into this now. The picture may be more complex than
 >>> this. Tell me, so you have a Solaris system available for testing?
 >>>
 >>> Thanks,
 >>
 >> Hi Michael,
 >>
 >> Thanks for looking into this. Unfortunately I do not have any
 >> Solaris system for testing, that was a customer report. His
 >> complaint was mainly not because the behaviour on Linux differs, but
 >> because this was not documented anywhere.
 >
 > Okay. I will see if I can find a test system somewhere. (The system I
 > used to use seems to have gone away.)
 >
 > Just FYI: I confirmed what you are seeing, but the issue seems more
 > general: basically, closing a file descriptor in one thread while
 > reading in another thread does not cause the read operation to
 > terminate (it will still read data if/when it becomes available).
 >
 > By the way, I'm wondering about creating a small repo of test
 > programs. I might like to add a modified version of your test program
 > to that repo. Can you put that code under a Free License and supply a
 > copyright?
 >
 > Thanks,
 >
 > Michael

Hi Michael,

no problem, here is the program, feel free to update it as you like.
Thanks for looking into this.

-Lukas
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2013-02-12 18:41 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-29 12:26 [PATCH] close.2: Mention a need of shutdown before closing socket Lukas Czerner
     [not found] ` <1314620800-15587-1-git-send-email-lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2011-09-01  8:43   ` Lukas Czerner
     [not found]     ` <alpine.LFD.2.00.1109011043050.3665-D5fXUWdM4RC5eBp5B7zZTR/sF2h8X+2i0E9HWUfgJXw@public.gmane.org>
2011-09-07 15:54       ` Lukas Czerner
2011-09-10 13:59   ` Michael Kerrisk
     [not found]     ` <CAKgNAkj_+h9njO6ffVg_1ckpBLdyHM=6iZyyU1jpL5sY6Zrs2Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-09-12 10:25       ` Lukas Czerner
     [not found]         ` <alpine.LFD.2.00.1109121215310.3888-D5fXUWdM4RC5eBp5B7zZTR/sF2h8X+2i0E9HWUfgJXw@public.gmane.org>
2011-09-22  9:36           ` Lukas Czerner
2011-10-07  6:28   ` Michael Kerrisk
     [not found]     ` <CAKgNAkjDZufWp-H7NeSRT40FGBRCkMuisesbGcdJnD94OZucUA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-10-07  7:09       ` Lukas Czerner
     [not found]         ` <alpine.LFD.2.00.1110070906440.7183-D5fXUWdM4RC5eBp5B7zZTR/sF2h8X+2i0E9HWUfgJXw@public.gmane.org>
2011-10-07 10:34           ` Michael Kerrisk
     [not found]             ` <CAKgNAki_fBFgoOHwsBbhocpzRt3_Zges49mLQwpkHfA+NYcxHA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-10-07 11:03               ` Lukas Czerner
2013-02-12 18:41 Peter Schiffer

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.