Skip to content

Commit 4461f5b

Browse files
authored
Merge pull request #62946 from tshortli/avoid-defer-in-backdeployed-funcs
[TaskLocals] Avoid use of `defer` in back deployed functions in the standard library
2 parents 0a05cd0 + 087aacc commit 4461f5b

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

stdlib/public/BackDeployConcurrency/TaskCancellation.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ public func withTaskCancellationHandler<T>(
4646
// unconditionally add the cancellation record to the task.
4747
// if the task was already cancelled, it will be executed right away.
4848
let record = _taskAddCancellationHandler(handler: handler)
49-
defer { _taskRemoveCancellationHandler(record: record) }
50-
51-
return try await operation()
49+
do {
50+
let result = try await operation()
51+
_taskRemoveCancellationHandler(record: record)
52+
return result
53+
} catch {
54+
_taskRemoveCancellationHandler(record: record)
55+
throw error
56+
}
5257
}
5358

5459
@available(SwiftStdlib 5.1, *)

stdlib/public/BackDeployConcurrency/TaskLocal.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,14 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
145145
_checkIllegalTaskLocalBindingWithinWithTaskGroup(file: file, line: line)
146146

147147
_taskLocalValuePush(key: key, value: valueDuringOperation)
148-
defer { _taskLocalValuePop() }
149-
150-
return try await operation()
148+
do {
149+
let result = try await operation()
150+
_taskLocalValuePop()
151+
return result
152+
} catch {
153+
_taskLocalValuePop()
154+
throw error
155+
}
151156
}
152157

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

stdlib/public/Concurrency/TaskCancellation.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ public func withTaskCancellationHandler<T>(
4646
// unconditionally add the cancellation record to the task.
4747
// if the task was already cancelled, it will be executed right away.
4848
let record = _taskAddCancellationHandler(handler: handler)
49-
defer { _taskRemoveCancellationHandler(record: record) }
50-
51-
return try await operation()
49+
do {
50+
let result = try await operation()
51+
_taskRemoveCancellationHandler(record: record)
52+
return result
53+
} catch {
54+
_taskRemoveCancellationHandler(record: record)
55+
throw error
56+
}
5257
}
5358

5459
@available(SwiftStdlib 5.1, *)

stdlib/public/Concurrency/TaskLocal.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,14 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
145145
_checkIllegalTaskLocalBindingWithinWithTaskGroup(file: file, line: line)
146146

147147
_taskLocalValuePush(key: key, value: valueDuringOperation)
148-
defer { _taskLocalValuePop() }
149-
150-
return try await operation()
148+
do {
149+
let result = try await operation()
150+
_taskLocalValuePop()
151+
return result
152+
} catch {
153+
_taskLocalValuePop()
154+
throw error
155+
}
151156
}
152157

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

0 commit comments

Comments
 (0)