Socket recv() hang on large message with MSG_WAITALL

后端 未结 2 1610
你的背包
你的背包 2020-12-16 01:54

I have an application that reads large files from a server and hangs frequently on a particular machine. It has worked successfully under RHEL5.2 for a long time. We have

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 02:47

    Consider these two possible rules:

    1. The receiver may wait for the sender to send more before receiving what has already been sent.

    2. The sender may wait for the receiver to receive what has already been sent before sending more.

    We can have either of these rules, but we cannot have both of these rules.

    Why? Because if the receiver is permitted to wait for the sender, that means the sender cannot wait for the receiver to receive before sending more, otherwise we deadlock. And if the sender is permitted to wait for the receiver, that means the receiver cannot wait for the sender to send before receiving more, otherwise we deadlock.

    If both of these things happen at the same time, we deadlock. The sender will not send more until the receiver receives what has already been sent, and the receiver will not receive what has already been sent unless the sender send more. Boom.

    TCP chooses rule 2 (for reasons that should be obvious). Thus it cannot support rule 1. But in your code, you are the receiver, and you are waiting for the sender to send more before you receive what has already been sent. So this will deadlock.

提交回复
热议问题