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

runtime, cgo: cgo call to setgid hangs on GNU/Linux #3871

ianlancetaylor opened this issue Jul 27, 2012 · 1 comment

runtime, cgo: cgo call to setgid hangs on GNU/Linux #3871

ianlancetaylor opened this issue Jul 27, 2012 · 1 comment


Copy link

This program hangs on GNU/Linux.

package main

#include <sys/types.h>
#include <unistd.h>
import "C"

import "fmt"

func main() {
    i, errno := C.setgid(1)
    fmt.Println(i, errno)

The problem is that when a threaded program calls setgid, the pthreads library sends a
SIGSETXID signal to each thread, and waits for the thread to acknowledge it.  The Go
runtime overrides the default signal handler with a Go-specific one.  Since the
os/signal package is not imported, the Go-specific signal handler simply ignores
SIGSETXID.  This means that the acknowledgement is never sent, and the program hangs.
Copy link
Contributor Author

Comment 1:

This issue was closed by revision b9e051e.

Status changed to Fixed.

aclements added a commit that referenced this issue Dec 23, 2014
These signals are used by glibc to broadcast setuid/setgid to all
threads and to send pthread cancellations.  Unlike other signals, the
Go runtime does not intercept these because they must invoke the libc
handlers (see issues #3871 and #6997).  However, because 1) these
signals may be issued asynchronously by a thread running C code to
another thread running Go code and 2) glibc does not set SA_ONSTACK
for its handlers, glibc's signal handler may be run on a Go stack.
Signal frames range from 1.5K on amd64 to many kilobytes on ppc64, so
this may overflow the Go stack and corrupt heap (or other stack) data.

Fix this by ensuring that these signal handlers have the SA_ONSTACK
flag (but not otherwise taking over the handler).

This has been a problem since Go 1.1, but it's likely that people
haven't encountered it because it only affects setuid/setgid and

Fixes #9600.

Change-Id: I6cf5f5c2d3aa48998d632f61f1ddc2778dcfd300
Reviewed-by: Ian Lance Taylor <>
ianlancetaylor added a commit that referenced this issue May 11, 2015
««« backport bfa4e1f1027f
runtime: ignore signal 33 == SIGSETXID on GNU/Linux

When a cgo program calls setuid, setgid, etc., the GNU/Linux
pthread library sends signal SIGSETXID to each thread to tell
it to update its UID info.  If Go is permitted to intercept
the default SIGSETXID signal handler, the program will hang.

This patch tells the runtime package to not try to intercept
SIGSETXID on GNU/Linux.  This will be odd if a Go program
wants to try to use that signal, but it means that cgo
programs that call setuid, etc., won't hang.

Fixes #3871.

R=rsc, r,, bradfitz

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet

No branches or pull requests

2 participants