Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
cmd/cgo: misleading report on crash in cgo code #3797
SIGSEGV in cgo-wrapped mysql produces the following report which is completely misleading: panic: runtime error: invalid memory address or nil pointer dereference [signal 0xb code=0x1 addr=0x0 pc=0x7fc8c17948c2] goroutine 1 [chan receive]: net.(*pollServer).WaitRead(0xf840143c80, 0xf8401486c0, 0xf8400d5c30, 0xb, 0xf8400d5c01, ...) /usr/local/google/home/msolomon/dev/go/src/pkg/net/fd.go:268 +0x73 net.(*netFD).accept(0xf8401486c0, 0x504ed0, 0x0, 0xf84009c210, 0xf8400aa040, ...) gdb reveals the real cause: (gdb) bt #0 my_stat (path=0x7f38aa7fac50 "vt-mysql/share/mysql/charsets/Index.xml", stat_area=0x7f38aa7fab90, my_flags=0) at my_lib.c:540 #1 0x00007f38c4dea70f in my_read_charset_file (filename=0x2 <Address 0x2 out of bounds>, myflags=0) at charset.c:338 #2 0x00007f38c4deb51e in init_available_charsets () at charset.c:431 #3 0x00007f38c540e400 in pthread_once () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_once.S:104 #4 0x00007f38c4deb144 in get_charset_by_csname (cs_name=0x7f3890006ff0 "utf8", cs_flags=32, flags=-1434465744) at charset.c:572 #5 0x00007f38c4dfdc46 in mysql_init_character_set (mysql=0x7f38900009e0) at client.c:1807 #6 0x00007f38c4dff8ed in mysql_real_connect (mysql=0x7f38900009e0, host=0x7f38c4e03fb0 "localhost", user=0x7f38900008c0 "vt_dba", passwd=<optimized out>, db=0x0, port=<optimized out>, unix_socket=0x7f38900008e0 "/vt/vt_0000041983/mysql.sock", client_flag=0) at client.c:2224 #7 0x0000000000527e05 in _cgo_a45dcd6e4066_Cfunc_mysql_real_connect () I can understand if Go can't unwind C stack, but at least it can show the correct goroutine that executes cgo call and show status [cgo call]. Mike may provide more details.
The original problem was the my_* calls rely on thread-local state to be initialized. I was not pedantically initializing this thread-local state and a goroutine migrating across threads was leading to this crash. I now pedantically initialize the thread locals in every cgo calls with a special set of wrappers.
This issue was closed.