Skip to content
Myr

fix(tree): grow skippedNodes when pooled capacity is stale (#4852)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

getValue resliced the backtrack stack to index+1 on the assumption that its capacity equals Engine.maxSections. The pool captures that capacity at allocation. Routes registered afterwards raise maxSections without resizing the pooled slice. The next request that needs one more frame panics inside the router, before Recovery. append grows the slice when the captured capacity is short and preserves the existing backtrack behaviour when it is already sufficient. The reset at entry stays: getValue still runs once per method tree on the same stack.

authored by HazyLab · committed by GitHub · parent e8f2496 · Browse files · 2 files+31 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: hazyhaar <hazyhaarlabs@gmail.com>
  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>

Diff stats

2 files+31 added−8 removed

  • other:+31 added−8 removed

2 files+31 added−8 removed

modified:routes_test.go+24 added
845 unmodified lines
846 w := PerformRequest(r, http.MethodGet, "/base/v1/user/groups")847 assert.Equal(t, http.StatusNotFound, w.Code)848}849850func TestLateRouteRegistrationSkippedNodes(t *testing.T) {851 router := New()852 router.HandleMethodNotAllowed = true853 router.GET("/a", func(c *Context) { c.Status(http.StatusOK) })854855 w := PerformRequest(router, http.MethodGet, "/a")856 if w.Code != http.StatusOK {857 t.Fatalf("warmup status = %d, want 200", w.Code)858 }859860 h := func(c *Context) {}861 router.GET("/x/y/z/w", h)862 router.GET("/x/y/:id/w", h)863 router.GET("/x/:id/z/w", h)864 router.GET("/:id/y/z/w", h)865866 req := httptest.NewRequest(http.MethodPost, "/x/y/z/w/extra", nil)867 w = httptest.NewRecorder()868 router.ServeHTTP(w, req)869 if w.Code != http.StatusNotFound {870 t.Fatalf("status = %d, want 404", w.Code)871 }872}873874// TestIssue4818_skippedNodesOverflow_Panic reproduces the panic from issue #4818:875// with HandleMethodNotAllowed enabled, getValue is called once per method tree876// reusing the same c.skippedNodes stack without resetting it, so residue877// accumulates until the reslice exceeds the capacity (engine.maxSections).
92 unmodified lines
modified:tree.go+7 added-8 removed
418 unmodified lines
419 var globalParamsCount int16420421 // Reset the skipped-nodes stack on entry. getValue is called once per422 // method tree (e.g. in the HandleMethodNotAllowed loop) reusing the same423 // pooled Context stack, and the walk below grows it via a raw reslice that424 // cannot exceed engine.maxSections. Without this reset the residue from a425 // previous tree leaks into the next call and can panic with "slice bounds426 // out of range" once the accumulated length passes the capacity.423 // pooled Context stack. Without this reset the residue from a previous424 // tree leaks into the next call. The walk grows the stack with append:425 // a context allocated before addRoute raised maxSections must still be426 // able to record one more backtrack frame. A raw reslice of the capacity427 // captured at pool allocation panics with "slice bounds out of range".428 *skippedNodes = (*skippedNodes)[:0]429430walk: // Outer loop for walking the tree431 for {
7 unmodified lines
439 for i, c := range []byte(n.indices) {440 if c == idxc {441 // strings.HasPrefix(n.children[len(n.children)-1].path, ":") == n.wildChild442 if n.wildChild {442 index := len(*skippedNodes)443 *skippedNodes = (*skippedNodes)[:index+1]444 (*skippedNodes)[index] = skippedNode{443 *skippedNodes = append(*skippedNodes, skippedNode{444 path: prefix + path,445 node: &node{446 path: n.path,447 wildChild: n.wildChild,
3 unmodified lines
451 handlers: n.handlers,452 fullPath: n.fullPath,453 },454 paramsCount: globalParamsCount,456 }455 })456 }457458 n = n.children[i]459 continue walk
498 unmodified lines