Skip to content

Commit

Permalink
Debug hanging threads
Browse files Browse the repository at this point in the history
  • Loading branch information
Naatan committed May 13, 2022
1 parent 8a70e8d commit d9f541b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/rtutils/singlethread/singlethread.go
Expand Up @@ -3,6 +3,8 @@ package singlethread
import (
"fmt"
"sync"

"github.com/ActiveState/cli/internal/osutils/stacktrace"
)

type callback struct {
Expand All @@ -14,15 +16,28 @@ type Thread struct {
callback chan (callback)
closed bool
mutex *sync.Mutex
stack string
}

var history []*Thread

func PrintNotClosedThreads() {
for _, thread := range history {
if !thread.closed {
fmt.Printf("Thread not closed: %s", thread.stack)
}
}
}

func New() *Thread {
t := &Thread{
make(chan (callback)),
false,
&sync.Mutex{},
stacktrace.Get().String(),
}
go t.run()
history = append(history, t)
return t
}

Expand Down
7 changes: 7 additions & 0 deletions test/integration/run_int_test.go
Expand Up @@ -10,6 +10,7 @@ import (
"testing"
"time"

"github.com/ActiveState/cli/internal/rtutils/singlethread"
"github.com/stretchr/testify/suite"

"github.com/ActiveState/termtest"
Expand Down Expand Up @@ -282,5 +283,11 @@ func (suite *RunIntegrationTestSuite) TestRun_BadLanguage() {
}

func TestRunIntegrationTestSuite(t *testing.T) {
defer func() {
if r := recover(); r != nil {
singlethread.PrintNotClosedThreads()
panic(r)
}
}()
suite.Run(t, new(RunIntegrationTestSuite))
}

0 comments on commit d9f541b

Please sign in to comment.