Skip to content

Crash task-local: illegal task-local value binding #110

Answered by stephencelis
corysullivan asked this question in Q&A
Discussion options

You must be logged in to vote

@corysullivan That error is due to executing task local code inside an invalid context, like within a withTaskGroup block but outside an addTask. Here's a vanilla test case demonstrating the issue:

func testIllegalTaskLocal() async {
  enum Test {
    @TaskLocal static var isSetting = false
  }

  await withTaskGroup(of: Void.self) { group in
    Test.$isSetting.withValue(true) {
      print(Test.isSetting)
    }
    print(Test.isSetting)
  }
}

If you follow your trace to the effect causing the crash, you're likely in a withTaskGroup context. You'll want to avoid accessing dependencies in that scope, and instead move dependency access inside group.addTask, or outside the task group ent…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@corysullivan
Comment options

Answer selected by corysullivan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants