Skip to content

Commit bb2aa86

Browse files
asimshankartensorflower-gardener
authored andcommitted
go: Prevent a Graph from being GCed while an Operation is alive.
And also add trivial accessors for Name() and Type() to the Operation type. The lifetime of TF_Operation* objects is tied to the lifetime of the TF_Graph* that they come from in the C API. Prior to this commit, it was possible for the Go Graph object to be garbage collected even while the Operation object is still alive. The added test demonstrates this problem and fails with the following if the Operation type doesn't hold on to a reference to the Graph: === RUN TestOperationLifetime fatal error: unexpected signal during runtime execution [signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x886ebc4] runtime stack: runtime.throw(0xc73afc7, 0x2a) go/gc/src/runtime/panic.go:569 +0x9e runtime.sigpanic() go/gc/src/runtime/sigpanic_unix.go:12 +0x2cc goroutine 34 [syscall, locked to thread]: runtime.cgocall(0x7733410, 0xc420055e28, 0x0) go/gc/src/runtime/cgocall.go:131 +0x110 fp=0xc420055df8 sp=0xc420055db8 pc=0x75e64a0 tensorflow/go/tensorflow._Cfunc_TF_OperationName(0x7f4c9f59b790, 0x0) ??:0 +0x4a fp=0xc420055e28 sp=0xc420055df8 pc=0x764884a tensorflow/go/tensorflow.(*Operation).Name(0xc4201760c0, 0x0, 0x0) /build/work/c9dcfe7aa482d82c72c0b2b6c0351d7a/tensorflow/go/operation.go:32 +0x78 fp=0xc420055e70 sp=0xc420055e28 pc=0x7649248 tensorflow/go/tensorflow.TestOperationLifetime(0xc4201a00c0) tensorflow/go/operation_test.go:45 +0x7e fp=0xc420055f68 sp=0xc420055e70 pc=0x764409e testing.tRunner(0xc4201a00c0, 0xc748ed0) go/gc/src/testing/testing.go:610 +0x81 fp=0xc420055f90 sp=0xc420055f68 pc=0x76589a1 runtime.goexit() go/gc/src/runtime/asm_amd64.s:2086 +0x1 fp=0xc420055f98 sp=0xc420055f90 pc=0x7642c91 created by testing.(*T).Run go/gc/src/testing/testing.go:646 +0x2ec This is yet another step towards tensorflow#10 Change: 134086736
1 parent 06324c7 commit bb2aa86

2 files changed

Lines changed: 86 additions & 2 deletions

File tree

‎tensorflow/go/operation.go‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ import "unsafe"
2222
// Operation that has been added to the graph.
2323
type Operation struct {
2424
c *C.TF_Operation
25+
// A reference to the Graph to prevent it from
26+
// being GCed while the Operation is still alive.
27+
g *Graph
28+
}
29+
30+
// Name returns the name of the operation.
31+
func (op *Operation) Name() string {
32+
return C.GoString(C.TF_OperationName(op.c))
33+
}
34+
35+
// Type returns the name of the operator used by this operation.
36+
func (op *Operation) Type() string {
37+
return C.GoString(C.TF_OperationOpType(op.c))
2538
}
2639

2740
// Output represents one of the outputs of an operation in the graph. Has a
@@ -44,12 +57,15 @@ func (p *Output) c() C.TF_Port {
4457
// Build() must be called for any in-progress Operation, or else we leak.
4558
type opBuilder struct {
4659
c *C.TF_OperationDescription
60+
// A reference to the Graph to prevent it from being GCed while
61+
// the opBuilder is still alive.
62+
g *Graph
4763
}
4864

4965
func newOpBuilder(g *Graph, typ string, name string) *opBuilder {
5066
opType := C.CString(typ)
5167
opName := C.CString(name)
52-
b := &opBuilder{c: C.TF_NewOperation(g.c, opType, opName)}
68+
b := &opBuilder{c: C.TF_NewOperation(g.c, opType, opName), g: g}
5369
C.free(unsafe.Pointer(opType))
5470
C.free(unsafe.Pointer(opName))
5571
return b
@@ -75,7 +91,7 @@ func (b *opBuilder) AddInput(port Output) {
7591

7692
func (b *opBuilder) Build() (*Operation, error) {
7793
status := newStatus()
78-
op := &Operation{c: C.TF_FinishOperation(b.c, status.c)}
94+
op := &Operation{c: C.TF_FinishOperation(b.c, status.c), g: b.g}
7995
if err := status.Err(); err != nil {
8096
return nil, err
8197
}

‎tensorflow/go/operation_test.go‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package tensorflow
16+
17+
import (
18+
"runtime"
19+
"runtime/debug"
20+
"testing"
21+
)
22+
23+
// createGraphAndOp creates an Operation but loses the reference to the Graph.
24+
func createGraphAndOp() (*Operation, error) {
25+
t, err := NewTensor(int64(1))
26+
if err != nil {
27+
return nil, err
28+
}
29+
g := NewGraph()
30+
output, err := Placeholder(g, "my_placeholder", t.DataType())
31+
if err != nil {
32+
return nil, err
33+
}
34+
return output.Op, nil
35+
}
36+
37+
func TestOperationLifetime(t *testing.T) {
38+
// Ensure that the Graph is not garbage collected while the program
39+
// still has access to the Operation.
40+
op, err := createGraphAndOp()
41+
if err != nil {
42+
t.Fatal(err)
43+
}
44+
forceGC()
45+
if got, want := op.Name(), "my_placeholder"; got != want {
46+
t.Errorf("Got '%s', want '%s'", got, want)
47+
}
48+
if got, want := op.Type(), "Placeholder"; got != want {
49+
t.Errorf("Got '%s', want '%s'", got, want)
50+
}
51+
}
52+
53+
func forceGC() {
54+
var mem runtime.MemStats
55+
runtime.ReadMemStats(&mem)
56+
// It was empirically observed that without this extra allocation
57+
// TestOperationLifetime would fail only 50% of the time if
58+
// Operation did not hold on to a reference to Graph. With this
59+
// additional allocation, and with the bug where Operation does
60+
// not hold onto a Graph, the test failed 90+% of the time.
61+
//
62+
// The author is aware that this technique is potentially fragile
63+
// and fishy. Suggestions for alternatives are welcome.
64+
bytesTillGC := mem.NextGC - mem.HeapAlloc + 1
65+
_ = make([]byte, bytesTillGC)
66+
runtime.GC()
67+
debug.FreeOSMemory()
68+
}

0 commit comments

Comments
 (0)