Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit f8b9fa5

Browse files
authored
Fix VPN memory pressure monitor (#3535)
Task/Issue URL: https://app.asana.com/0/414235014887631/1208695924903429/f Tech Design URL: CC: Description: This PR fixes the memory pressure monitor. Previously, the memory source and queue were not being retained in any way, so they would deallocate instantly. Now, the memory source and queue are kept as properties on the packet tunnel provider.
1 parent 76b1f76 commit f8b9fa5

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

PacketTunnelProvider/NetworkProtection/NetworkProtectionPacketTunnelProvider.swift

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -479,28 +479,33 @@ final class NetworkProtectionPacketTunnelProvider: PacketTunnelProvider {
479479
APIRequest.Headers.setUserAgent(DefaultUserAgentManager.duckDuckGoUserAgent)
480480
}
481481

482+
deinit {
483+
memoryPressureSource?.cancel()
484+
memoryPressureSource = nil
485+
}
486+
487+
private var memoryPressureSource: DispatchSourceMemoryPressure?
488+
private let memoryPressureQueue = DispatchQueue(label: "com.duckduckgo.mobile.ios.NetworkExtension.memoryPressure")
489+
482490
private func startMonitoringMemoryPressureEvents() {
483-
let source = DispatchSource.makeMemoryPressureSource(eventMask: .all, queue: nil)
484-
485-
let queue = DispatchQueue.init(label: "com.duckduckgo.mobile.ios.alpha.NetworkExtension.memoryPressure")
486-
queue.async {
487-
source.setEventHandler {
488-
let event: DispatchSource.MemoryPressureEvent = source.mask
489-
print(event)
490-
switch event {
491-
case DispatchSource.MemoryPressureEvent.normal:
492-
break
493-
case DispatchSource.MemoryPressureEvent.warning:
494-
DailyPixel.fire(pixel: .networkProtectionMemoryWarning)
495-
case DispatchSource.MemoryPressureEvent.critical:
496-
DailyPixel.fire(pixel: .networkProtectionMemoryCritical)
497-
default:
498-
break
499-
}
491+
let source = DispatchSource.makeMemoryPressureSource(eventMask: .all, queue: memoryPressureQueue)
500492

493+
source.setEventHandler { [weak source] in
494+
guard let source else { return }
495+
496+
let event = source.data
497+
498+
if event.contains(.warning) {
499+
Logger.networkProtectionMemory.warning("Received memory pressure warning")
500+
DailyPixel.fire(pixel: .networkProtectionMemoryWarning)
501+
} else if event.contains(.critical) {
502+
Logger.networkProtectionMemory.warning("Received memory pressure critical warning")
503+
DailyPixel.fire(pixel: .networkProtectionMemoryCritical)
501504
}
502-
source.resume()
503505
}
506+
507+
self.memoryPressureSource = source
508+
source.activate()
504509
}
505510

506511
private func observeServerChanges() {

0 commit comments

Comments
 (0)