Skip to content

Deprecate async version of withValue and introduce sync version. #1792

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 1 commit into from
Jan 5, 2023

Conversation

mbrandonw
Copy link
Member

@mbrandonw mbrandonw commented Jan 5, 2023

We made withValue async on ActorIsolated, but I don't think we really meant to. That makes the method re-entrant, which means inflight operations can overwrite each other.

For example, this test fails when it shouldn't:

func testAsyncWithValue() async {
  let xs = ActorIsolated<[Int]>([])

  let task1 = Task {
    await xs.withValue {
      try? await Task.sleep(nanoseconds: NSEC_PER_MSEC * 100)
      $0.append(1)
    }
  }
  let task2 = Task {
    await xs.withValue {
      try? await Task.sleep(nanoseconds: NSEC_PER_MSEC * 200)
      $0.append(2)
    }
  }
  let task3 = Task {
    await xs.withValue {
      try? await Task.sleep(nanoseconds: NSEC_PER_MSEC * 300)
      $0.append(3)
    }
  }

  await task1.value
  await task2.value
  await task3.value
  let value = await xs.value
  XCTAssertEqual(value, [1, 2, 3])
}

@mbrandonw mbrandonw requested a review from stephencelis January 5, 2023 03:41
@mbrandonw mbrandonw merged commit 5eedf98 into main Jan 5, 2023
@mbrandonw mbrandonw deleted the fix-actor-isolated branch January 5, 2023 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants