go-python / go-python/gopy

Unstoppable program: CSignal: interrupt not received

未关闭
#358 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Go
星标
2.3k
派生
136
平均合并
14 分钟
30 天内合并 PR
2

描述

## 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
```bash
.
├── go.mod
├── go.sum
├── main.go
├── Makefile
├── out
└── test.py
```

### Files

#### main.go

```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

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

#### test.py

```go
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
```bash
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:
```go
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")
}

```

贡献指南

这个仓库没有索引到贡献指南

调研方向

先从 main.go、test.py 和 Makefile 中复现的设置开始,然后阅读 gopy 使用的信号处理和 CPython 绑定入口点。运行 python3 test.py,并将生成的绑定的中断行为与普通 Go 二进制文件进行比较。当文档中的服务器示例可以从调用它的终端停止,而不需要来自另一个终端的信号时,即可视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
go, python
领域
backend, operating-systems
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。