mirror of
https://github.com/golang/net.git
synced 2026-03-31 10:27:08 +09:00
In certain shutdown cases (from the client and/or server), the http2
Server can Push stream-specific frames on closed streams. This caused
memory leaks in the random write scheduler.
As a conservative fix for backporting, just clear the map element
whenever its queue value is empty. The map entry is re-created as
needed anyway. This isn't perfectly ideal (it adds a map+delete and
free queue put+get) in the case where a stream is open & actively
writing, but it's an easy fix for now. A future CL can optimize all
this code. It looks like there are some other good optimization
opportunities in related code anyway. But I'd rather that happen on
master and not be done in a backported change.
Updates golang/go#34636 (needs bundle to std before fixed)
Updates golang/go#33812
Change-Id: I21508ba2ebc361e8b8532d0d1cebf882e82c473c
Reviewed-on: https://go-review.googlesource.com/c/net/+/198462
Reviewed-by: Bryan C. Mills <bcmills@google.com>
(cherry picked from commit d98b1b4438)
Reviewed-on: https://go-review.googlesource.com/c/net/+/198617
Reviewed-by: Andrew Bonventre <andybons@golang.org>
Run-TryBot: Andrew Bonventre <andybons@golang.org>
61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
// Copyright 2016 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package http2
|
|
|
|
import "testing"
|
|
|
|
func TestRandomScheduler(t *testing.T) {
|
|
ws := NewRandomWriteScheduler()
|
|
ws.Push(makeWriteHeadersRequest(3))
|
|
ws.Push(makeWriteHeadersRequest(4))
|
|
ws.Push(makeWriteHeadersRequest(1))
|
|
ws.Push(makeWriteHeadersRequest(2))
|
|
ws.Push(makeWriteNonStreamRequest())
|
|
ws.Push(makeWriteNonStreamRequest())
|
|
|
|
// Pop all frames. Should get the non-stream requests first,
|
|
// followed by the stream requests in any order.
|
|
var order []FrameWriteRequest
|
|
for {
|
|
wr, ok := ws.Pop()
|
|
if !ok {
|
|
break
|
|
}
|
|
order = append(order, wr)
|
|
}
|
|
t.Logf("got frames: %v", order)
|
|
if len(order) != 6 {
|
|
t.Fatalf("got %d frames, expected 6", len(order))
|
|
}
|
|
if order[0].StreamID() != 0 || order[1].StreamID() != 0 {
|
|
t.Fatal("expected non-stream frames first", order[0], order[1])
|
|
}
|
|
got := make(map[uint32]bool)
|
|
for _, wr := range order[2:] {
|
|
got[wr.StreamID()] = true
|
|
}
|
|
for id := uint32(1); id <= 4; id++ {
|
|
if !got[id] {
|
|
t.Errorf("frame not found for stream %d", id)
|
|
}
|
|
}
|
|
|
|
// Verify that we clean up maps for empty queues in all cases (golang.org/issue/33812)
|
|
const arbitraryStreamID = 123
|
|
ws.Push(makeHandlerPanicRST(arbitraryStreamID))
|
|
rws := ws.(*randomWriteScheduler)
|
|
if got, want := len(rws.sq), 1; got != want {
|
|
t.Fatalf("len of 123 stream = %v; want %v", got, want)
|
|
}
|
|
_, ok := ws.Pop()
|
|
if !ok {
|
|
t.Fatal("expected to be able to Pop")
|
|
}
|
|
if got, want := len(rws.sq), 0; got != want {
|
|
t.Fatalf("len of 123 stream = %v; want %v", got, want)
|
|
}
|
|
|
|
}
|