Make Golang correctly delete temp files during uploading (#36128) (#36129)

Fix #36127

Partially backport #36128
And by the way partially backport  #36017
This commit is contained in:
wxiaoguang
2025-12-11 20:10:59 +01:00
committed by GitHub
parent e98d9bb93e
commit 3d66e75a47
7 changed files with 55 additions and 13 deletions
+5 -2
View File
@@ -71,8 +71,11 @@ func RequestContextHandler() func(h http.Handler) http.Handler {
req = req.WithContext(cache.WithCacheContext(ctx))
ds.SetContextValue(httplib.RequestContextKey, req)
ds.AddCleanUp(func() {
if req.MultipartForm != nil {
_ = req.MultipartForm.RemoveAll() // remove the temp files buffered to tmp directory
// The req in context might have changed due to the new req.WithContext calls
// For example: in NewBaseContext, a new "req" with context is created, and the multipart-form is parsed there.
ctxReq := ds.GetContextValue(httplib.RequestContextKey).(*http.Request)
if ctxReq.MultipartForm != nil {
_ = ctxReq.MultipartForm.RemoveAll() // remove the temp files buffered to tmp directory
}
})
next.ServeHTTP(respWriter, req)