0

I have the raw socket in Python as below to receive netlink messages from linux kernel.

socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, socket.NETLINK_ROUTE)

I am doing blocking recv on this socket and want to close it from another Python thread. However, calling shutdown(socket.SHUT_RD) on this socket returns an error ([Errno95] Operation not supported)

Why is that? How can we close this socket? I am using Python 3.7

There is no mention of this behavior in https://docs.python.org/3/library/socket.html#socket

It states below:

Note close() releases the resource associated with a connection but does not necessarily close the connection immediately. If you want to close the connection in a timely fashion, call shutdown() before close().

1 Answer 1

2

If you want to close the connection in a timely fashion, call shutdown() before close()

This statement is about connected sockets. TCP sockets are connected, but UDP, Raw, Netlink sockets etc are not. That's why shutdown is not supported on such sockets. Use a simple close instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the explanations! What I actually want to do is to unblock recv() so that I can close the thread in which recv() is running. Calling close() does not seem unblock recv(). Is that expected?
@leopoodle: based on "things found on the internet" like this one it looks like a close in one thread might not wake up the recv in the other. Better use non-blocking sockets in combination with select or poll.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.