quipo / statsd

Golang StatsD client
MIT License
164 stars 51 forks source link

fatal error stack overflow when using PrecisionTiming.String() #27

Closed algrebe closed 8 years ago

algrebe commented 8 years ago

the code in event/precisiontiming.go has the String function calling the Payload function, and the payload function returns the precision timing object - this is causing infinite recursion.

package main

import "fmt"

type PrecisionTiming struct {
    Name string
}

func (e PrecisionTiming) Payload() interface{} {
    return e
}

func (e PrecisionTiming) String() string {
    return fmt.Sprintf("{Value: %+v}", e.Payload())
}

func main() {
    p := PrecisionTiming{}
    fmt.Printf("%s\n", p)
}
algrebe commented 8 years ago

This was observed when I tried to use the same key in both PrecisionTiming and Incr using buffered client - Issue https://github.com/quipo/statsd/issues/28

The Update function in PrecisionTiming finds out that they are not of the same type and when it calls the .String() inside the fmt.Errorf this recursion starts.

algrebe commented 8 years ago

@quipo can you open this again ? I don't think it is fixed yet.

package main

import (
    "fmt"
    "github.com/quipo/statsd/event"
    "time"
)

func main() {
    st := time.Now()
    time.Sleep(2 * time.Second)
    duration := time.Now().Sub(st)
    p := event.NewPrecisionTiming("mystat", duration)
    fmt.Printf("%v", p)
}
src/github.com/algrebe/statest$ go run statest.go 
runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow

runtime stack:
runtime.throw(0x50fb00, 0xe)
    /usr/local/go/src/runtime/panic.go:527 +0x90
runtime.newstack()
    /usr/local/go/src/runtime/stack1.go:800 +0xb17
runtime.morestack()
    /usr/local/go/src/runtime/asm_amd64.s:330 +0x7f