Przeglądaj źródła

Make withRecover raceless for real

Properly fixes #52 this time.

Almost exactly a year ago (+ 3 days) when Flo added the withRecover function we
discussed how if the user set/unset the PanicHandler in time with an actual
panic they could cause secondary panics and weirdness. We decided putting a
mutex in was overkill and closed the issue.

It just occured to me that there is a 1000x better fix, which is to just cache
the handler value locally so we know we're calling the same value we do our
nil-check on. Duh. I'm not sure why neither of us thought of this at the time...
Evan Huus 11 lat temu
rodzic
commit
87f20fcd6d
1 zmienionych plików z 3 dodań i 2 usunięć
  1. 3 2
      utils.go

+ 3 - 2
utils.go

@@ -19,9 +19,10 @@ func (slice int32Slice) Swap(i, j int) {
 
 
 func withRecover(fn func()) {
 func withRecover(fn func()) {
 	defer func() {
 	defer func() {
-		if PanicHandler != nil {
+		handler := PanicHandler
+		if handler != nil {
 			if err := recover(); err != nil {
 			if err := recover(); err != nil {
-				PanicHandler(err)
+				handler(err)
 			}
 			}
 		}
 		}
 	}()
 	}()