From 04fc4b7e05bfbfee6cb7aa4f6c30d1af6f2d4d2d Mon Sep 17 00:00:00 2001
From: wxiaoguang <wxiaoguang@gmail.com>
Date: Thu, 5 May 2022 22:13:23 +0800
Subject: [PATCH] Call MultipartForm.RemoveAll when request finishes (#19606)

---
 modules/context/api.go               |  1 +
 modules/context/context.go           | 12 ++++++++++++
 modules/context/package.go           |  1 +
 modules/context/private.go           |  2 ++
 modules/test/context_tests.go        |  1 +
 routers/api/v1/misc/markdown_test.go |  2 ++
 routers/install/install.go           |  2 ++
 7 files changed, 21 insertions(+)

diff --git a/modules/context/api.go b/modules/context/api.go
index 20a3e05177..33534dbf6b 100644
--- a/modules/context/api.go
+++ b/modules/context/api.go
@@ -256,6 +256,7 @@ func APIContexter() func(http.Handler) http.Handler {
 				},
 				Org: &APIOrganization{},
 			}
+			defer ctx.Close()
 
 			ctx.Req = WithAPIContext(WithContext(req, ctx.Context), &ctx)
 
diff --git a/modules/context/context.go b/modules/context/context.go
index 17e1673a36..dcc43973ca 100644
--- a/modules/context/context.go
+++ b/modules/context/context.go
@@ -75,6 +75,16 @@ type Context struct {
 	Package     *Package
 }
 
+// Close frees all resources hold by Context
+func (ctx *Context) Close() error {
+	var err error
+	if ctx.Req != nil && ctx.Req.MultipartForm != nil {
+		err = ctx.Req.MultipartForm.RemoveAll() // remove the temp files buffered to tmp directory
+	}
+	// TODO: close opened repo, and more
+	return err
+}
+
 // TrHTMLEscapeArgs runs Tr but pre-escapes all arguments with html.EscapeString.
 // This is useful if the locale message is intended to only produce HTML content.
 func (ctx *Context) TrHTMLEscapeArgs(msg string, args ...string) string {
@@ -693,6 +703,8 @@ func Contexter() func(next http.Handler) http.Handler {
 					"RunModeIsProd": setting.IsProd,
 				},
 			}
+			defer ctx.Close()
+
 			// PageData is passed by reference, and it will be rendered to `window.config.pageData` in `head.tmpl` for JavaScript modules
 			ctx.PageData = map[string]interface{}{}
 			ctx.Data["PageData"] = ctx.PageData
diff --git a/modules/context/package.go b/modules/context/package.go
index 47af88c97b..cb352fb18a 100644
--- a/modules/context/package.go
+++ b/modules/context/package.go
@@ -100,6 +100,7 @@ func PackageContexter() func(next http.Handler) http.Handler {
 				Resp: NewResponse(resp),
 				Data: map[string]interface{}{},
 			}
+			defer ctx.Close()
 
 			ctx.Req = WithContext(req, &ctx)
 
diff --git a/modules/context/private.go b/modules/context/private.go
index b57ba102e6..fdc7751227 100644
--- a/modules/context/private.go
+++ b/modules/context/private.go
@@ -66,6 +66,8 @@ func PrivateContexter() func(http.Handler) http.Handler {
 					Data: map[string]interface{}{},
 				},
 			}
+			defer ctx.Close()
+
 			ctx.Req = WithPrivateContext(req, ctx)
 			ctx.Data["Context"] = ctx
 			next.ServeHTTP(ctx.Resp, ctx.Req)
diff --git a/modules/test/context_tests.go b/modules/test/context_tests.go
index a05b221af8..c745a106c5 100644
--- a/modules/test/context_tests.go
+++ b/modules/test/context_tests.go
@@ -38,6 +38,7 @@ func MockContext(t *testing.T, path string) *context.Context {
 		Resp:   context.NewResponse(resp),
 		Locale: &mockLocale{},
 	}
+	defer ctx.Close()
 
 	requestURL, err := url.Parse(path)
 	assert.NoError(t, err)
diff --git a/routers/api/v1/misc/markdown_test.go b/routers/api/v1/misc/markdown_test.go
index 349498e2ab..9beb88be16 100644
--- a/routers/api/v1/misc/markdown_test.go
+++ b/routers/api/v1/misc/markdown_test.go
@@ -37,6 +37,8 @@ func createContext(req *http.Request) (*context.Context, *httptest.ResponseRecor
 		Render: rnd,
 		Data:   make(map[string]interface{}),
 	}
+	defer c.Close()
+
 	return c, resp
 }
 
diff --git a/routers/install/install.go b/routers/install/install.go
index 459f534b89..41b11aef33 100644
--- a/routers/install/install.go
+++ b/routers/install/install.go
@@ -79,6 +79,8 @@ func Init(next http.Handler) http.Handler {
 				"PasswordHashAlgorithms": user_model.AvailableHashAlgorithms,
 			},
 		}
+		defer ctx.Close()
+
 		ctx.Req = context.WithContext(req, &ctx)
 		next.ServeHTTP(resp, ctx.Req)
 	})