When and why socket.send() returns 0 in python?

前端 未结 2 1836
逝去的感伤
逝去的感伤 2021-02-13 14:34

The python3 socket programming howto presents this code snippet

class MySocket:
    \"\"\"demonstration class only
      - coded for clarity, not efficiency
             


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-13 15:06

    Upon seeing the question I was somehow stunned, because a send C call can return 0 bytes and the connection is of course still alive (the socket cannot simply send more bytes at that given moment in time)

    • https://github.com/python/cpython/blob/master/Modules/socketmodule.c

    I decided to "use the source" and unless I am very wrong (which can always be and often is) this is a bug in the HOWTO.

    Chain:

    • send is an alias for sock_send
    • sock_send calls in turn sock_call
    • sock_call calls in turn sock_call_ex
    • sock_call calls in turn sock_send_impl (which has been passed down the chain starting with sock_send)

    Unwinding:

    • sock_send_impl returns true or false (1 or 0) with return (ctx->result >= 0)

    • sock_call_ex returns

      • -1 if sock_send_impl returns false
      • 0 if sock_send_impl returns true
    • sock_call returns this value transparently.

    • sock_send

      • returns NULL for a -1 (because an error has been set and an exception will be raised)

      • returns ctx->result for 0from sock_call

        And ctx->result is the number of bytes written by the C call send in sock_send_impl.

    The chain shows that if 0 bytes have been sent, there is no error and this actually is a potential real life socket situation.

    If my logic is wrong, someone please let me know.

提交回复
热议问题