Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds call to ctxClear to avoid memory leak. #8

Closed
wants to merge 1 commit into from
Closed

Adds call to ctxClear to avoid memory leak. #8

wants to merge 1 commit into from

Conversation

ghost
Copy link

@ghost ghost commented Jul 7, 2014

The following test code shows that there's a memory leak across uses
of ServeHTTP.


package main

import (
    "github.com/justinas/nosurf"
    "net/http"
    "net/http/httptest"
)

func doNothing(w http.ResponseWriter, r *http.Request) {}

func dummyGet() *http.Request {
    req, err := http.NewRequest("GET", "http://dum.my/", nil)
    if err != nil {
        panic(err)
    }
    return req
}

func main() {
    h := nosurf.New(http.HandlerFunc(doNothing))
    for {
        recorder := httptest.NewRecorder()
        h.ServeHTTP(recorder, dummyGet())
    }
}

The following test code shows that there's a memory leak across uses
of ServeHTTP.

----------------------------------------------------------------------
package main

import (
	"github.com/justinas/nosurf"
	"net/http"
	"net/http/httptest"
)

func doNothing(w http.ResponseWriter, r *http.Request) {}

func dummyGet() *http.Request {
	req, err := http.NewRequest("GET", "http://dum.my/", nil)
	if err != nil {
		panic(err)
	}
	return req
}

func main() {
	h := nosurf.New(http.HandlerFunc(doNothing))
	for {
		recorder := httptest.NewRecorder()
		h.ServeHTTP(recorder, dummyGet())
	}
}
----------------------------------------------------------------------
@justinas justinas closed this in 8108a85 Jul 7, 2014
@justinas
Copy link
Owner

justinas commented Jul 7, 2014

A very nice catch! Though I really wanted to do this a bit differently (a perfect use case for defer, in my opinion). I hope you don't mind. Thanks for finding the bug.

@ghost
Copy link
Author

ghost commented Jul 7, 2014

No problem! I'll continue playing with the code if I have time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants