Skip to content

Commit 8046a7b

Browse files
committed
Defer post-finalizer actions.
This could prevent a deadlock, in the event that f panics and something tries to recover.
1 parent 6a1ce94 commit 8046a7b

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

cstruct.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,17 @@ func (c *cStruct) doClose(f func() error) error {
6464
if c.parent != nil {
6565
c.parent.rLock()
6666
}
67+
defer func() {
68+
if c.parent != nil {
69+
c.parent.rUnlock()
70+
}
71+
c.cptr = nil
72+
c.parent = nil
73+
c.lock.Unlock()
74+
}()
6775
if c.live() {
6876
err = f()
6977
}
70-
if c.parent != nil {
71-
c.parent.rUnlock()
72-
}
73-
c.cptr = nil
74-
c.parent = nil
75-
c.lock.Unlock()
7678
return err
7779
}
7880

0 commit comments

Comments
 (0)