Only call fwrite() if select() said the fd is ready. #1132
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The current code calls fwrite() even when select_write() returns 0, ie when the write is expected to fail with EAGAIN (Resource temporarily unavailable). This happens if the TCP transmit buffer is full.
In real life I have seen select() return 0 and fwrite() succeeds anyway - I guess some room became available between the two calls.
But at one point fwrite() will fail with EAGAIN - and then an AMQPIOException occurs.
With the proposed changes fwrite() will not result in EAGAIN. Instead the result is an AMQPTimeoutException if the fd is still blocking after $this->write_timeout.
(See #1131 for a question about how the timeout should be calculated).
I don't know if this change should include any new tests.
I have used
rabbitmqctl set_vm_memory_high_watermark 0
on the RabbitMQ server to force the EAGAIN error when testing this issue.