Skip to content

Commit

Permalink
Avoid panic when using a released CFRunLoop
Browse files Browse the repository at this point in the history
As fsevents is saving the result of `CFRunLoopGetCurrent` and stopping
it from a separate thread, it's necessary to increase the reference
count of the returned CFRunLoopRef.

In the event that the run loop is terminated by other means, `stop`
might be working with a deallocated reference and produce a panic
(SIGSEGV).

closes #39
  • Loading branch information
adriansr authored and nathany committed Jan 5, 2019
1 parent 0739535 commit 972ac2c
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func (es *EventStream) start(paths []string, callbackInfo uintptr) {
go func() {
runtime.LockOSThread()
es.rlref = CFRunLoopRef(C.CFRunLoopGetCurrent())
C.CFRetain(C.CFTypeRef(es.rlref))
C.FSEventStreamScheduleWithRunLoop(es.stream, C.CFRunLoopRef(es.rlref), C.kCFRunLoopDefaultMode)
C.FSEventStreamStart(es.stream)
close(started)
Expand Down Expand Up @@ -262,4 +263,5 @@ func stop(stream FSEventStreamRef, rlref CFRunLoopRef) {
C.FSEventStreamInvalidate(stream)
C.FSEventStreamRelease(stream)
C.CFRunLoopStop(C.CFRunLoopRef(rlref))
C.CFRelease(C.CFTypeRef(rlref))
}

0 comments on commit 972ac2c

Please sign in to comment.