Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unstoppable program: CSignal: interrupt not received #358

Open
divad1196 opened this issue Jul 14, 2024 · 0 comments
Open

Unstoppable program: CSignal: interrupt not received #358

divad1196 opened this issue Jul 14, 2024 · 0 comments

Comments

@divad1196
Copy link

divad1196 commented Jul 14, 2024

Summary

I am trying to run a Golang http server through python but then the program cannot be stopped.
I tried to send SIGTERM(ctrl+c)/SIGKILL(ctrl+d) signals from the terminal but it didn't work.
Sending the signal from another terminal works.

Reproduce the error

File structure

.
├── go.mod
├── go.sum
├── main.go
├── Makefile
├── out
└── test.py

Files

main.go

package mypackage

import (
	"fmt"
	"net/http"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello, World!")
}

func Run() {
	http.HandleFunc("/", helloHandler)
	http.ListenAndServe(":8080", nil)
}

Makefile

build:
	export LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:.
	gopy build -output=out -vm=python3 .

test.py

from out import mypackage
mypackage.Run()

NOTE: The example in the README.md says to first enter out folder, but the import does not work from inside the out folder

Run the test

python3 test.py

Then try to stop the process with ctrl+c (NOTE: this interrupt signal works fine if the Go code is compiled as a regular binary and not for the binding)

Workaround

A workaround is to run the server in a goroutine while the main thread block until it receives the signal:

func Run() {
	server := &http.Server{
		Addr:    ":8080",
		Handler: http.DefaultServeMux,
	}

	http.HandleFunc("/", helloHandler)

	// Channel to listen for OS signals
	stop := make(chan os.Signal, 1)
	signal.Notify(stop, os.Interrupt, syscall.SIGTERM)

	go func() {
		if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			fmt.Printf("ListenAndServe(): %s\n", err)
		}
	}()
	fmt.Println("Server is ready to handle requests at :8080")

	<-stop // Block until we receive a signal

	fmt.Println("Shutting down the server...")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	if err := server.Shutdown(ctx); err != nil {
		fmt.Printf("Server Shutdown Failed:%+v", err)
	}
	fmt.Println("Server stopped")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant