Skip to content

Commit 04fc4b7

Browse files
authored
Call MultipartForm.RemoveAll when request finishes (#19606)
1 parent 7b089c4 commit 04fc4b7

File tree

7 files changed

+21
-0
lines changed

7 files changed

+21
-0
lines changed

modules/context/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ func APIContexter() func(http.Handler) http.Handler {
256256
},
257257
Org: &APIOrganization{},
258258
}
259+
defer ctx.Close()
259260

260261
ctx.Req = WithAPIContext(WithContext(req, ctx.Context), &ctx)
261262

modules/context/context.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ type Context struct {
7575
Package *Package
7676
}
7777

78+
// Close frees all resources hold by Context
79+
func (ctx *Context) Close() error {
80+
var err error
81+
if ctx.Req != nil && ctx.Req.MultipartForm != nil {
82+
err = ctx.Req.MultipartForm.RemoveAll() // remove the temp files buffered to tmp directory
83+
}
84+
// TODO: close opened repo, and more
85+
return err
86+
}
87+
7888
// TrHTMLEscapeArgs runs Tr but pre-escapes all arguments with html.EscapeString.
7989
// This is useful if the locale message is intended to only produce HTML content.
8090
func (ctx *Context) TrHTMLEscapeArgs(msg string, args ...string) string {
@@ -693,6 +703,8 @@ func Contexter() func(next http.Handler) http.Handler {
693703
"RunModeIsProd": setting.IsProd,
694704
},
695705
}
706+
defer ctx.Close()
707+
696708
// PageData is passed by reference, and it will be rendered to `window.config.pageData` in `head.tmpl` for JavaScript modules
697709
ctx.PageData = map[string]interface{}{}
698710
ctx.Data["PageData"] = ctx.PageData

modules/context/package.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func PackageContexter() func(next http.Handler) http.Handler {
100100
Resp: NewResponse(resp),
101101
Data: map[string]interface{}{},
102102
}
103+
defer ctx.Close()
103104

104105
ctx.Req = WithContext(req, &ctx)
105106

modules/context/private.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ func PrivateContexter() func(http.Handler) http.Handler {
6666
Data: map[string]interface{}{},
6767
},
6868
}
69+
defer ctx.Close()
70+
6971
ctx.Req = WithPrivateContext(req, ctx)
7072
ctx.Data["Context"] = ctx
7173
next.ServeHTTP(ctx.Resp, ctx.Req)

modules/test/context_tests.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func MockContext(t *testing.T, path string) *context.Context {
3838
Resp: context.NewResponse(resp),
3939
Locale: &mockLocale{},
4040
}
41+
defer ctx.Close()
4142

4243
requestURL, err := url.Parse(path)
4344
assert.NoError(t, err)

routers/api/v1/misc/markdown_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ func createContext(req *http.Request) (*context.Context, *httptest.ResponseRecor
3737
Render: rnd,
3838
Data: make(map[string]interface{}),
3939
}
40+
defer c.Close()
41+
4042
return c, resp
4143
}
4244

routers/install/install.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ func Init(next http.Handler) http.Handler {
7979
"PasswordHashAlgorithms": user_model.AvailableHashAlgorithms,
8080
},
8181
}
82+
defer ctx.Close()
83+
8284
ctx.Req = context.WithContext(req, &ctx)
8385
next.ServeHTTP(resp, ctx.Req)
8486
})

0 commit comments

Comments
 (0)