alexedwards / scs

HTTP Session Management for Go
MIT License
2.09k stars 166 forks source link

mime/multipart: TempFile file hangs around on disk after scs.LoadAndSave #89

Closed wwcd closed 4 years ago

wwcd commented 4 years ago

Below exmaple works well

package main

import (
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/upload", upload)
    log.Fatal(http.ListenAndServe(":8080", http.DefaultServeMux))
}

func upload(w http.ResponseWriter, r *http.Request) {
    r.ParseMultipartForm(1 << 20)
}

but tempFile file hands around on disk when use LoadAndSave decorator

package main

import (
    "log"
    "net/http"

    "github.com/alexedwards/scs/v2"
)

func main() {
    http.HandleFunc("/upload", upload)
    session := scs.New()
    log.Fatal(http.ListenAndServe(":8080", session.LoadAndSave(http.DefaultServeMux)))
}

func upload(w http.ResponseWriter, r *http.Request) {
    r.ParseMultipartForm(1 << 20)
}
~ wsl@zdevel
❯ ll -t /tmp/multipart-*
-rw------- 1 wsl zdevel 102400000 Feb 25 12:52 /tmp/multipart-414914614
-rw------- 1 wsl zdevel 102400000 Feb 25 12:52 /tmp/multipart-096958323
-rw------- 1 wsl zdevel 102400000 Feb 25 12:10 /tmp/multipart-776391545
-rw------- 1 wsl zdevel 102400000 Feb 25 12:10 /tmp/multipart-611107330

Package http remove tempfile at end of request https://github.com/golang/go/blob/0d4fa27fd71d12670b7efe06fce3a5b769fa79ce/src/net/http/server.go#L1602-L1605

scs can't remove tmeplfile casue by alloc new request https://github.com/alexedwards/scs/blob/4cd374bf181ed45ace1a3f9af3a04cc26ba70243/session.go#L134-L136

alexedwards commented 4 years ago

Thank you for the clear explanation and the PR : ) Merging now...