Skip to content

Commit 2c25188

Browse files
Add TS_GO_DEBUG_STACK_LIMIT to cap goroutine stack size at entry points (#3649)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com> Co-authored-by: Ryan Cavanaugh <RyanCavanaugh@users.noreply.github.com>
1 parent 1b81e8f commit 2c25188

15 files changed

Lines changed: 43 additions & 0 deletions

File tree

cmd/tsgo/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"os"
55

6+
"github.com/microsoft/typescript-go/internal/core"
67
"github.com/microsoft/typescript-go/internal/execute"
78
)
89

@@ -11,6 +12,7 @@ func main() {
1112
}
1213

1314
func runMain() int {
15+
core.ApplyDebugStackLimit()
1416
args := os.Args[1:]
1517
if len(args) > 0 {
1618
switch args[0] {

internal/api/encoder/testmain_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package encoder_test
33
import (
44
"testing"
55

6+
"github.com/microsoft/typescript-go/internal/core"
67
"github.com/microsoft/typescript-go/internal/testutil/baseline"
78
)
89

910
func TestMain(m *testing.M) {
11+
core.ApplyDebugStackLimit()
1012
defer baseline.Track()()
1113
m.Run()
1214
}

internal/astnav/testmain_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package astnav_test
33
import (
44
"testing"
55

6+
"github.com/microsoft/typescript-go/internal/core"
67
"github.com/microsoft/typescript-go/internal/testutil/baseline"
78
)
89

910
func TestMain(m *testing.M) {
11+
core.ApplyDebugStackLimit()
1012
defer baseline.Track()()
1113
m.Run()
1214
}

internal/core/core.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import (
44
"iter"
55
"maps"
66
"math"
7+
"os"
8+
rtdebug "runtime/debug"
79
"slices"
810
"sort"
11+
"strconv"
912
"strings"
1013
"sync"
1114
"unicode"
@@ -18,6 +21,18 @@ import (
1821
"github.com/microsoft/typescript-go/internal/tspath"
1922
)
2023

24+
func ApplyDebugStackLimit() {
25+
v := os.Getenv("TS_GO_DEBUG_STACK_LIMIT") //nolint:forbidigo
26+
if v == "" {
27+
return
28+
}
29+
n, err := strconv.Atoi(v)
30+
if err != nil || n <= 0 {
31+
return
32+
}
33+
rtdebug.SetMaxStack(n)
34+
}
35+
2136
func Filter[T any](slice []T, f func(T) bool) []T {
2237
for i, value := range slice {
2338
if !f(value) {

internal/execute/tsctests/testmain_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package tsctests
33
import (
44
"testing"
55

6+
"github.com/microsoft/typescript-go/internal/core"
67
"github.com/microsoft/typescript-go/internal/testutil/baseline"
78
)
89

910
func TestMain(m *testing.M) {
11+
core.ApplyDebugStackLimit()
1012
defer baseline.Track()()
1113
m.Run()
1214
}

internal/fourslash/_scripts/convertFourslash.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ export async function main() {
8282
import (
8383
"testing"
8484
85+
"github.com/microsoft/typescript-go/internal/core"
8586
"github.com/microsoft/typescript-go/internal/testutil/baseline"
8687
)
8788
8889
func TestMain(m *testing.M) {
90+
core.ApplyDebugStackLimit()
8991
defer baseline.Track()()
9092
m.Run()
9193
}

internal/fourslash/tests/gen/testmain_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package fourslash_test
33
import (
44
"testing"
55

6+
"github.com/microsoft/typescript-go/internal/core"
67
"github.com/microsoft/typescript-go/internal/testutil/baseline"
78
)
89

910
func TestMain(m *testing.M) {
11+
core.ApplyDebugStackLimit()
1012
defer baseline.Track()()
1113
m.Run()
1214
}

internal/fourslash/tests/manual/testmain_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package fourslash_test
33
import (
44
"testing"
55

6+
"github.com/microsoft/typescript-go/internal/core"
67
"github.com/microsoft/typescript-go/internal/testutil/baseline"
78
)
89

910
func TestMain(m *testing.M) {
11+
core.ApplyDebugStackLimit()
1012
defer baseline.Track()()
1113
m.Run()
1214
}

internal/fourslash/tests/testmain_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package fourslash_test
33
import (
44
"testing"
55

6+
"github.com/microsoft/typescript-go/internal/core"
67
"github.com/microsoft/typescript-go/internal/testutil/baseline"
78
)
89

910
func TestMain(m *testing.M) {
11+
core.ApplyDebugStackLimit()
1012
defer baseline.Track()()
1113
m.Run()
1214
}

internal/ls/autoimport/testmain_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package autoimport_test
33
import (
44
"testing"
55

6+
"github.com/microsoft/typescript-go/internal/core"
67
"github.com/microsoft/typescript-go/internal/testutil/baseline"
78
)
89

910
func TestMain(m *testing.M) {
11+
core.ApplyDebugStackLimit()
1012
defer baseline.Track()()
1113
m.Run()
1214
}

0 commit comments

Comments
 (0)