@@ -24,6 +24,7 @@ type state struct {
2424 // stackState is the state of the stack while unwound.
2525 stackState
2626
27+ stackBase uintptr
2728 launched bool
2829
2930 // finishing marks a goroutine that paused after it completed.
@@ -35,6 +36,8 @@ type state struct {
3536 panicTarget uintptr
3637}
3738
39+ const hasReleasableStack = true
40+
3841// stackState is the saved state of a stack while unwound.
3942// The stack is arranged with asyncify at the bottom, C stack at the top, and a gap of available stack space between the two.
4043type stackState struct {
@@ -76,8 +79,8 @@ func (s *state) initialize(fn uintptr, args unsafe.Pointer, stackSize uintptr) {
7679 s .entry = fn
7780 s .args = args
7881
79- // Create a stack.
8082 stack := runtime_alloc (stackSize , nil )
83+ s .stackBase = uintptr (stack )
8184
8285 // Set up the stack canary, a random number that should be checked when
8386 // switching from the task back to the scheduler. The stack canary pointer
@@ -152,6 +155,7 @@ func StopPanicUnwind(replay, target uintptr) {
152155 currentTask .state .panicReplay = replay
153156 currentTask .state .panicTarget = target
154157 }
158+ panicStackState = stackState {}
155159}
156160
157161func ClearPanicReplay () {
@@ -164,6 +168,7 @@ func ClearPanicReplay() {
164168 currentTask .state .panicOrigin = stackState {}
165169 currentTask .state .panicReplay = 0
166170 currentTask .state .panicTarget = 0
171+ panicStackState = stackState {}
167172}
168173
169174func PanicRewindData () unsafe.Pointer {
@@ -210,7 +215,12 @@ func (t *Task) Resume() {
210215 if uintptr (t .state .asyncifysp ) > uintptr (t .state .csp ) {
211216 runtimeFatal ("stack overflow" )
212217 }
213- if t .state .finishing {
218+ if t .Exited {
219+ runtime_freeTaskStack (t .state .stackBase )
220+ t .state = state {}
221+ t .gcData = gcData {}
222+ t .DeferFrame = nil
223+ } else if t .state .finishing {
214224 // The task is complete. Clear stale stack pointers and release its argument bundle.
215225 t .state .finishing = false
216226 t .clearStack ()
0 commit comments