Rclone to respect a soft-kill termination

rclone.exe does not seem to respect a soft-kill request such as taskkill /im rclone.exe
a soft-kill sends a signal to the rclone.exe to close gracefully
and
unmount all mounted drives.

when i try to soft-kill this happens:

C:\data\rclone\scripts>taskkill /IM rclone.exe
ERROR: The process "rclone.exe" with PID 3000 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).
SUCCESS: Sent termination signal to the process "rclone.exe" with PID 15936.

Run as admin, maybe works

thanks for the suggestion but the command prompt that i am using to run the command has administrator privileges.

It looks like that rclone would have to respond to the WM_CLOSE message in order to terminate gracefully like this. Given that rclone isn't a GUI program and doesn't have any windows open this would be reasonably complicated and involve making a hidden windows to receive the event.

Another alternative to the kill would be to simulate sending CTRL-C to rclone. There are some ideas here:

after a lot of testing, it seems that sometimes a softkill will work and sometimes a softkill will not work.
it depends on how rclone is lauched.

i mount 3 different mounts and use taskkill to kill them.
in the first test, only 1 out of 3 were terminated.
in the second test, only 2 out of 3 were terminated.

in all my testing, i was not able to get all 3 rclone.exe to respect the softkill request.

test 1

start rclone.exe mount wasabiwest01:vserver03-bjv.h.vserver03.g.ampe01\ x:
start /b rclone.exe mount wasabiwest01:vserver03-bjv.h.vserver03.g.ampe01\ y:
rclone.exe mount wasabiwest01:vserver03-bjv.h.vserver03.g.ampe01\ z:

and here is the output of

C:\data\rclone\scripts\rr\mount>tasklist /fi "imagename eq rclone.exe"
C:\data\rclone\scripts\rr\mount>taskkill /im rclone.exe
SUCCESS: Sent termination signal to the process "rclone.exe" with PID 20060.
ERROR: The process "rclone.exe" with PID 4988 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).
ERROR: The process "rclone.exe" with PID 15100 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).

test 2
and here are the 3 scripts,

start rclone.exe mount wasabiwest01:vserver03-bjv.h.vserver03.g.ampe01\ x:

and

start /b rclone.exe mount wasabiwest01:vserver03-bjv.h.vserver03.g.ampe01\ y:

and

rclone.exe mount wasabiwest01:vserver03-bjv.h.vserver03.g.ampe01\ z:

and here is the output

C:\data\rclone\scripts\rr\mount>taskkill /im rclone.exe
SUCCESS: Sent termination signal to the process "rclone.exe" with PID 17400.
SUCCESS: Sent termination signal to the process "rclone.exe" with PID 9344.
ERROR: The process "rclone.exe" with PID 20704 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).

in my testing, never once, did sendsignal.exe kill rclone.exe

i did some more testing.
it seems that when rclone.exe is executed, there is no conhost.exe created and as a result, rclone cannot see the ctrl+c/ctrl+break softkill as sent by taskkill

this process tree is showing the following command.
start rclone.exe mount wasabiwest01:vserver03-bjv.h.vserver03.g.ampe01\ x:
as you can see, there is a conhost.exe attachted to rclone.exe
image

this process tree is showing the following command
rclone.exe mount wasabiwest01:vserver03-bjv.h.vserver03.g.ampe01\ z:
as you can see, there is NOT a conhost.exe attached to rclone.exe

seems to me that rclone.exe needs to implement this function call.

It looks like that has been added to go1.13 already: https://github.com/golang/go/issues/7479

rclone up to v1.49.3 was compiled with go1.12 but if you try the recently released rclone 1.49.4 that was compiled with go1.13 so may include the fix...

Can you give it a go?

thanks for the update but no change in behavior,
but i will look deeper into the link that you sent to me.

C:\data\rclone\scripts\rr\mount>C:\data\rclone\scripts\rclone.1.49.4.exe --version
rclone v1.49.4

  • os/arch: windows/amd64
  • go version: go1.13.1

C:\data\rclone\scripts\rr\mount>tasklist /fi "imagename eq rclone.1.49.4.exe"

Image Name PID Session Name Session# Mem Usage
========================= ======== ================ =========== ============
rclone.1.49.4.exe 21404 Console 1 24,148 K

C:\data\rclone\scripts\rr\mount>taskkill /im rclone.1.49.4.exe
ERROR: The process "rclone.1.49.4.exe" with PID 21404 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).

i also tried sendsignal.exe and that did not work.

C:\data\rclone\scripts\rr\mount>C:\data\rclone\scripts\rclone.1.49.4.exe --version
rclone v1.49.4

  • os/arch: windows/amd64
  • go version: go1.13.1

C:\data\rclone\scripts\rr\mount>tasklist /fi "imagename eq rclone.1.49.4.exe"

Image Name PID Session Name Session# Mem Usage
========================= ======== ================ =========== ============
rclone.1.49.4.exe 21404 Console 1 23,956 K

C:\data\rclone\scripts\rr\mount>sendsignal.exe 21404
Sending signal to process 21404...
CreateRemoteThread failed with 0x00000005.
StartRemoteThread failed with 0x00000005.
0x00000005 == Access is denied.

i found this
https://golang.org/pkg/os/signal/
" On Windows a ^C (Control-C) or ^BREAK (Control-Break) normally cause the program to exit. If Notify is called for os.Interrupt, ^C or ^BREAK will cause os.Interrupt to be sent on the channel, and the program will not exit. If Reset is called, or Stop is called on all channels passed to Notify, then the default behavior will be restored."

rclone does intercept os.Interrupt and I think WinFSP does too. Does rclone quit OK if you press CTRL-C when you have it running in a CMD windows?

yes,

based on my testing, rclone will close ok in the following cases.

  1. when run inside a cmd window.
  2. when run using start
  3. when run using start /w

rlcone will not close ok when run as a standalone program.

Ah... How are you running rclone as a standalone program? I would have thought it would open a CMD windows if you just double click on it.

well, if you want to run rclone.exe in the background, without a cmd window in the taskbar, then you would have a .cmd file with the line.
start /b rclone.1.49.4.exe mount wasabiwest01:vserver03-bjv.h.vserver03.g.ampe01\ y:
in this case, it is not possible to send a ^c but it should be possible to send a ^break.

tho i am not clear as to how to send the signal?

on windows, we use the taskkill.exe to kill apps.

CTRL-Break should stop rclone according to the go docs.

No me neither...

in any event, taskkill.exe is the standard way to kill since windows xp.
most strange the rclone.exe is creating this problem.

when there is cmd window that rclone is running inside of, it could be the ^c is being sent to the cmd window, which then kills all sub-processes.

One thing to try is building rclone like this

go build -ldflags "-H windowsgui"

Can you try this? I'm not 100% sure what will happen!

i program in python, no nothing about go.

and in doing research on the flag windowsgui, i found this link and other links stating the same thing.

https://lukeeckley.com/post/useful-go-build-flags/

Windows Executable Without Console Window

I find this unsightly to have a console window open when I run an exe and I don’t need to see any output. I couldn’t find this documented on any golang site. I can’t remember where I found it, but it is good to know:

Just add "-H=windowsgui" to your linker flags. Console window gone. You’re Welcome.

Download the rclone source, install go then run that command to build your own rclone.exe go build -ldflags "-H windowsgui" is what is required.

I can do it for you but I don't have access to a Windows machine right now!