Skip to content

[5.8][TaskLocals] Avoid use of defer in back deployed functions in the standard library #62947

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

Merged
Merged
Show file tree
Hide file tree
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
11 changes: 8 additions & 3 deletions stdlib/public/BackDeployConcurrency/TaskCancellation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ public func withTaskCancellationHandler<T>(
// unconditionally add the cancellation record to the task.
// if the task was already cancelled, it will be executed right away.
let record = _taskAddCancellationHandler(handler: handler)
defer { _taskRemoveCancellationHandler(record: record) }

return try await operation()
do {
let result = try await operation()
_taskRemoveCancellationHandler(record: record)
return result
} catch {
_taskRemoveCancellationHandler(record: record)
throw error
}
}

@available(SwiftStdlib 5.1, *)
Expand Down
11 changes: 8 additions & 3 deletions stdlib/public/BackDeployConcurrency/TaskLocal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,14 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
_checkIllegalTaskLocalBindingWithinWithTaskGroup(file: file, line: line)

_taskLocalValuePush(key: key, value: valueDuringOperation)
defer { _taskLocalValuePop() }

return try await operation()
do {
let result = try await operation()
_taskLocalValuePop()
return result
} catch {
_taskLocalValuePop()
throw error
}
}

/// Binds the task-local to the specific value for the duration of the
Expand Down
11 changes: 8 additions & 3 deletions stdlib/public/Concurrency/TaskCancellation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ public func withTaskCancellationHandler<T>(
// unconditionally add the cancellation record to the task.
// if the task was already cancelled, it will be executed right away.
let record = _taskAddCancellationHandler(handler: handler)
defer { _taskRemoveCancellationHandler(record: record) }

return try await operation()
do {
let result = try await operation()
_taskRemoveCancellationHandler(record: record)
return result
} catch {
_taskRemoveCancellationHandler(record: record)
throw error
}
}

@available(SwiftStdlib 5.1, *)
Expand Down
11 changes: 8 additions & 3 deletions stdlib/public/Concurrency/TaskLocal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,14 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
_checkIllegalTaskLocalBindingWithinWithTaskGroup(file: file, line: line)

_taskLocalValuePush(key: key, value: valueDuringOperation)
defer { _taskLocalValuePop() }

return try await operation()
do {
let result = try await operation()
_taskLocalValuePop()
return result
} catch {
_taskLocalValuePop()
throw error
}
}

/// Binds the task-local to the specific value for the duration of the
Expand Down