Skip to content

Commit 46637b1

Browse files
authored
Call MultipartForm.RemoveAll when request finishes (#19606) (#19607)
1 parent 7b18c67 commit 46637b1

File tree

6 files changed

+20
-0
lines changed

6 files changed

+20
-0
lines changed

modules/context/api.go

+1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ func APIContexter() func(http.Handler) http.Handler {
288288
},
289289
Org: &APIOrganization{},
290290
}
291+
defer ctx.Close()
291292

292293
ctx.Req = WithAPIContext(WithContext(req, ctx.Context), &ctx)
293294
ctx.csrf = Csrfer(csrfOpts, ctx.Context)

modules/context/context.go

+12
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ type Context struct {
7171
Org *Organization
7272
}
7373

74+
// Close frees all resources hold by Context
75+
func (ctx *Context) Close() error {
76+
var err error
77+
if ctx.Req != nil && ctx.Req.MultipartForm != nil {
78+
err = ctx.Req.MultipartForm.RemoveAll() // remove the temp files buffered to tmp directory
79+
}
80+
// TODO: close opened repo, and more
81+
return err
82+
}
83+
7484
// TrHTMLEscapeArgs runs Tr but pre-escapes all arguments with html.EscapeString.
7585
// This is useful if the locale message is intended to only produce HTML content.
7686
func (ctx *Context) TrHTMLEscapeArgs(msg string, args ...string) string {
@@ -643,6 +653,8 @@ func Contexter() func(next http.Handler) http.Handler {
643653
"RunModeIsProd": setting.IsProd,
644654
},
645655
}
656+
defer ctx.Close()
657+
646658
// PageData is passed by reference, and it will be rendered to `window.config.pageData` in `head.tmpl` for JavaScript modules
647659
ctx.PageData = map[string]interface{}{}
648660
ctx.Data["PageData"] = ctx.PageData

modules/context/private.go

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func PrivateContexter() func(http.Handler) http.Handler {
3838
Data: map[string]interface{}{},
3939
},
4040
}
41+
defer ctx.Close()
42+
4143
ctx.Req = WithPrivateContext(req, ctx)
4244
next.ServeHTTP(ctx.Resp, ctx.Req)
4345
})

modules/test/context_tests.go

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func MockContext(t *testing.T, path string) *context.Context {
3939
Resp: context.NewResponse(resp),
4040
Locale: &mockLocale{},
4141
}
42+
defer ctx.Close()
4243

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

routers/api/v1/misc/markdown_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ func createContext(req *http.Request) (*context.Context, *httptest.ResponseRecor
3535
Render: rnd,
3636
Data: make(map[string]interface{}),
3737
}
38+
defer c.Close()
39+
3840
return c, resp
3941
}
4042

routers/install/install.go

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ func Init(next http.Handler) http.Handler {
8484
"PasswordHashAlgorithms": user_model.AvailableHashAlgorithms,
8585
},
8686
}
87+
defer ctx.Close()
88+
8789
for _, lang := range translation.AllLangs() {
8890
if lang.Lang == locale.Language() {
8991
ctx.Data["LangName"] = lang.Name

0 commit comments

Comments
 (0)