午夜视频在线网站,日韩视频精品在线,中文字幕精品一区二区三区在线,在线播放精品,1024你懂我懂的旧版人,欧美日韩一级黄色片,一区二区三区在线观看视频

分享

crawshaw

 圣地亞哥rqb0dv 2020-04-10

Sharp-Edged Finalizers in Go

2018-04-05, David Crawshaw

For background, see my last post on why in general finalizers do not work.

We cannot use an object finalizer for resource management, because finalizers are called at some unpredictable distant time long after resources need to be reclaimed.

However, a finalizer does provide us with a bound on when a managed resource needs to have been released. If we reach an object finalizer, and a manually managed resource has not been freed, then there is a bug in the program.

So we can use finalizers to detect resource leaks.

package db 

func Open(path string, flags OpenFlags) (*Conn, error) {
	// ...

	runtime.SetFinalizer(conn, func(conn *Conn) {
		panic("open db connection never closed")
	})
	return conn, nil
}

func (c *Conn) Close() {
	// ...
	runtime.SetFinalizer(conn, nil) // clear finalizer
}

This is a sharp-edged finalizer. Misuse the resource and it will cut your program short.

I suspect this kind of aggressive finalizer is off-putting to many, who view resource management something nice to have. But there are many programs for which correct resource management is vital. Leaking a resource can leave to unsuspecting crashes, or data loss. For people in similar situations, you may want to consider a panicing finalizer.

Debugging

One big problem with the code above is the error message is rubbish. You leaked something. OK, great. Got any details?

Ideally the error would point at exactly where we need to release the resource, but this is a hard problem. One cheap and easy alternative is to point to where the resource was originally acquired, which is straightforward:

_, file, line, _ := runtime.Caller(1)
runtime.SetFinalizer(conn, func(conn *Conn) {
	panic(fmt.Sprintf("%s:%d: db conn not closed", file, line))
})

This prints the file name and line number of where the connection was created, which is often useful in tracing a leaked resource.

Why not just log?

Because I have found myself ignoring logs, time and again.

While working on an sqlite wrapper package most of the leaked resources I encountered were in the tests of programs using the package. A log line in the middle of go test will never be seen. A panic will.

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多