4 ms·
The simplest example is that you're probably writing this function: func Foo() error { m.Lock() defer m.Unlock() return fooWithLockHeld()
by knorker 3y ago
The simplest example is that you're probably writing this function:
func Foo() error {
m.Lock()
defer m.Unlock()
return fooWithLockHeld()
}
But your less experienced colleague was told by Rob Pike himself that the language doesn't have exceptions, so writes:
func Foo() error {
m.Lock()
err := fooWithLockHeld()
m.Unlock()
return err
}
That code is not exception safe. It may be a contrived example, but in code review I've many times seen real code that is not exception safe, for basically the same reason.
A held lock is fairly benign (only causes a deadlock, not corrupted data) if the net/http.Server swallows it in your handler, or fmt.Print swallows it. But some other errors are not as benign.