linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH RFC net-next] af_unix: eof in recvmsg after shutdown for nonblocking dgram socket
@ 2020-03-29 18:25 Tomasz Meresiński
  0 siblings, 0 replies; only message in thread
From: Tomasz Meresiński @ 2020-03-29 18:25 UTC (permalink / raw)
  To: netdev, linux-kernel, linux-api; +Cc: davem, kuba, Tomasz Meresiński

Calling recvmsg() after shutdown(SHUT_RD) is a some kind of undocumented
behaviour. For blocking socket it just returns 0 (EOF), but for nonblocking
socket it returns -EAGAIN. It can cause some event loops to infinitely wait
for an event on this socket (https://github.com/tokio-rs/tokio/issues/1679)

Simple Python test case:
| import socket
|
| print('BLOCKING TEST')
| a = socket.socket(family=socket.AF_UNIX, type=socket.SOCK_DGRAM)
| a.shutdown(socket.SHUT_RD)
|
| result = a.recv(1)
| print('recv result ', result)
|
| a.close()
|
| print('NONBLOCKING TEST')
| type = socket.SOCK_DGRAM | socket.SOCK_NONBLOCK
| a = socket.socket(family=socket.AF_UNIX, type=type)
| a.shutdown(socket.SHUT_RD)
|
| try:
|     result = a.recv(1)
| except BlockingIOError:
|     print('Got Blocking IO Error')
| else:
|     print('recv result ', result)
|
| a.close()

Signed-off-by: Tomasz Meresiński <tomasz@meresinski.eu>
---
I'm not so sure about this patch because it can be called userspace API break. 
This sequence is now some kind of undefined behaviour - it's documented nowhere.
In the first place, I think that shutdown(SHUT_RD) should fail here as it does with AF_INET dgram socket.
On the other hand, there may be some user of this kind of shutdown() behaviour so it'd be too risky.

The problem here is that EAGAIN errno is used in event loops as we should wait for the next events indicator.
It's not true here because there won't be any new events with this socket as it's shut down.

 net/unix/af_unix.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 3385a7a0b231..9458b11289c2 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -2123,9 +2123,8 @@ static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
 
 	if (!skb) { /* implies iolock unlocked */
 		unix_state_lock(sk);
-		/* Signal EOF on disconnected non-blocking SEQPACKET socket. */
-		if (sk->sk_type == SOCK_SEQPACKET && err == -EAGAIN &&
-		    (sk->sk_shutdown & RCV_SHUTDOWN))
+		/* Signal EOF on disconnected socket. */
+		if (err == -EAGAIN && (sk->sk_shutdown & RCV_SHUTDOWN))
 			err = 0;
 		unix_state_unlock(sk);
 		goto out;
-- 
2.17.1



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2020-03-29 18:45 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-29 18:25 [PATCH RFC net-next] af_unix: eof in recvmsg after shutdown for nonblocking dgram socket Tomasz Meresiński

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).