close.2: Mention a need of shutdown before closing socket
PATCH 1/1 close.2: Mention a need of shutdown before closing socket
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.
--- man2/close.2 | 7 +++++++ 1 files changed, 7 insertions(+), 0 deletions(-)
man2/close.2 +7 -0
@@ -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 <[email protected]> .\" One such race involves signals and ERESTARTSYS. If a file descriptor
Signature · 6 lines
1.7.4.4 -- To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): 1314620800-15587-1-git-send-email-lczerner-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org
Quoted 4 lines · 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.
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
Quoted 25 lines
Signed-off-by Lukas Czerner <[email protected]>--- man2/close.2 | 7 +++++++ 1 files changed, 7 insertions(+), 0 deletions(-)man2/close.2 +7
-0@@ -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 <[email protected]> .\" One such race involves signals and ERESTARTSYS. If a file descriptor
Signature · 4 lines
-- To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): alpine.LFD.2.00.1109011043050.3665-D5fXUWdM4RC5eBp5B7zZTR/[email protected]
Quoted 7 lines · Lukas Czerner
Quoted 4 lines · 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.
Quoted 7 lines
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
Quoted 28 lines
Quoted 26 lines
Signed-off-by Lukas Czerner <[email protected]>--- man2/close.2 | 7 +++++++ 1 files changed, 7 insertions(+), 0 deletions(-)man2/close.2 +7
-0@@ -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 <[email protected]> .\" One such race involves signals and ERESTARTSYS. If a file descriptor
Signature · 4 lines
-- To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Hi Lukas,
Quoted 4 lines · 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.
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
Quoted 27 lines
Signed-off-by Lukas Czerner <[email protected]>---
man2/close.2 | 7 +++++++1 files changed, 7 insertions(+), 0 deletions(-)man2/close.2 +0
-0@@ -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.
+.PPman2/close.2 +6
-0+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 <[email protected]> .\" One such race involves signals and ERESTARTSYS. If a file descriptor --
1.7.4.4
Signature · 7 lines
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 [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): CAKgNAkj_+h9njO6ffVg_1ckpBLdyHM=6iZyyU1jpL5sY6Zrs2Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Quoted 8 lines · Michael Kerrisk
Hi Lukas,
Quoted 4 lines · 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.
Quoted 7 lines
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 [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): alpine.LFD.2.00.1109121215310.3888-D5fXUWdM4RC5eBp5B7zZTR/[email protected]
Quoted 28 lines · Lukas Czerner
Quoted 8 lines · Michael Kerrisk
Hi Lukas,
Quoted 4 lines · 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.
Quoted 7 lines
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 killrecv()orread()on another thread. It is simple the way it is in Linux, but it should at least be documented.
Quoted 5 lines
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
Quoted 122 lines
#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 therecv()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"); } }
Signature · 4 lines
-- To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Lukas,
Quoted 4 lines · 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.
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
Quoted 27 lines
Signed-off-by Lukas Czerner <[email protected]>---
man2/close.2 | 7 +++++++1 files changed, 7 insertions(+), 0 deletions(-)man2/close.2 +0
-0@@ -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.
+.PPman2/close.2 +6
-0+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 <[email protected]> .\" One such race involves signals and ERESTARTSYS. If a file descriptor --
1.7.4.4
Signature · 7 lines
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 [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): CAKgNAkjDZufWp-H7NeSRT40FGBRCkMuisesbGcdJnD94OZucUA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Quoted 8 lines · Michael Kerrisk
Lukas,
Quoted 4 lines · 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.
Quoted 4 lines
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
Quoted 35 lines
Michael
Quoted 27 lines
Signed-off-by Lukas Czerner <[email protected]>---
man2/close.2 | 7 +++++++1 files changed, 7 insertions(+), 0 deletions(-)man2/close.2 +0
-0@@ -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.
+.PPman2/close.2 +6
-0+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 <[email protected]> .\" One such race involves signals and ERESTARTSYS. If a file descriptor --
1.7.4.4
A message that is not in the archive (sent off-list or not yet received): alpine.LFD.2.00.1110070906440.7183-D5fXUWdM4RC5eBp5B7zZTR/[email protected]
Hi Lukas
Quoted 13 lines · Lukas Czerner
Quoted 11 lines · Michael Kerrisk
Lukas,
Quoted 4 lines · 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.
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?
Quoted 7 lines
Quoted 1 line
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
Quoted 33 lines
Quoted 19 lines
Quoted 19 lines
Signed-off-by Lukas Czerner <[email protected]>---
man2/close.2 | 7 +++++++1 files changed, 7 insertions(+), 0 deletions(-)man2/close.2 +0
-0@@ -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.
+.PPman2/close.2 +5
-0+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)Quoted 12 lines
Quoted 8 lines
+to shut down all parts the connection before closing the socket. .\" Date: Tue, 4 Sep 2007 13:57:35 +0200
.\" From: Fredrik Noring <[email protected]> .\" One such race involves signals and ERESTARTSYS. If a file descriptor --
1.7.4.4--
Signature · 7 lines
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 [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): CAKgNAki_fBFgoOHwsBbhocpzRt3_Zges49mLQwpkHfA+NYcxHA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Quoted 32 lines · Michael Kerrisk
Hi Lukas
Quoted 13 lines · Lukas Czerner
Quoted 11 lines · Michael Kerrisk
Lukas,
Quoted 4 lines · 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.
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?
Quoted 7 lines
Quoted 1 line
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).
Quoted 8 lines
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 <[email protected]> *
* 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 [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html