Never send more than IOV_MAX (1024) iovecs in pwritev.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 27 Apr 2013 11:54:52 +0000 (13:54 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 27 Apr 2013 11:54:52 +0000 (13:54 +0200)
This fixes issues with “pwritev: Invalid argument” showing up
under rare circumstances. Apparently the glibc wrapper workaround
described in writev(2) does not work, or does not work for pwritev().

stream.cpp

index 0c0faba..e8d65db 100644 (file)
@@ -1,5 +1,6 @@
 #include <assert.h>
 #include <errno.h>
+#include <limits.h>
 #include <netinet/in.h>
 #include <stdlib.h>
 #include <string.h>
@@ -129,7 +130,8 @@ void Stream::put_client_to_sleep(Client *client)
 vector<iovec> collect_iovecs(const vector<iovec> &data, size_t bytes_wanted)
 {
        vector<iovec> ret;
-       for (size_t i = 0; i < data.size() && bytes_wanted > 0; ++i) {
+       size_t max_iovecs = std::min<size_t>(data.size(), IOV_MAX);
+       for (size_t i = 0; i < max_iovecs && bytes_wanted > 0; ++i) {
                if (data[i].iov_len <= bytes_wanted) {
                        // Consume the entire iovec.
                        ret.push_back(data[i]);