|
| 1 | +// Copyright (c) 2017 Uber Technologies, Inc. |
| 2 | + |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | +package tchannel_test |
| 21 | + |
| 22 | +import ( |
| 23 | + "io/ioutil" |
| 24 | + "runtime" |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | + |
| 28 | + . "github.com/uber/tchannel-go" |
| 29 | + "github.com/uber/tchannel-go/testutils" |
| 30 | + |
| 31 | + "github.com/stretchr/testify/require" |
| 32 | +) |
| 33 | + |
| 34 | +// This is a regression test for https://github.com/uber/tchannel-go/issues/643 |
| 35 | +// We want to ensure that once a connection is closed, there are no references |
| 36 | +// to the closed connection, and the GC frees the connection. |
| 37 | +// We use `runtime.SetFinalizer` to detect whether the GC has freed the object. |
| 38 | +// However, finalizers cannot be set on objects with circular references, |
| 39 | +// so we cannot set a finalizer on the connection, but instead set a finalizer |
| 40 | +// on a field of the connection which has the same lifetime. The connection |
| 41 | +// logger is unique per connection and does not have circular references |
| 42 | +// so we can use the logger, but need a pointer for `runtime.SetFinalizer`. |
| 43 | +// loggerPtr is a Logger implementation that uses a pointer unlike other |
| 44 | +// TChannel loggers. |
| 45 | +type loggerPtr struct { |
| 46 | + Logger |
| 47 | +} |
| 48 | + |
| 49 | +func (l *loggerPtr) WithFields(fields ...LogField) Logger { |
| 50 | + return &loggerPtr{l.Logger.WithFields(fields...)} |
| 51 | +} |
| 52 | + |
| 53 | +func TestPeerConnectionLeaks(t *testing.T) { |
| 54 | + // Disable log verification since we want to set our own logger. |
| 55 | + opts := testutils.NewOpts().NoRelay().DisableLogVerification() |
| 56 | + opts.Logger = &loggerPtr{NullLogger} |
| 57 | + |
| 58 | + connFinalized := make(chan struct{}) |
| 59 | + setFinalizer := func(p *Peer, hostPort string) { |
| 60 | + ctx, cancel := NewContext(time.Second) |
| 61 | + defer cancel() |
| 62 | + |
| 63 | + conn, err := p.GetConnection(ctx) |
| 64 | + require.NoError(t, err, "Failed to get connection") |
| 65 | + |
| 66 | + runtime.SetFinalizer(conn.Logger(), func(interface{}) { |
| 67 | + close(connFinalized) |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + testutils.WithTestServer(t, opts, func(ts *testutils.TestServer) { |
| 72 | + s2Opts := testutils.NewOpts().SetServiceName("s2") |
| 73 | + s2Opts.Logger = NewLogger(ioutil.Discard) |
| 74 | + s2 := ts.NewServer(s2Opts) |
| 75 | + |
| 76 | + // Set a finalizer to detect when the connection from s1 -> s2 is freed. |
| 77 | + peer := ts.Server().Peers().GetOrAdd(s2.PeerInfo().HostPort) |
| 78 | + setFinalizer(peer, s2.PeerInfo().HostPort) |
| 79 | + |
| 80 | + // Close s2, so that the connection in s1 to s2 is released. |
| 81 | + s2.Close() |
| 82 | + closed := testutils.WaitFor(time.Second, s2.Closed) |
| 83 | + require.True(t, closed, "s2 didn't close") |
| 84 | + |
| 85 | + // Trigger the GC which will call the finalizer, and ensure |
| 86 | + // that the connection logger was finalized. |
| 87 | + finalized := testutils.WaitFor(time.Second, func() bool { |
| 88 | + runtime.GC() |
| 89 | + select { |
| 90 | + case <-connFinalized: |
| 91 | + return true |
| 92 | + default: |
| 93 | + return false |
| 94 | + } |
| 95 | + }) |
| 96 | + require.True(t, finalized, "Connection was not freed") |
| 97 | + }) |
| 98 | +} |
0 commit comments