I am using a python driver that's written in go, the driver connects to a certain service and do some processing. We found an issue with an "unheanlthy" instance of the service and that was making the driver to get stuck and was impossible to terminate unless we kill
the process.
Doing some experimentation I saw that when an extension written in go is executed the program ignores the Ctrl+C
commands issued until control comes back to Python, not happening this with a C written extension, in which the KeyboardInterrupt
is raised while executing C code. My question is why this happens and if there's a way to circunvent this problem or issue a kind of timeout. Tried to raise an exception to emulate a timeout using the threading.Timer
but the problem is that the exception is thrown in it's own thread and doesn't interrupt the main thread. Tested in Python (cpython) 3.9
and 3.10
. As for the extensions I wrote as a poc the Go version is 1.20.4
and the C compiler is 9.4.0
.
I'll leave a small poc below.
Python code:
import ctypes
import ctypes
go_library = ctypes.cdll.LoadLibrary('./go_library.so')
hello_Go = go_library.helloWorld
c_library = ctypes.cdll.LoadLibrary("./c_library.so")
hello_c = c_library.helloWorld
try:
print("Calling golang code")
hello_Go()
print("Calling C code")
hello_c()
except KeyboardInterrupt:
print("Ctrl+C issued DD:")
finally:
print("Done")
Go extension
package main
import (
"C"
"log"
"time"
)
func helloWorld(){
log.Println("Hello World")
time.Sleep(10 * time.Second)
log.Println("Done sleeping")
}
func main(){
}
C extension
#include <stdio.h>
int helloWorld() {
printf("Hello from C\n");
sleep(10);
printf("Done sleeping from C\n");
return 0;
}