Looking to install a runtime hook when the program...
# kotlin-native
m
Looking to install a runtime hook when the program completes for Kotlin/Native (kind of like Jvm's
File.deleteOnExit()
). Anyone know of a K/N API that I can use to build off of that would provide similar functionality?
j
❤️ 1
💡 1
You'd still need to install signal handlers if you want to run code on interrupts that normally terminate the process
m
Thanks Jake!
a
AutofreeScope has
defer {}
, for when the memory scope is finished, which is similar https://kotlinlang.org/api/core/kotlin-stdlib/kotlinx.cinterop/-autofree-scope/#-2026372596%2FFunctions%2F1938390331
m
I thought about that, but unfortunately will not work for my use case. Trying to get the same/similar functionality as
File.deleteOnExit()
where the hook executes when the VM shuts down. Looking into using signal handlers atm.
Wait, jake...
finalization-hook
is a suspend function, so I could do something like?
Copy code
GlobalScope.launch {
  withFinalizationHook(
    hook = ::executeDeleteOnExit,
    block = { delay(Duration.INFINITE) }
  )
}
mind blown 1
j
Probably? Although it's not set up for you to do that more than once, but it would be possible to add that
m
Yeah, unfortunately it wasn't working when I was playing around with things. I purposefully caused a segmentation fault which re-sends
SIGABRT
, but it wasn't catching it. Ended up just using
sigaction
for Unix which is working. Still have to figure out Windows.
nativeMain
https://github.com/05nelsonm/kmp-tor-resource/blob/master/library/resource-noexec-tor/src/nativeMain/kotlin/io/matthewnelson/kmp/tor/resource/noexec/tor/internal/DeleteOnExit.kt
unixMain
https://github.com/05nelsonm/kmp-tor-resource/blob/master/library/resource-noexec-[…]nelson/kmp/tor/resource/noexec/tor/internal/DeleteOnExitUnix.kt