[libvirt] [PATCH v4 15/23] utils: Write a maximum of MAX_PIPE_WRITE_BYTES into a pipe

Stefan Berger posted 23 patches 6 years, 7 months ago
There is a newer version of this series
[libvirt] [PATCH v4 15/23] utils: Write a maximum of MAX_PIPE_WRITE_BYTES into a pipe
Posted by Stefan Berger 6 years, 7 months ago
To avoid blocking on a write on a pipe that the receiving process
does not read from, write only MAX_PIPE_WRITE_BYTES into the pipe
so that we can serve other pipes as well.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
---
 src/util/vircommand.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/util/vircommand.c b/src/util/vircommand.c
index 49347a9800..3ac51c77a8 100644
--- a/src/util/vircommand.c
+++ b/src/util/vircommand.c
@@ -86,6 +86,8 @@ struct _virCommandSendBuffer {
     size_t buflen;
     off_t offset;
 };
+/* max. number of bytes we write to pipe to avoid blocking on it */
+#define MAX_PIPE_WRITE_BYTES 1024
 
 struct _virCommand {
     int has_error; /* ENOMEM on allocation failure, -1 for anything else.  */
@@ -2251,7 +2253,7 @@ virCommandProcessIO(virCommandPtr cmd)
                 int done;
 
                 done = write(cmd->inpipe, cmd->inbuf + inoff,
-                             inlen - inoff);
+                             MIN(inlen - inoff, MAX_PIPE_WRITE_BYTES));
                 if (done < 0) {
                     if (errno == EPIPE) {
                         VIR_DEBUG("child closed stdin early, ignoring EPIPE "
-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v4 15/23] utils: Write a maximum of MAX_PIPE_WRITE_BYTES into a pipe
Posted by Daniel P. Berrangé 6 years, 7 months ago
On Thu, Jul 11, 2019 at 03:41:43PM -0400, Stefan Berger wrote:
> To avoid blocking on a write on a pipe that the receiving process
> does not read from, write only MAX_PIPE_WRITE_BYTES into the pipe
> so that we can serve other pipes as well.

This doesn't work. The pipe can have 1025 bytes free space, so you'll
write 1024 bytes first fine, and next iteration there'll be one byte
free space and you'll block. If you need to avoid blocking you must
use O_NONBLOCK on the fd. Don't try to guess how much you can write,
just write until you get EAGAIN.


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list