From 29885401f33529fba21b5cc85985e606d553e608 Mon Sep 17 00:00:00 2001 From: Jay Herron Date: Sat, 18 Apr 2026 11:14:37 -0600 Subject: [PATCH] fix: Changes detached Tasks to child tasks This improves the cancellation behavior of the DataLoader fulfillment tasks in cases where only single keys are loaded It also removes data races associated with `prime` where the value may not be fully primed by the time the function succeeds. --- Sources/AsyncDataLoader/DataLoader.swift | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Sources/AsyncDataLoader/DataLoader.swift b/Sources/AsyncDataLoader/DataLoader.swift index 705168a..4a40360 100644 --- a/Sources/AsyncDataLoader/DataLoader.swift +++ b/Sources/AsyncDataLoader/DataLoader.swift @@ -51,6 +51,8 @@ public actor DataLoader { queue.append((key: key, channel: channel)) if let executionPeriod = options.executionPeriod, !dispatchScheduled { + // This task must be detached in order to fulfill potential other keys + // even if the originally triggering key is cancelled. Task.detached { try await Task.sleep(nanoseconds: executionPeriod) try await self.execute() @@ -59,7 +61,7 @@ public actor DataLoader { dispatchScheduled = true } } else { - Task.detached { + Task { do { let results = try await self.batchLoadFunction([key]) @@ -158,11 +160,7 @@ public actor DataLoader { if cache[cacheKey] == nil { let channel = Channel() - - Task.detached { - await channel.fulfill(value) - } - + await channel.fulfill(value) cache[cacheKey] = channel }