Skip to content
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

Async repeating channel return before task completion #17

Open
adam-rocska opened this issue Aug 2, 2024 · 1 comment
Open

Async repeating channel return before task completion #17

adam-rocska opened this issue Aug 2, 2024 · 1 comment
Labels

Comments

@adam-rocska
Copy link
Member

AI says: "The AsyncRepeatingChannel is returned before the Task completes. This could lead to race conditions if the caller starts using the channel immediately. Consider awaiting the task group completion before returning the channel."

Related to this:

public typealias AsyncRepeatingChannel<T: Sendable> = AsyncChannel<(iteration: Int, result: T)>
@discardableResult
@inlinable
public func repeating<T: Sendable>(
  count: Int,
  _ function: @escaping (Int) async -> T
) async -> AsyncRepeatingChannel<T> {
  let channel = AsyncRepeatingChannel<T>()

  Task {
    await withTaskGroup(of: Void.self) { group in
      for i in 0..<count {
        group.addTask { await channel.send((i, function(i))) }
      }
    }
    channel.finish()
  }

  return channel
}

on the 2nd of August, 2024.

I'm leaning in, but wanna give it a second thought and a bunch of tests before I accept it.
This funciton is in use for more than a year in prod by many projects and individuals, and so far it didn't explode. I'm not saying that this is proof by any means, just a reason why I defer this topic.

@adam-rocska
Copy link
Member Author

Same applies to this too:

"The @autoclosure attribute is used with an async function, which might lead to unexpected behavior. Consider removing @autoclosure or documenting its intended use."

@discardableResult
@inlinable
public func repeating<T: Sendable>(
  count: Int,
  _ function: @escaping @autoclosure () async -> T
) async -> AsyncRepeatingChannel<T> {
  await repeating(count: count) { _ in await function() }
}

It could be right, and I might be wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant