Why does Sonarr delete original file on upgrade before copy?

Sonarr version (exact version):
Mono version (if Sonarr is not running on Windows):
OS: Linux
Debug logs: N/A
Description of issue: When Sonarr copies an upgraded episode, it deletes the old episode first which leaves the upgrade time with no episode in place (I am using Plex so ongoing streams are stopped). Why does Sonarr do this and not just copy the upgrade to a slightly modified location (e.g. appending .upgrade) and then do a move to overwrite the old episode as a final step?

Ensures it can be deleted. You’re better off setting up your system so Sonarr can do atomic moves, there is still going to be a brief period where no file is present and streams would be stopped regardless, you can’t swap video files and expect seamless playback.

i would have thought that plex would have the file open for playback which should stop sonarr from being able to delete it? or does it do something funky for streaming?

if it did then sonarr would error and wait for manual assistance to import the new file for that job.

My files to be copied are remote so I would need to sync everything manually before handing off to sonarr – it’s possible but would take quite a bit of work to make sure sonarr only does the move after the torrent has finished and after I’ve manually synced it locally.

I already maintain my own fork, so I was thinking of commenting out ClearTargetPath (https://github.com/Sonarr/Sonarr/blob/577604fcccac15832716cf24467e5e94d28febb1/src/NzbDrone.Common/Disk/DiskTransferService.cs#L299), copying to a temporary location first (https://github.com/Sonarr/Sonarr/blob/577604fcccac15832716cf24467e5e94d28febb1/src/NzbDrone.Common/Disk/DiskTransferService.cs#L336) and then doing a local TryMoveVerified, setting overwrite to true? (https://github.com/Sonarr/Sonarr/blob/577604fcccac15832716cf24467e5e94d28febb1/src/NzbDrone.Common/Disk/DiskTransferService.cs#L476). Sorry for the random code links, but do you think that could mess up how Sonarr is working somewhere else (like it gets confused about the file not being in the expected place straight after the copy or something)

Yeah it has it open and loads it into a tmpfs before streaming, but that doesn’t stop the file its reading from being deleted (and preventing Plex from buffering any more content), so once the client has streamed all the stuff in memory it stops