dongxichan8627 2017-04-19 07:16
浏览 196

goroutine堆栈跟踪不完整

I wrote a web app using golang. When it's running in production, there are some goroutines blocked. Here are the information (generated by using pprof):

goroutine 792247 [chan receive, 948 minutes]:
database/sql.(*Tx).awaitDone(0xc4206e2b80)
    /usr/local/go/src/database/sql/sql.go:1440 +0x57
created by database/sql.(*DB).begin
    /usr/local/go/src/database/sql/sql.go:1383 +0x274

The goroutine has been waiting on the channel for 948 minites. Apparently, there's something wrong. But the stack traces seems incomplete. It's not enough for me to find the bug. (I want some stack traces start from my program.)

How can I get the full stack traces of this goroutine? Or are there any other ways to debug this issue?

Update:

I've read the source code of database/sql/sql.go. It turns out database/sql/sql.go:1440 is in a new goroutine. The stack traces are incomplete because previous stack traces belong to the parent goroutine.

My question should be : are there better ways to debug this issue?

  • 写回答

1条回答 默认 最新

  • du77887 2017-04-24 08:40
    关注

    I don't think there is any way to get the parent goroutine stack without you having to manually track each go routine invocation and generating an id for it.

    In this specific case, what is likely is that you have a transaction that has not been Committed or Rollbacked because an error occurs and the function prematurely exits without calling either.

    A good template to avoid the same is to use 'defer'.

    func (s Service) DoSomething() (err error) {
        tx, err := s.db.Begin()
        if err != nil {
            return
        }
        defer func() {
            if err != nil {
                tx.Rollback()
                return
            }
            err = tx.Commit()
        }()
        if _, err = tx.Exec(...); err != nil {
            return
        }
        if _, err = tx.Exec(...); err != nil {
            return
        }
        // ...
        return }
    

    Code Reference

    PS: Beware of error shadowing.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效