#2762 When terminate a Fantom script using Ctrl-C the extenal process called from the script still running

Ilove:= Fri 19 Jul 2019

I use sys::Process to call an extenal process. The app (written in C, not developed by me) is not user friendly at all so I created a wrapper for it using Fantom. The problem is if the user accidently terminate the Fantom script using Ctrl-C the extenal process called from the script still running. Please show me how to deal with this situation. The behavior I wanted is like the function End process tree of Windows Task Manager, when the Fantom script was canceled it will terminate everything.

SlimerDude Fri 19 Jul 2019

You could try adding a Shutdown hook but I'm not sure that gets invokes when Fantom is killed with CTRL-C.

But Java is known for having bad Process management.

Ilove:= Fri 19 Jul 2019

I don't know how to use addShutdownHook. When I add Env.cur.addShutdownHook(p.kill) after p.join with p is the external process I'm calling F4 will show error, expect sys::Void not sys::Process.

I'm not good at functional programming much and still use Fantom with imperative style like Java. What the function addShutdownHook expects is something using -> so I don't know what to do now.

SlimerDude Sat 20 Jul 2019

addShutdownHook() takes a function so it would be:

Env.cur.addShutdownHook() |->| {
    process.kill
}

But the function needs to be immutable / thread safe - and Process isn't, so you'll need to cheat with Unsafe.

processRef := Unsafe(process)
Env.cur.addShutdownHook() |->| {
    ((Process) processRef.val).kill
}

Ilove:= Sat 20 Jul 2019

The default behavior of Ctrl-C on Linux (I only tested with bash shell) is it will also terminate the external process regardless of having addShutdownHook or not.

On Windows the external process still running regardless of having addShutdownHook or not. Answering Y or N to Terminate batch job (Y/N)? doesn't have any effect. I think it's the jvm and specific os implementation, not Fantom's fault. But I'm not sure, though. On SO people said to have the same behavior as End process tree I've to mess with the WinAPI through JNA. It's an overkill to me. So I should just give up, Fantom script just not suitable to be a wrapper script.

Ilove:= Sat 20 Jul 2019

I think if somehow my app could capture Ctrl-C instead of the shell I could manually terminate everything. But I don't know how.

SlimerDude Sat 20 Jul 2019

Fantom's Process class is just a thin wrapper around Java's Process and ProcessBuilder classes.

Answering Y or N to Terminate batch job (Y/N)? doesn't have any effect

It wouldn't. By the time that message comes up, the Fantom / Java process has already ended. That message is just asking if you'd like to continue running the rest of the batch script.

With my own previous experiments with of process killing on Windows machines, I noted that I could only kill actual processes, not scripts. So if I executed a .bat or a .cmd I couldn't then stop whatever the script had started. Instead I would have to re-write any scripts in Fantom (which in my case was running java directly).

Login or Signup to reply.