[asio] boost::asio::read with empty input vector leads to an infinite loop

Hello, In the code bellow I have forgotten to set the input vector (request) size. Is the code really supposed to react as an infinite loop (no errors, length is always 0, boost::asio::read returns immediately) or such behavior is a defect? for (;;) { std::vector<uint8_t> request; boost::system::error_code error; size_t length = boost::asio::read(sock, boost::asio::buffer(request), error); if (error == boost::asio::error::eof) break; else if (error) throw my_exception(L"Socket error"); ... } Thank you in advance, -- Michael Kochetkov

On Thu, Jul 12, 2012 at 10:20 PM, Michael Kochetkov <michael.kv@gmail.com> wrote:
Hello, In the code bellow I have forgotten to set the input vector (request) size. Is the code really supposed to react as an infinite loop (no errors, length is always 0, boost::asio::read returns immediately)
Probably. What else would you expect?
or such behavior is a defect?
Olaf

Hello, In the code bellow I have forgotten to set the input vector (request) size. Is the code really supposed to react as an infinite loop (no errors, length is always 0, boost::asio::read returns immediately)
Probably. What else would you expect? I expect someone from asio authers to tell me if the behavior I have described shall be considered as normal or defective. We understand we shall reconsider our approach to using asio but I believe we need this very additional information to do it the proper way. I would prefer a error message or crash because it is logically to suppose the asio would resize the vector or emit a error because it cannot handle situation properly (IMO). The concern behind the issue is working time and money -- it is really tricky for inexperienced boost users to figure out the culprit of the behavior. The program does not crash, it reports no errors and it just does not work.
-- Michael Kochetkov

On Fri, Jul 13, 2012 at 2:19 PM, Michael Kochetkov <michael.kv@gmail.com> wrote:
I would prefer a error message or crash because it is logically to suppose the asio would resize the vector or emit a error because it cannot handle situation properly (IMO). The concern behind the issue is working time and money -- it is really tricky for inexperienced boost users to figure out the culprit of the behavior. The program does not crash, it reports no errors and it just does not work.
It seems like the documentation already addresses both of these questions. For boost::asio::read() it says "The call will block until one of the following conditions is true: 1) The supplied buffers are full. That is, the bytes transferred is equal to the sum of the buffer sizes..." For an empty vector, that's always going to be the case, so it will never block. As for supposing that the buffer might resize the vector for you, ASIO has no idea (for example) how large you'd be willing to let it grow. The docs specifically say that buffer(std::vector<...>) is equivalent to: mutable_buffers_1( data.size() ? &data[0] : 0, data.size() * sizeof(PodType)); which shows that it's definitely not going to manipulate the container itself. -- Matthew L. Creech

I would prefer a error message or crash because it is logically to suppose the asio would resize the vector or emit a error because it cannot handle situation properly (IMO). The concern behind the issue is working time and money -- it is really tricky for inexperienced boost users to figure out
On Fri, Jul 13, 2012 at 2:19 PM, Michael Kochetkov <michael.kv@gmail.com> wrote: the
culprit of the behavior. The program does not crash, it reports no errors and it just does not work.
It seems like the documentation already addresses both of these questions.
For boost::asio::read() it says "The call will block until one of the following conditions is true: 1) The supplied buffers are full. That is, the bytes transferred is equal to the sum of the buffer sizes..." Ok. Thank you. I have considered this explanation but infinite loop confused me so I have decided to get an "official" opinion. We will try to be alerted to the issue.
As for supposing that the buffer might resize the vector for you, ASIO has no idea (for example) how large you'd be willing to let it grow. This is OK too. Though I bet the total majority of asio customers have to solve resizing problem somehow. I will probably add the total data size to the serialization protocol and wrap boost::asio::read function.
-- Michael Kochetkov

On 13/07/12 14:29, Matthew L. Creech wrote:
On Fri, Jul 13, 2012 at 2:19 PM, Michael Kochetkov <michael.kv@gmail.com> wrote:
I would prefer a error message or crash because it is logically to suppose the asio would resize the vector or emit a error because it cannot handle situation properly (IMO). The concern behind the issue is working time and money -- it is really tricky for inexperienced boost users to figure out the culprit of the behavior. The program does not crash, it reports no errors and it just does not work.
It seems like the documentation already addresses both of these questions.
For boost::asio::read() it says "The call will block until one of the following conditions is true: 1) The supplied buffers are full. That is, the bytes transferred is equal to the sum of the buffer sizes..."
For an empty vector, that's always going to be the case, so it will never block.
That is how the docs appear, and it may be true for this function, but at least for async_read_some with a zero-size buffer the handler won't be called until there is some data to be read (but it won't actually read it), which is a very useful feature. It may well be that read is doing the same (i.e. returning once there is data to be read). John Bytheway
participants (4)
-
John Bytheway
-
Matthew L. Creech
-
Michael Kochetkov
-
Olaf van der Spek