Search code examples
linuxsshfs

Copy file crashes terminal while sshfs disconnects


I have a perl script that runs in the background and copies files from my local system to a remote device connected through sshfs. The problem is that if the connection fails while the script is copying a file, the script completely freezes. If the connection fails anywhere else in the script, the script does not freeze. The script also cannot be killed by a kill or kill -9 command. It has become a zombie process.

Anyone who came across the same problem and has a potential solution? I've tried creating a parent script that would start a new instance of the script (using the screen command, so it would run in a whole other process), but that script also freezes :(

Some pseudo code:

while (1) {
    if (!device is mounted) {
        sleep 1;
        next;
    }
    // calculate files to copy
    if (files to copy) {
        copy_files();
    }
}

copy_files {
    foreach my $file (@files) {
        if (!device is mounted) {
            return;
        }
        // copy this file
    }
}

Edit: I have some more information about the problem. It is not related to perl at all and can be reproduced very easily. All you need is 3 terminals.

  1. Open a terminal and sshfs your device to a mountpoint (lets say this mountpoint is /home/user/mountpoint).

  2. Open a second terminal with the next command:

    while true; do mountpoint /home/user/mountpoint; sleep 1; done

    This will print is (not) a mountpoint every second.

  3. Open a third terminal with which you copy a large file to your mountpoint:

    cp /home/user/very_big_file /home/user/mountpoint

  4. Now disconnect your device from the internet (eg by turning off the wifi).

Result is that now terminal 2 and 3 are frozen. Umounting and remounting the device does not fix the problem. However if you shut down the connection gracefully (eg with fusermount -uz /home/user/mountpoint) instead of a hard disconnect, neither process crashes. If you do a disconnect without copying a file, the mountpoint command will print 'is a not mountpoint' followed by 'is a mountpoint' when the device is connected again, so it does not crash. It seems like the combination of network interruption and copying a file over ssh that causes some processes to hang.


Solution

  • The problem is in sshfs. It is outdated. Better is just to use scp.