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

LockIsolated.withValue closure should be sendable #13

Merged
merged 1 commit into from Nov 8, 2023

Conversation

stephencelis
Copy link
Member

Brought up by @kabiroberai in Slack with this demo of the issue:

class Bad {
  var i = 0
  func racy() { i += 1 }
}

let bad = LockIsolated(Bad())

var _bad: Bad?
bad.withValue { _bad = $0 }
_bad?.racy()  // not lock-isolated!

extension LockIsolated: Equatable where Value: Equatable {
public static func == (lhs: LockIsolated, rhs: LockIsolated) -> Bool {
lhs.withValue { lhsValue in rhs.withValue { rhsValue in lhsValue == rhsValue } }
lhs.value == rhs.value
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line (and the hashable line) do emit warnings in strict concurrency, so we should have a timetable for removal or consider making them unavailable, though that is a breaking change.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there new Sendability issues if you continue using withValue here?

Worth mentioning that OSAllocatedUnfairLock has a similar API where withLock takes a Sendable closure but there's also a withLockUnchecked that doesn't enforce Sendability. Would be cleaner ergonomically than requiring the use of UncheckedSendable, and you could use the unchecked variant here and in the Hashable impl (assuming you want to keep the conformances.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kabiroberai Yup, now that withValue is sendable we can't traffic the lhsValue into the closure unless it's sendable.

withLockUnchecked is an interesting idea, though I still think equatability and hashability should probably be on the chopping block since they are simple operations that should not block/lock.

@stephencelis stephencelis merged commit bb5059b into main Nov 8, 2023
7 checks passed
@stephencelis stephencelis deleted the fix-lock-isolated-bug branch November 8, 2023 20:19
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.

None yet

3 participants