4

I'm trying to use pv rather than rsync's builtin --bwlimit to limit the bandwidth of an rsync transfer because --bwlimit is implemented in an eventually-consistent way, and my transfers are so short that the limit never actually kicks in. This nearly works, but the system doesn't quit properly.

For those that aren't familiar, pv is the pipe-viewer utility, but it has a little-known capability to limit the rate of data passing through a pipe.


Edit to add: I believe I've found the root problem. Normally a process in a pipeline stops either because a) the stdin closes or b) it gets SIGPIPE while writing to stdout. Neither of these happen because a) rsync never closes the socket on stdin (apparently) and b) it never attempts to write to stdout, because rsync stops sending data on stdin at the same instant that stdout closes.

What I need is some kind of wrapping command that causes pv to shut down as soon as stdout closes, regardless of whether stdin is still sending data.


Demonstration:

head -c 100000 /dev/urandom > random4.bin
ls -lh random4.bin
>    -rw-r--r-- 1 buck users 98K 2014-07-07 11:01 random4.bin

export RSYNC_RSH='sh -c '\''pv -qL10k | ssh "$@" | pv -qL11k'\'' ssh'

rsync -av --progress localhost:$PWD/random4.bin random5.bin
>    receiving incremental file list
>    random4.bin
>          100000 100%   17.47kB/s    0:00:05 (xfer#1, to-check=0/1)
>    
>    sent 30 bytes  received 100109 bytes  10540.95 bytes/sec
>    total size is 100000  speedup is 1.00
>    (never exits)

At this point I have to ctrl-c to regain control of my terminal. The problem is that the pv that sits at the input of ssh never exits, even though its output has closed.

A bit of debugging:

pstree -lap 29532
>     rsync,29532 -av --progress localhost:/nail/home/buck/tmp/random4.bin random5.bin
>      └─sh,29533 -c pv -qL10k | ssh "$@" | pv -qL11k ssh localhost rsync --server --sender -vlogDtpre.iLsf . /nail/home/buck/tmp/random4.bin
>          └─pv,29534 -qL10k


lsof -p 29532
>    COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF      NODE NAME
>    rsync   29532 buck  cwd    DIR              252,0     4096   7345851 /nail/home/buck/tmp
>    rsync   29532 buck  rtd    DIR                9,1     4096         2 /
>    rsync   29532 buck  txt    REG                9,1   405368     94575 /usr/bin/rsync
>    rsync   29532 buck  mem    REG                9,1    51712    554006 /lib/libnss_files-2.11.1.so
>    rsync   29532 buck  mem    REG                9,1    43552    553997 /lib/libnss_nis-2.11.1.so
>    rsync   29532 buck  mem    REG                9,1    97256    553992 /lib/libnsl-2.11.1.so
>    rsync   29532 buck  mem    REG                9,1    35712    553831 /lib/libnss_compat-2.11.1.so
>    rsync   29532 buck  mem    REG                9,1    18704    553802 /lib/libattr.so.1.1.0
>    rsync   29532 buck  mem    REG                9,1  1584520    554000 /lib/libc-2.11.1.so
>    rsync   29532 buck  mem    REG                9,1    44008    554064 /lib/libpopt.so.0.0.0
>    rsync   29532 buck  mem    REG                9,1    31208    553800 /lib/libacl.so.1.1.0
>    rsync   29532 buck  mem    REG                9,1   136936    554003 /lib/ld-2.11.1.so
>    rsync   29532 buck  mem    REG                9,1   256324    172607 /usr/lib/locale/en_US.utf8/LC_CTYPE
>    rsync   29532 buck  mem    REG                9,1    26048     93726 /usr/lib/gconv/gconv-modules.cache
>    rsync   29532 buck    0u   CHR             136,10      0t0        13 /dev/pts/10
>    rsync   29532 buck    1u   CHR             136,10      0t0        13 /dev/pts/10
>    rsync   29532 buck    2u   CHR             136,10      0t0        13 /dev/pts/10
>    rsync   29532 buck    3u  unix 0xffff882026de4100      0t0 349807593 socket
>    rsync   29532 buck    4u  unix 0xffff880d79016e80      0t0 349851596 socket


lsof -p 29533
>    COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF      NODE NAME
>    sh      29533 buck  cwd    DIR              252,0     4096   7345851 /nail/home/buck/tmp
>    sh      29533 buck  rtd    DIR                9,1     4096         2 /
>    sh      29533 buck  txt    REG                9,1   101608    504936 /bin/dash
>    sh      29533 buck  mem    REG                9,1  1584520    554000 /lib/libc-2.11.1.so
>    sh      29533 buck  mem    REG                9,1   136936    554003 /lib/ld-2.11.1.so
>    sh      29533 buck    0u  unix 0xffff880d79014100      0t0 349851595 socket
>    sh      29533 buck    1u  unix 0xffff880d79014e00      0t0 349851598 socket
>    sh      29533 buck    2u   CHR             136,10      0t0        13 /dev/pts/10

lsof -p 29534
>    COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF      NODE NAME
>    pv      29534 buck  cwd    DIR              252,0     4096   7345851 /nail/home/buck/tmp
>    pv      29534 buck  rtd    DIR                9,1     4096         2 /
>    pv      29534 buck  txt    REG                9,1    35336    106025 /usr/bin/pv
>    pv      29534 buck  mem    REG                9,1  1584520    554000 /lib/libc-2.11.1.so
>    pv      29534 buck  mem    REG                9,1   136936    554003 /lib/ld-2.11.1.so
>    pv      29534 buck  mem    REG                9,1   256324    172607 /usr/lib/locale/en_US.utf8/LC_CTYPE
>    pv      29534 buck  mem    REG                9,1       54    172608 /usr/lib/locale/en_US.utf8/LC_NUMERIC
>    pv      29534 buck  mem    REG                9,1     2454    172707 /usr/lib/locale/en_US.utf8/LC_TIME
>    pv      29534 buck  mem    REG                9,1  1170770    172610 /usr/lib/locale/en_US.utf8/LC_COLLATE
>    pv      29534 buck  mem    REG                9,1      286    172708 /usr/lib/locale/en_US.utf8/LC_MONETARY
>    pv      29534 buck  mem    REG                9,1       57    172710 /usr/lib/locale/en_US.utf8/LC_MESSAGES/SYS_LC_MESSAGES
>    pv      29534 buck  mem    REG                9,1       34    172614 /usr/lib/locale/en_US.utf8/LC_PAPER
>    pv      29534 buck  mem    REG                9,1       77    172615 /usr/lib/locale/en_US.utf8/LC_NAME
>    pv      29534 buck  mem    REG                9,1      155    172711 /usr/lib/locale/en_US.utf8/LC_ADDRESS
>    pv      29534 buck  mem    REG                9,1       59    172712 /usr/lib/locale/en_US.utf8/LC_TELEPHONE
>    pv      29534 buck  mem    REG                9,1       23    172618 /usr/lib/locale/en_US.utf8/LC_MEASUREMENT
>    pv      29534 buck  mem    REG                9,1    26048     93726 /usr/lib/gconv/gconv-modules.cache
>    pv      29534 buck  mem    REG                9,1      373    172713 /usr/lib/locale/en_US.utf8/LC_IDENTIFICATION
>    pv      29534 buck    0u  unix 0xffff880d79014100      0t0 349851595 socket
>    pv      29534 buck    1w  FIFO                0,8      0t0 349847461 pipe
>    pv      29534 buck    2u   CHR             136,10      0t0        13 /dev/pts/10
2

It seems that pv is waiting for data from rsync, and rsync is waiting for data too (stuck in select()) and not closing the input to pv. So it's a deadlock. Same happens when you substitute pv with something else (like dd). It seems that those commands just don't behave like rsync expects them to.

Haven't found a workaround short of killing everything:

export RSYNC_RSH="sh -c 'pv -qL10k | ssh \"\$@\" | (pv -qL11k; kill \$\$)' ssh"

kill is not a solution I'd be happy with. But I haven't found another.

Of course using $$ was the lazy way out, should kill pv instead but how to obtain the pid in a oneliner? pidof just gives any pv pids. Querying their respective parent pids seemed a bit odd for a oneliner.

Looking forward to a real solution... ;)

2
  • While not a true 'solution', this is a fine workaround. Thanks! – bukzor Jul 8 '14 at 15:36
  • How come the same doesn't happen with RSYNC_RSH='ssh' ? It seems to me that all the same conditions would occur. In particular, does ssh stop before its stdin closes? – bukzor Jul 8 '14 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.