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

os/signal: SIGTSTP can't be handled #41996

Closed
kke opened this issue Oct 15, 2020 · 3 comments
Closed

os/signal: SIGTSTP can't be handled #41996

kke opened this issue Oct 15, 2020 · 3 comments

Comments

@kke
Copy link

kke commented Oct 15, 2020

What version of Go are you using (go version)?

$ go version
go version go1.15.2 darwin/amd64

Does this issue reproduce with the latest release?

It is the latest release

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/kimmo/Library/Caches/go-build"
GOENV="/Users/kimmo/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/kimmo/Projects/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/kimmo/Projects/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.15.2/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/kimmo/Projects/go/src/github.com/Mirantis/mcc/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/t5/k06n6kss3qz1z5sf0y8pqsw40000gn/T/go-build559599993=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I'm trying to write a remote terminal client that needs to relay Ctrl-Z to the remote host. Intercepting Ctrl-C works fine by using signal.Notify(c, os.Interrupt), but signal.Notify(c, syscall.SIGTSTP) works only half way.

package main

import (
	"os"
	"os/signal"
	"syscall"
	"time"
)

func main() {
	c := make(chan os.Signal)
	signal.Notify(c, syscall.SIGTSTP)

	go func() {
		for {
			<-c
			println("received ctrl-z")
		}
	}()

	for {
		println("Sleeping...")
		time.Sleep(time.Second * 10)
	}
}

What did you expect to see?

Pressing Ctrl-Z should be intercepted and the go process should not suspend.

$ go run main.go
Sleeping...
received ctrl-z
Sleeping...
Sleeping...

What did you see instead?

received ctrl-z is printed but the go process itself is also suspended.

$ go run main.go
Sleeping...
received ctrl-z
[1]+  Stopped                 go run main.go
$ _
@seankhliao
Copy link
Member

use go build and run the executable instead of using go run

@toothrot toothrot changed the title SIGTSTP can't be handled os/signal: SIGTSTP can't be handled Oct 15, 2020
@toothrot
Copy link
Contributor

@kke I am closing this, as I believe @seankhliao's answer is correct. Please comment if I am mistaken.

@kke
Copy link
Author

kke commented Oct 16, 2020

@seankhliao is correct. Setting the local terminal to raw mode (for example via "golang.org/x/crypto/ssh/terminal"s MakeRaw()) makes it work even in go run mode (which also makes ctrl-d work)

@golang golang locked and limited conversation to collaborators Oct 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants