Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,14 @@ func (c *Context) Reset(r *http.Request, w http.ResponseWriter) {
c.handler = nil
c.dsw = delayedStatusWriter{}
c.path = ""
// NOTE: empty by setting length to 0. PathValues has to have capacity of c.echo.contextPathParamAllocSize at all times
c.clearPathValues()
}

// clearPathValues empties pathValues and zeroes its whole backing array, so a pooled Context does not keep
// the previous request's path parameter strings alive.
// NOTE: empty by setting length to 0. PathValues has to have capacity of c.echo.contextPathParamAllocSize at all times
func (c *Context) clearPathValues() {
clear((*c.pathValues)[:cap(*c.pathValues)])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This runs when a context is taken from the pool, not when it goes back: serveHTTP does defer e.contextPool.Put(c), and Reset only happens on the next Get. So while a context sits in the pool it still holds the last request's values. After GET /a/1/secret, its backing array is [{id 1} {token secret}] on this branch too. If the aim is to drop those references while pooled, as the description says, clearing before Put would cover it. As it stands, the change zeroes the slots past the next route's params.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — you're right that clearing only inside Context.Reset() didn't release the strings while an idle Context sits in contextPool.

I've updated serveHTTP to call c.clearPathValues() before returning c to e.contextPool.Put(c) (while keeping clearPathValues() in Context.Reset() for direct callers) and added TestEchoServeHTTPClearsPathValuesBeforePooling to verify that a pooled Context has its pathValues backing array zeroed immediately after ServeHTTP returns.

Note: I left Echo.ReleaseContext untouched to avoid breaking callers that might still read c after calling it (the existing TestPathValuesSizeOverMultipleRequests in router_test.go actually calls e.ReleaseContext(c) before handler(c) and c.Param(...)). Happy to also clear in ReleaseContext and adjust that test's ordering if you prefer!

*c.pathValues = (*c.pathValues)[:0]
}

Expand Down
22 changes: 22 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1822,3 +1822,25 @@ func TestRouteInfo(t *testing.T) {
orgRI.Name = "changed"
assert.NotEqual(t, expect, c.RouteInfo())
}

func TestContextResetClearsPathValuesBackingArray(t *testing.T) {
e := New()
c := e.NewContext(nil, nil)
c.SetPathValues(PathValues{
{Name: "id", Value: "12345"},
{Name: "token", Value: "secret-value"},
})
// Shortening the slice leaves the previous second element in spare capacity.
c.SetPathValues(PathValues{
{Name: "id", Value: "67890"},
})

origCap := cap(*c.pathValues)
c.Reset(nil, nil)

assert.Empty(t, *c.pathValues)
assert.Equal(t, origCap, cap(*c.pathValues))
for i, pv := range (*c.pathValues)[:cap(*c.pathValues)] {
assert.Equal(t, PathValue{}, pv, "pathValues backing array element %d should be zeroed", i)
}
}
6 changes: 5 additions & 1 deletion echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,11 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// serveHTTP implements `http.Handler` interface, which serves HTTP requests.
func (e *Echo) serveHTTP(w http.ResponseWriter, r *http.Request) {
c := e.contextPool.Get().(*Context)
defer e.contextPool.Put(c)
defer func() {
// Drop the request's path values before pooling so an idle Context does not keep them alive.
c.clearPathValues()
e.contextPool.Put(c)
}()

c.Reset(r, w)

Expand Down
17 changes: 17 additions & 0 deletions echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,23 @@ func TestEchoContext(t *testing.T) {
e.ReleaseContext(c)
}

func TestEchoServeHTTPClearsPathValuesBeforePooling(t *testing.T) {
e := New()
var pooled *Context
e.GET("/a/:id/:token", func(c *Context) error {
pooled = c
return c.NoContent(http.StatusOK)
})

e.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/a/1/secret", nil))

// pooled now sits in the pool; it must not keep the request's path values alive.
assert.Empty(t, *pooled.pathValues)
for i, pv := range (*pooled.pathValues)[:cap(*pooled.pathValues)] {
assert.Equal(t, PathValue{}, pv, "pathValues backing array element %d should be zeroed", i)
}
}

func TestPreMiddlewares(t *testing.T) {
e := New()
assert.Equal(t, 0, len(e.PreMiddlewares()))
Expand Down