Panic and crash cases
Pages 64
Clone this wiki locally
For the standard Go compiler, the following cases will cause run-time recoverable panics (some of them might be compiler dependent):
- An interger value is divided by zero.
- Nil dereference:
- Nil pointer dereference, including
- evaluating promoted value-receiver methods on nil embedded pointer fields (this behavior might be changed later, or not).
- calling a promoted value-receiver methods corresponding to a nil embedded pointer field on an interface value.
- Nil slice indexing.
- Add entries to nil maps.
- Calling nil function values (not as goroutine start functions).
- Access methods of nil interface values.
- Nil pointer dereference, including
- Index out of range.
- Index out of range when indexing array/slice elements.
- Index out of range when deriving slices from arrays/slices.
- Type assertion failed but only one return result is presented.
- Comparing interface values with the same incomparable dynamic type.
- Using interface value with incomparable dynamic types as map keys.
- Sending values to or closing closed channels.
- On 32-bit OS, applying 64 bit atomic operations on non-8-bytes aligned words.
- (Since Go Toolchain 1.14,) when
-gcflags=all=-d=checkptr
compiler dynamic analysis option is used, many incorrect unsafe pointer uses will be detected at run time. Such incorrect uses causes panics. - ... (more? Welcome everybody to improve this list.)
Crash cases, or fatal unrecoverable errors (some of them might be compiler dependent):
- Using nil function values as goroutine start functions.
- Stack overflow (dead loop, etc).
- Out of memory when allocating memory.
- Segmentation violation (generally by using
unsafe.Pointer
wrongly). - Concurrent map read and map write detected.
- All goroutines are asleep - deadlock!
- A goroutine exceeds the MaxStack limit while growing its stack.
- Modify immutable memory zone (mainly through unsafe ways. See the last example below.).
- No goroutines (main called runtime.Goexit) - deadlock!
- When data race detector compiler dynamic analysis option is used, many data race cases will be detected at run time. Once a data race is detected, program crashes.
- Set finalizer for an address twice.
An example for the last crash case:
package main
import (
"runtime"
"time"
)
func main() {
go func() {
time.Sleep(time.Second)
}()
runtime.Goexit()
}
An example of modifying immutable memory zone:
package main
import (
"fmt"
"strings"
"unsafe"
)
var _ = strings.Join
type SliceHeader struct {
Data unsafe.Pointer
Len int
Cap int
}
type StringHeader struct {
Data unsafe.Pointer
Len int
}
func String2ByteSlice(str string) (bs []byte) {
strHdr := (*StringHeader)(unsafe.Pointer(&str))
sliceHdr := (*SliceHeader)(unsafe.Pointer(&bs))
sliceHdr.Data = strHdr.Data
sliceHdr.Len = strHdr.Len
sliceHdr.Cap = strHdr.Len
return
}
//var Golang = strings.Join([]string{"Go", "lang"}, "") // not cause panicking
var Golang = "Golang" // causes panicking
func main() {
var goland = String2ByteSlice(Golang)
goland[5] = 'd' // fatal error: unexpected fault address
fmt.Printf("%s\n", goland)
}
Please follow the official Twitter account of Go 101, @go100and1, to learn some Go details, facts, tips, etc, and read some Go articles from time to time.
Go 101 is a series of books on Go programming.
Books in Go 101 series
- Go (Fundamentals) 101 focuses on Go syntax/semantics (except custom generics related) and all kinds of runtime related things.
- Go Optimizations 101 provides some code performance optimization tricks, tips, and suggestions.
- Go Details & Tips 101 collects many details and provides several tips in Go programming.
- Go Generics 101 explains Go custom generics in detail.
Tapir has spent 4+ years on writing the Go 101 book and maintaining the go101.org website. New contents will continue being added to the book and the website from time to time. If you would like to, you can also support this book by playing Tapir's games (for both Android and iPhone/iPad):
- Color Infection (★★★★★) - a physics based casual puzzle original game. 140+ levels.
- Rectangle Pushers (★★★★★) - a casual puzzle original game. 104+ levels.
- Let's Play With Particles - a casual action original game. Three game modes are included.