RClone Mount - Only delete if not in use

Hey everyone

Is there a way to tell RClone not to delete a file if it's in use, and to keep checking until it's not in use to delete?

I have the following set up

/mnt/files1 (Rclone Mount)
/mnt/files3 (2nd Rclone Mount)
/mnt/files3 (MergerFS of the above mounts)

Now when a file is in use in /mnt/files3, Rclone deletes it from the /mnt/files1- causing the file in use to error out.

Is there a way to make Rclone only delete a file if it's not being used, and if it is being used, keeo checking until it's not in use anymore, and then delete it?

Thank you

Not sure with rclone as it doesn't know.

You could do something with 'lsof' and check if a file is open and loop until it's not and delete it.

What causes you to delete something in use ? What's the use case you are doing?

Using it for Plex.

I run two servers. 1 for downloading and post processing with Sonarr/Radarr, and one for Plex streaming.

The downloading server uploads post processed files to two seperate Rclone folders (one a tdrive the other a gdrive)

I then have a seperate machine running Plex, with the above folders Rclone mounted, and then merged with MergerFS.

What is happening is when Sonarr/Radarr updates a file, it deletes the existing file, then uploads the new file.

Problem is, when it deletes this old file, its removed from the MergerFS mount on the Plex server, even if it's being played. So the playback errors out.

It you prevented the file being deleted, that would break the Sonarr/Radarr flow though.

If the machines are remote, you'd have to do something like a check file or something to show the file was being used and that feels ugly too.

In your case machine A running Sonarr/Radarr would never know machine B is playing something as it's a completely different mount so A would show no use as it's B playing it.

Maybe someone else has a better idea on how to solve that. I've never hit that particular item myself, but I don't keep quality with names so it replaces with the same name.

Yeah I'm happy for the file to be deleted/renamed on the Sonarr/Radarr side straight away, but don't want the file to be deleted/renamed on the Plex server mount whilst it's being played back.

Then once playback has been completed and the file is no longer in use, Rclone then makes its change in the Plex side mount.

Another way to ask this is:

Can you make an Rclone mount delay a rename/delete if the file being renamed/deleted is in use?

There isn't a concept of in use really though for the mount as you are not running a command to delete it as it's done by polling since it was deleted on a remote machine.

It detects a change based on your polling interval and processes those changes that come in. By default, they are every minute.

Not quite sure how it handles a file that is gone from the remote in terms of continuing to stream it. I'd surmise it would break eventually if the source on the provider was gone, which might be what you are seeing? You could test by having a large poll interval and delete something see if it continues to play.

I'm sure I've seen an rclone mount log say that a file is open, and delayed the action required to mirror the mount until the file is no longer open.

rclone can tell a file is being written to since the file bytes change. Perhaps that is what you saw.

Sorry as I can see my terminology is pretty bad.

rclone "knows" a file is open as it created the open on the file.
You can see open files with a lsof command

I ran a copy command on a test file and removed it while it was going:

[felix@gemini GD]$ sudo lsof /GD
COMMAND   PID  USER   FD   TYPE DEVICE   SIZE/OFF                NODE NAME
bash     3465 felix  cwd    DIR   0,49          0                   1 /GD
bash    17418 felix  cwd    DIR   0,49          0                   1 /GD
cp      18009 felix  cwd    DIR   0,49          0                   1 /GD
cp      18009 felix    3r   REG   0,49 3178233856 9379287261864803092 /GD/test.mkv (deleted)
sudo    18015  root  cwd    DIR   0,49          0                   1 /GD
lsof    18016  root  cwd    DIR   0,49          0                   1 /GD
lsof    18017  root  cwd    DIR   0,49          0                   1 /GD
[felix@gemini GD]$ rm test.mkv
[felix@gemini GD]$ sudo lsof /GD
COMMAND   PID  USER   FD   TYPE DEVICE   SIZE/OFF                NODE NAME
bash     3465 felix  cwd    DIR   0,49          0                   1 /GD
bash    17418 felix  cwd    DIR   0,49          0                   1 /GD
cp      18009 felix  cwd    DIR   0,49          0                   1 /GD
cp      18009 felix    3r   REG   0,49 3178233856 9379287261864803092 /GD/test.mkv (deleted)

You'd see the deleted bit and that wouldn't go away until my command is done meaning the source command/program/etc closed the file.

My full file copied:

[felix@gemini GD]$ cp test.mkv /data
[felix@gemini GD]$ ls -al /data/test.mkv
-rw-r--r-- 1 felix felix 3178233856 Jun  7 09:18 /data/test.mkv

and the file is gone from lsof.

[felix@gemini GD]$ sudo lsof /GD
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
bash     3465 felix  cwd    DIR   0,49        0    1 /GD
bash    17418 felix  cwd    DIR   0,49        0    1 /GD
sudo    18207  root  cwd    DIR   0,49        0    1 /GD
lsof    18208  root  cwd    DIR   0,49        0    1 /GD
lsof    18209  root  cwd    DIR   0,49        0    1 /GD

Thanks for looking into it, I appreciate your help.

So basically the delete command doesn't come into affect until the file is no longer in use?

Are you using qualities or changing the names? I'd guess the playback would break if the client requested a file that isn't there anymore as the client side might not have updated.

Changing names yes.

But the issue I am experiencing is when playback is already active. The stream drops midstream.

Based on what you're saying - is that rclone should know the file is currently open, and would delay the delete/rename until after the file is closed.

No, I'm saying regardless of rclone, the OS keeps the file handle open on the deleted file until the file is close by the parent process.

rclone doesn't wait for anything.

If you can reproduce it with a debug log, the issue might rclone closes the file when it deletes it.

Hmm yeah, I just tried it again.

I started playback of a file on Server 2 (Streaming Server)- deleted the same file from Server 1 (Downloading Server), and Server 2 could no longer access that file.

It reached the point where it had transcoded ahead (a minute or so), then froze. You could see Plex trying to transcode the file more but it couldn't, because it didn't exist anymore.

I'll do some more testing tomorrow- but it seems Rclone never provided Plex with the full file before Rclone triggered the delete.

This issue will affect anyone that runs Plex on a separate server from their Sonarr/Radarr server.

I think you are mixing terms up a bit.

If you have a file and you are playing it, it'll stay there until the file is closed even if you delete it along the way.

If you have a file delete a file and something tries to access a file no longer there, it doesn't work because the file is gone. It isn't a rclone thing as your scenario would occur with a local file system as well.

If you delete a file being played and Plex has closed the file and tries to reopen it, it won't work. It's not rclone related as it is file access and deleting files.

Yeah I get what you are saying. I didn't try and reopen the file in Plex though.

It was playing on Server 2. And as it was playing I deleted the file on Server 1, and shortly afterwards playback paused on Server 2 and then errored.

I tested it three times with three different files and the same thing occured each time.

"You" might not have, but Plex may have. The debug logs would show that though. It really depends on how it uses the files.

Yeah I understand what you're saying. My only thought is Plex closes off the file when it reaches the "throttle transcode" threshold. Then tries to open the file again when it needs to transcode. Can't find the file and errors.

One way to potentially get around this is to set the transcode buffer to a much higher level, so it never throttles the transcoder.

If my family lets me I'll have a play with some logs tomorrow. What logs should I look at, rclone debug logs?

You can see open and closes like this:

2018/12/28 12:01:08 DEBUG : J. Rawls/Great Live Caper (Album)/06 Great Live Caper (Pt. 2) featuring J-Live (Clean).mp3: Open: flags=O_RDONLY
2018/12/28 12:01:09 DEBUG : &{J. Rawls/Great Live Caper (Album)/06 Great Live Caper (Pt. 2) featuring J-Live (Clean).mp3 (r)}: Flush:
2018/12/28 12:01:09 DEBUG : J. Rawls/Great Live Caper (Album)/06 Great Live Caper (Pt. 2) featuring J-Live (Clean).mp3: Open: flags=O_RDONLY
2018/12/28 12:01:09 DEBUG : &{J. Rawls/Great Live Caper (Album)/06 Great Live Caper (Pt. 2) featuring J-Live (Clean).mp3 (r)}: Flush:
2018/12/28 12:01:09 DEBUG : J. Rawls/Great Live Caper (Album)/06 Great Live Caper (Pt. 2) featuring J-Live (Clean).mp3: Open: flags=O_RDONLY
2018/12/28 12:01:19 DEBUG : &{J. Rawls/Great Live Caper (Album)/06 Great Live Caper (Pt. 2) featuring J-Live (Clean).mp3 (r)}: Flush:

The Flush is the close

1 Like