Skip to content

Commit

Permalink
Merge pull request wikimedia#3724 from wikimedia/teardown_session
Browse files Browse the repository at this point in the history
Teardown Session so that all in-flight URLSessionTasks are canceled
  • Loading branch information
tonisevener committed Oct 2, 2020
2 parents 5b98a4b + a309a36 commit 4f18f4c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
3 changes: 3 additions & 0 deletions WMF Framework/WidgetController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ public final class WidgetController: NSObject {
completion()
finishBackgroundActivity()
#if DEBUG
guard !self.isCreatingDataStore, self._dataStore == nil else { // Don't check open files if another MWKDataStore was created after this one was destroyed
return
}
let openFiles = self.openFilePaths()
let openSqliteFile = openFiles.first(where: { $0.hasSuffix(".sqlite") })
assert(openSqliteFile == nil, "There should be no open sqlite files (which in our case are Core Data persistent stores) in the shared app container after the data store is released. The widget still has a lock on these files: \(openFiles)")
Expand Down
1 change: 1 addition & 0 deletions Wikipedia/Code/MWKDataStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ - (instancetype)initWithContainerURL:(NSURL *)containerURL {

- (void)teardown:(nullable dispatch_block_t)completion {
if (self.cacheController) {
[self.session teardown];
[self.cacheController teardown:^{
if (completion) {
completion();
Expand Down
9 changes: 6 additions & 3 deletions Wikipedia/Code/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,15 @@ public class Session: NSObject {
}

deinit {
defaultURLSession.invalidateAndCancel()
teardown()
}

public func cancelAllRequests() {
@objc public func teardown() {
guard defaultURLSession !== URLSession.shared else { // [NSURLSession sharedSession] may not be invalidated
return
}
defaultURLSession.invalidateAndCancel()
defaultURLSession = Session.getURLSession(with: permanentCache, delegate: sessionDelegate)
defaultURLSession = URLSession.shared
}

public let wifiOnlyURLSession: URLSession = {
Expand Down
2 changes: 1 addition & 1 deletion WikipediaUnitTests/Code/ArticleViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ArticleViewControllerTests: XCTestCase {
XCTAssert(schemeHandler.accessed, "SchemeHandler was not accessed during article load.")
UIApplication.shared.keyWindow?.rootViewController = nil
dataStore.clearTemporaryCache()
dataStore.session.cancelAllRequests()
dataStore.session.teardown()
}

UIApplication.shared.keyWindow?.rootViewController = articleVC
Expand Down

0 comments on commit 4f18f4c

Please sign in to comment.