Skip to content
Advertisement

Why does this function block?

I have some code that is reading data (if there is any) from a non-blocking socket and printing it, but somehow when this happens (data is received & printed) the function then blocks and does not return unless I kill the sending process (which also uses a non-blocking stream to send the data).

Obviously I have done something wrong but for the life of me I can’t see it – the socket is non-blocking and I think the continue statements are somewhat redundant. Python isn’t my main language so I’m assuming I’ve made a daft mistake in this somehow.

This is the socket setup which accepts connections & appends them to the list – I am assuming the non-blocking nature of the socket is retained on accept():

JavaScript

This is the receiver-side Python code that I believe is at fault, it’s polled from a main loop:

JavaScript

If I send some data to the socket from another process, I get (for example): <('127.0.0.1', 33196)> sent b'[HELLO:STATUS]' but then it just stops until I kill the sending process.

This is the [relevant parts of] sending-side code (GTK3 C code) which I think is correct and doesn’t block or hang at the send call:

JavaScript

So, what have I missed or done wrong in all this?

Advertisement

Answer

Well @Max’s comment led me straight to it:

JavaScript

The non-blocking state of the port isn’t inherited on accept(), adding sockfd.setblocking(False) fixed it.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement