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

feat(coroutine): improve panic handling with igop-style stack traces #487

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions internal/coroutine/coro.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ func (p *threadImpl) Stopped() bool {
}

// Thread represents a coroutine id.
//
type Thread = *threadImpl

// Coroutines represents a coroutine manager.
//
type Coroutines struct {
suspended map[Thread]bool
current Thread
Expand All @@ -44,7 +42,6 @@ type Coroutines struct {
}

// New creates a coroutine manager.
//
func New() *Coroutines {
p := &Coroutines{
suspended: make(map[Thread]bool),
Expand All @@ -54,7 +51,6 @@ func New() *Coroutines {
}

// Create creates a new coroutine.
//
func (p *Coroutines) Create(tobj ThreadObj, fn func(me Thread) int) Thread {
return p.CreateAndStart(false, tobj, fn)
}
Expand All @@ -68,7 +64,6 @@ func (p *Coroutines) Current() Thread {
}

// CreateAndStart creates and executes the new coroutine.
//
func (p *Coroutines) CreateAndStart(start bool, tobj ThreadObj, fn func(me Thread) int) Thread {
id := &threadImpl{Obj: tobj}
go func() {
Expand All @@ -80,7 +75,9 @@ func (p *Coroutines) CreateAndStart(start bool, tobj ThreadObj, fn func(me Threa
p.mutex.Unlock()
p.sema.Unlock()
if e := recover(); e != nil {
if e != ErrAbortThread {
if pe, ok := e.(iGopPanicError); ok {
panic(pe.Error() + "\n" + string(pe.Stack()))
Copy link
Member

@xushiwei xushiwei Feb 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not to format error info in outer side (at where recover this error)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue isn't about formatting error info. When user code panics, the panic/recover mechanism here catches and re-throws it, which creates a new stack trace starting from the re-panic point. This makes it impossible to trace back to the original panic location in user code.

} else if e != ErrAbortThread {
panic(e)
}
}
Expand Down Expand Up @@ -108,7 +105,6 @@ func (p *Coroutines) StopIf(filter func(th Thread) bool) {
}

// Yield suspends a running coroutine.
//
func (p *Coroutines) Yield(me Thread) {
if p.Current() != me {
panic(ErrCannotYieldANonrunningThread)
Expand All @@ -129,7 +125,6 @@ func (p *Coroutines) Yield(me Thread) {
}

// Resume resumes a suspended coroutine.
//
func (p *Coroutines) Resume(th Thread) {
for {
done := false
Expand All @@ -148,7 +143,6 @@ func (p *Coroutines) Resume(th Thread) {
}

// Sched func.
//
func (p *Coroutines) Sched(me Thread) {
go func() {
p.Resume(me)
Expand All @@ -166,3 +160,9 @@ func (p *Coroutines) Sleep(t time.Duration) {
}

// -------------------------------------------------------------------------------------

// See https://pkg.go.dev/github.com/goplus/igop#PanicError.
type iGopPanicError interface {
Error() string
Stack() []byte
}
Loading