Skip to content

Commit

Permalink
Merge pull request #91 from EvolveGlobal/bugfix/sigabrt
Browse files Browse the repository at this point in the history
[Bugfix] Fix SIGABRT in KqueueSelector
  • Loading branch information
Goos authored May 18, 2021
2 parents 065b6ce + 6f24fa6 commit ffc4730
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions Sources/KqueueSelector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,8 @@ public final class KqueueSelector: Selector {
}

// register events to kqueue

// Notice: we need to get the event count before we go into
// `withUnsafeMutableBufferPointer`, as we cannot rely on it inside the closure
// (you can read the offical document)
let keventCount = kevents.count
guard kevents.withUnsafeMutableBufferPointer({ pointer in
kevent(kqueue, pointer.baseAddress, Int32(keventCount), nil, Int32(0), nil) >= 0
kevent(kqueue, pointer.baseAddress, Int32(pointer.count), nil, Int32(0), nil) >= 0
}) else {
throw OSError.lastIOError()
}
Expand Down Expand Up @@ -107,13 +102,8 @@ public final class KqueueSelector: Selector {
}

// unregister events from kqueue

// Notice: we need to get the event count before we go into
// `withUnsafeMutableBufferPointer`, as we cannot rely on it inside the closure
// (you can read the offical document)
let keventCount = kevents.count
guard kevents.withUnsafeMutableBufferPointer({ pointer in
kevent(kqueue, pointer.baseAddress, Int32(keventCount), nil, Int32(0), nil) >= 0
kevent(kqueue, pointer.baseAddress, Int32(pointer.count), nil, Int32(0), nil) >= 0
}) else {
throw OSError.lastIOError()
}
Expand All @@ -126,7 +116,6 @@ public final class KqueueSelector: Selector {

public func select(timeout: TimeInterval?) throws -> [(SelectorKey, Set<IOEvent>)] {
var timeSpec: timespec?
let timeSpecPointer: UnsafePointer<timespec>?
if let timeout = timeout {
if timeout > 0 {
var integer = 0.0
Expand All @@ -135,21 +124,20 @@ public final class KqueueSelector: Selector {
} else {
timeSpec = timespec()
}
timeSpecPointer = withUnsafePointer(to: &timeSpec!) { $0 }
} else {
timeSpecPointer = nil
}

var kevents = Array<Darwin.kevent>(repeating: Darwin.kevent(), count: selectMaximumEvent)
let eventCount = kevents.withUnsafeMutableBufferPointer { pointer in
return kevent(
kqueue,
nil,
0,
pointer.baseAddress,
Int32(selectMaximumEvent),
timeSpecPointer
)
let eventCount:Int32 = kevents.withUnsafeMutableBufferPointer { pointer in
return withUnsafeOptionalPointer(to: &timeSpec) { timeSpecPointer in
return kevent(
kqueue,
nil,
0,
pointer.baseAddress,
Int32(selectMaximumEvent),
timeSpecPointer
)
}
}
guard eventCount >= 0 else {
throw OSError.lastIOError()
Expand All @@ -167,14 +155,26 @@ public final class KqueueSelector: Selector {
}
fileDescriptorIOEvents[fileDescriptor] = ioEvents
}
return Array(fileDescriptorIOEvents.map { (fileDescriptorMap[$0.0]!, $0.1) })
let fdMap = fileDescriptorMap
return fileDescriptorIOEvents.compactMap { [weak self] event in
fdMap[event.0].map { ($0, event.1) } ?? nil
}
}

public subscript(fileDescriptor: Int32) -> SelectorKey? {
get {
return fileDescriptorMap[fileDescriptor]
}
}

private func withUnsafeOptionalPointer<T, Result>(to: inout T?, body: (UnsafePointer<T>?) throws -> Result) rethrows -> Result {
if to != nil {
return try withUnsafePointer(to: &to!) { try body($0) }
} else {
return try body(nil)
}
}

}

#endif

0 comments on commit ffc4730

Please sign in to comment.