Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 01aa4eb

Browse files
os: refactor to use runtime.Addcleanup
change expected leaked cleanups and/or finalizers from 2 to 5. 3 is come from stdin stdout stderr Change-Id: I2f971772229ee642d5a11e2271a010ac7ffdbf52
1 parent bca3e98 commit 01aa4eb

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

‎src/os/file_unix.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type file struct {
6363
nonblock bool // whether we set nonblocking mode
6464
stdoutOrErr bool // whether this is stdout or stderr
6565
appendMode bool // whether file is opened for appending
66+
cleanup runtime.Cleanup
6667
}
6768

6869
// fd is the Unix implementation of Fd.
@@ -221,7 +222,9 @@ func newFile(fd int, name string, kind newFileKind, nonBlocking bool) *File {
221222
}
222223
}
223224

224-
runtime.SetFinalizer(f.file, (*file).close)
225+
f.file.cleanup = runtime.AddCleanup(f.file, func(_ int) {
226+
f.file.close()
227+
}, 0)
225228
return f
226229
}
227230

@@ -319,7 +322,7 @@ func (file *file) close() error {
319322
}
320323

321324
// no need for a finalizer anymore
322-
runtime.SetFinalizer(file, nil)
325+
file.cleanup.Stop()
323326
return err
324327
}
325328

‎src/os/file_windows.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type file struct {
2929
name string
3030
dirinfo atomic.Pointer[dirInfo] // nil unless directory being read
3131
appendMode bool // whether file is opened for appending
32+
cleanup runtime.Cleanup
3233
}
3334

3435
// fd is the Windows implementation of Fd.
@@ -68,7 +69,9 @@ func newFile(h syscall.Handle, name string, kind string, nonBlocking bool) *File
6869
},
6970
name: name,
7071
}}
71-
runtime.SetFinalizer(f.file, (*file).close)
72+
f.file.cleanup = runtime.AddCleanup(f.file, func(_ int) {
73+
f.file.close()
74+
}, 0)
7275

7376
// Ignore initialization errors.
7477
// Assume any problems will show up in later I/O.
@@ -144,7 +147,7 @@ func (file *file) close() error {
144147
}
145148

146149
// no need for a finalizer anymore
147-
runtime.SetFinalizer(file, nil)
150+
file.cleanup.Stop()
148151
return err
149152
}
150153

‎src/os/root_openat.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ type root struct {
2222
// refs is incremented while an operation is using fd.
2323
// closed is set when Close is called.
2424
// fd is closed when closed is true and refs is 0.
25-
mu sync.Mutex
26-
fd sysfdType
27-
refs int // number of active operations
28-
closed bool // set when closed
25+
mu sync.Mutex
26+
fd sysfdType
27+
refs int // number of active operations
28+
closed bool // set when closed
29+
cleanup runtime.Cleanup
2930
}
3031

3132
func (r *root) Close() error {
@@ -35,7 +36,7 @@ func (r *root) Close() error {
3536
syscall.Close(r.fd)
3637
}
3738
r.closed = true
38-
runtime.SetFinalizer(r, nil) // no need for a finalizer any more
39+
r.cleanup.Stop()
3940
return nil
4041
}
4142

‎src/os/root_unix.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ func newRoot(fd int, name string) (*Root, error) {
5656
fd: fd,
5757
name: name,
5858
}}
59-
runtime.SetFinalizer(r.root, (*root).Close)
59+
r.root.cleanup = runtime.AddCleanup(r.root, func(_ int) {
60+
r.root.Close()
61+
}, 0)
6062
return r, nil
6163
}
6264

‎src/runtime/gc_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,8 +1080,8 @@ func TestDetectFinalizerAndCleanupLeaks(t *testing.T) {
10801080
if len(sp) != 2 {
10811081
t.Fatalf("expected the runtime to throw, got:\n%s", got)
10821082
}
1083-
if strings.Count(sp[0], "is reachable from")!= 2 {
1084-
t.Fatalf("expected exactly two leaked cleanups and/or finalizers, got:\n%s", got)
1083+
if count:=strings.Count(sp[0], "is reachable from"); count!= 5 {
1084+
t.Fatalf("expected exactly five leaked cleanups and/or finalizers, got: %d \n%s", count, got)
10851085
}
10861086
// N.B. Disable in race mode and in asan mode. Both disable the tiny allocator.
10871087
wantSymbolizedLocations := 2
@@ -1091,7 +1091,7 @@ func TestDetectFinalizerAndCleanupLeaks(t *testing.T) {
10911091
}
10921092
wantSymbolizedLocations++
10931093
}
1094-
if strings.Count(sp[0], "main.DetectFinalizerAndCleanupLeaks()") != wantSymbolizedLocations {
1095-
t.Fatalf("expected %d symbolized locations, got:\n%s", wantSymbolizedLocations, got)
1094+
if actual:=strings.Count(sp[0], "main.DetectFinalizerAndCleanupLeaks()"); actual != wantSymbolizedLocations {
1095+
t.Fatalf("expected %d symbolized locations, actual %d symbolized locations, got:\n%s", actual, wantSymbolizedLocations, got)
10961096
}
10971097
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /