Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition crash by removing unnecessary retain/release #75

Merged
merged 1 commit into from
Jul 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions Sources/NTPConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ final class NTPConnection {
func start(_ callbackQueue: DispatchQueue, onComplete: @escaping NTPConnectionCallback) {
lockQueue.async {
guard !self.started else { return }
self.callbackPending = true
var ctx = CFSocketContext(
version: 0,
info: UnsafeMutableRawPointer(Unmanaged.passRetained(self).toOpaque()),
info: UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()),
retain: nil,
release: nil,
copyDescription: nil
Expand Down Expand Up @@ -136,16 +135,13 @@ final class NTPConnection {

private let dataCallback: CFSocketCallBack = { socket, type, address, data, info in
guard let info = info else { return }
let retainedClient = Unmanaged<NTPConnection>.fromOpaque(info)
let client = retainedClient.takeUnretainedValue()
let client = Unmanaged<NTPConnection>.fromOpaque(info).takeUnretainedValue()
guard let socket = socket, CFSocketIsValid(socket) else { return }

// Can't use switch here as these aren't defined as an enum.
if type == .dataCallBack {
let data = unsafeBitCast(data, to: CFData.self) as Data
client.callbackPending = false
client.handleResponse(data)
retainedClient.release()
} else if type == .writeCallBack {
client.debugLog("Buffer \(client.address) writable - requesting time")
client.requestTime()
Expand All @@ -169,7 +165,6 @@ final class NTPConnection {
private var source: CFRunLoopSource?
private var startTime: ntp_time_t?
private var finished: Bool = false
private var callbackPending: Bool = false
}

extension NTPConnection: TimedOperation {
Expand Down Expand Up @@ -201,10 +196,6 @@ private extension NTPConnection {
onComplete(self, result)
}
}
if callbackPending {
callbackPending = false
Unmanaged.passUnretained(self).release()
}
}

func requestTime() {
Expand Down