Skip to content

Commit

Permalink
Merge pull request #1001 from Infomaniak/preventLoopInCancelSession
Browse files Browse the repository at this point in the history
Prevent loop in cancel session
  • Loading branch information
PhilippeWeidmann committed Jun 27, 2023
2 parents b78f0c5 + b255e2b commit c0c515a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,19 @@ extension UploadOperation {
case .unableToBuildRequest:
file.error = DriveError.localError.wrapping(error)

case .uploadSessionTaskMissing,
.uploadSessionInvalid,
.unableToMatchUploadChunk,
case .unableToMatchUploadChunk,
.splitError,
.chunkError,
.fileIdentityHasChanged,
.parseError,
.missingChunkHash:
self.cleanUploadFileSession(file: file)
self.cleanUploadFileSession(file: file, remotely: true)
file.error = DriveError.localError.wrapping(error)

case .uploadSessionTaskMissing,
.uploadSessionInvalid:
// We do not clean remotely, as we expect the session to not exist remotely anymore
self.cleanUploadFileSession(file: file, remotely: false)
file.error = DriveError.localError.wrapping(error)

case .operationFinished, .operationCanceled:
Expand Down
16 changes: 12 additions & 4 deletions kDriveCore/Data/UploadQueue/Operation/UploadOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public final class UploadOperation: AsynchronousOperation, UploadOperationable,
return fileUrl
}

public func cleanUploadFileSession(file: UploadFile? = nil) {
public func cleanUploadFileSession(file: UploadFile? = nil, remotely: Bool = true) {
Log.uploadOperation("Clean uploading session for \(uploadFileId)")

let cleanFileClosure: (UploadFile) -> Void = { file in
Expand All @@ -436,16 +436,24 @@ public final class UploadOperation: AsynchronousOperation, UploadOperationable,
return
}

guard remotely else {
return
}

// Clean the remote session, and current tasks, to free resources.
let driveId = file.driveId
let userId = file.userId
self.enqueueCatching {
let driveFileManager = try self.getDriveFileManager(for: driveId, userId: userId)
self.enqueue {
guard let driveFileManager = try? self.getDriveFileManager(for: driveId, userId: userId) else {
return
}

let abstractToken = AbstractTokenWrapper(token: sessionTokenToCancel)
let apiFetcher = driveFileManager.apiFetcher
let drive = driveFileManager.drive

let cancelledSession = try await apiFetcher.cancelSession(drive: drive, sessionToken: abstractToken)
// We try to cancel the upload session, we do not watch results
let cancelledSession = try? await apiFetcher.cancelSession(drive: drive, sessionToken: abstractToken)
Log.uploadOperation("remove cancelledSession:\(cancelledSession) for \(self.uploadFileId)")

for (key, value) in self.uploadTasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public protocol UploadOperationable: Operationable {

/// Clean the local session and send an API call to free the session
/// - Parameter file: An UploadFile within a transaction
func cleanUploadFileSession(file: UploadFile?)
/// - Parameter remotely: try to delete remote session object if true
func cleanUploadFileSession(file: UploadFile?, remotely: Bool)

/// Process errors and terminate the operation
func end()
Expand Down
2 changes: 1 addition & 1 deletion kDriveCore/Data/UploadQueue/Queue/UploadQueue+Queue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ extension UploadQueue: UploadQueueable {
concurrentQueue.async {
if let operation = self.keyedUploadOperations.getObject(forKey: uploadFileId) {
Log.uploadQueue("operation to cancel:\(operation)")
operation.cleanUploadFileSession(file: nil)
operation.cleanUploadFileSession(file: nil, remotely: true)
operation.cancel()
}
self.keyedUploadOperations.removeObject(forKey: uploadFileId)
Expand Down

0 comments on commit c0c515a

Please sign in to comment.