@@ -25,7 +25,8 @@ type state struct {
2525 // stackState is the state of the stack while unwound.
2626 stackState
2727
28- launched bool
28+ stackBase uintptr
29+ launched bool
2930
3031 // finishing marks a goroutine that paused after it completed.
3132 // Resume uses this per task flag to clear the stack.
@@ -36,6 +37,8 @@ type state struct {
3637 panicTarget uintptr
3738}
3839
40+ const hasReleasableStack = true
41+
3942// stackState is the saved state of a stack while unwound.
4043// The stack is arranged with asyncify at the bottom, C stack at the top, and a gap of available stack space between the two.
4144type stackState struct {
@@ -79,6 +82,7 @@ func (s *state) initialize(fn uintptr, args unsafe.Pointer, stackSize uintptr) {
7982
8083 // Create a stack.
8184 stack := runtime_alloc (stackSize , gclayout .Conservative .AsPtr ())
85+ s .stackBase = uintptr (stack )
8286
8387 // Set up the stack canary, a random number that should be checked when
8488 // switching from the task back to the scheduler. The stack canary pointer
@@ -153,6 +157,7 @@ func StopPanicUnwind(replay, target uintptr) {
153157 currentTask .state .panicReplay = replay
154158 currentTask .state .panicTarget = target
155159 }
160+ panicStackState = stackState {}
156161}
157162
158163func ClearPanicReplay () {
@@ -165,6 +170,7 @@ func ClearPanicReplay() {
165170 currentTask .state .panicOrigin = stackState {}
166171 currentTask .state .panicReplay = 0
167172 currentTask .state .panicTarget = 0
173+ panicStackState = stackState {}
168174}
169175
170176func PanicRewindData () unsafe.Pointer {
@@ -211,7 +217,12 @@ func (t *Task) Resume() {
211217 if uintptr (t .state .asyncifysp ) > uintptr (t .state .csp ) {
212218 runtimeFatal ("stack overflow" )
213219 }
214- if t .state .finishing {
220+ if t .Exited {
221+ runtime_freeTaskStack (t .state .stackBase )
222+ t .state = state {}
223+ t .gcData = gcData {}
224+ t .DeferFrame = nil
225+ } else if t .state .finishing {
215226 // The task is complete. Clear stale stack pointers and release its argument bundle.
216227 t .state .finishing = false
217228 t .clearStack ()
0 commit comments