Migueldeicaza,
@Migueldeicaza@mastodon.social avatar

Good post by @heckj on what he learned and the tradeoffs he did when designing an API for strict swift concurrency support.

The best part are the regrets he has, good guidance for those of us starting on our strict journey:

https://rhonabwy.com/2024/04/29/designing-a-swift-library-with-data-race-safety/

krzyzanowskim,
@krzyzanowskim@mastodon.social avatar

deleted_by_author

  • Loading...
  • Migueldeicaza,
    @Migueldeicaza@mastodon.social avatar

    @krzyzanowskim @heckj threading was always fraught with mistakes, we were just living blissfully unaware and blaming random instability into “I guess we will never know”

    heckj,
    @heckj@mastodon.social avatar

    @Migueldeicaza @krzyzanowskim Speaking truth. The benefits of the compiler guarantees are amazing - with them in place, I spotted issues that I thought were otherwise completely safe, patterns of doing things that "just worked" 99.9% of the time because I wasn't unlucky. This is a huge update to pressing forward on writing solid, reliable code.

    krzyzanowskim,
    @krzyzanowskim@mastodon.social avatar

    deleted_by_author

  • Loading...
  • Migueldeicaza,
    @Migueldeicaza@mastodon.social avatar

    @krzyzanowskim @heckj why do you hate Americans? Is it because of our freedoms?

    spitfire,
    @spitfire@mastodon.social avatar
    Migueldeicaza,
    @Migueldeicaza@mastodon.social avatar

    @krzyzanowskim @heckj on a serious note, I don’t understand the issue with this one. Would love to know why the let assignment is not enough .

    holly,
    @holly@hachyderm.io avatar

    @Migueldeicaza @krzyzanowskim @heckj it’s because the type of the global variable is not Sendable. Initialization of globals is thread-safe, but if the value has, say, reference types with mutable state, then you can still get concurrent mutations.

    Migueldeicaza,
    @Migueldeicaza@mastodon.social avatar

    @holly @krzyzanowskim @heckj thank you Holly, I didn’t know what the rules were for that and made myself a test case earlier for the global. Glad to know that step is expected!

    bigzaphod,
    @bigzaphod@mastodon.social avatar

    @Migueldeicaza @krzyzanowskim @heckj assuming this is a global variable, Swift let values are lazy initialized when they're global. So any thread could potentially be the first one to read it and thus initialize it the same moment another thread tries to read it and thus initialize it.

    Migueldeicaza,
    @Migueldeicaza@mastodon.social avatar
    krzyzanowskim,
    @krzyzanowskim@mastodon.social avatar

    deleted_by_author

  • Loading...
  • shadowfacts,
    @shadowfacts@social.shadowfacts.net avatar

    @krzyzanowskim @bigzaphod @Migueldeicaza @heckj but your synchronous code that accesses/initializes the value could be called on any thread—unless you have some synchronization mechanism

    krzyzanowskim,
    @krzyzanowskim@mastodon.social avatar

    deleted_by_author

  • Loading...
  • shadowfacts,
    @shadowfacts@social.shadowfacts.net avatar

    @krzyzanowskim @bigzaphod @Migueldeicaza @heckj you don't, but presumably there's some synchronous entry point into your code that could be invoked on any thread, so transitively the let initializer could be invoked on any thread:

    Thread {
    CryptoSwift.doWhatever()
    }.start()
    Thread {
    CryptoSwift.doWhatever()
    }.start()

    Migueldeicaza,
    @Migueldeicaza@mastodon.social avatar
    krzyzanowskim,
    @krzyzanowskim@mastodon.social avatar

    deleted_by_author

  • Loading...
  • shadowfacts,
    @shadowfacts@social.shadowfacts.net avatar

    @krzyzanowskim @bigzaphod @Migueldeicaza @heckj you said your module is entirely synchronous, no? my point is that nothing binds your clients to that

    krzyzanowskim,
    @krzyzanowskim@mastodon.social avatar

    deleted_by_author

  • Loading...
  • shadowfacts,
    @shadowfacts@social.shadowfacts.net avatar

    @krzyzanowskim @bigzaphod @Migueldeicaza @heckj but there exists some codepath from which a public caller could trigger its initialization? otherwise why does the property exist

    the compiler doesn't actually do that level of flow analysis (so far as I know), but it makes the assumption that that scenario exists because it can't prove otherwise

    krzyzanowskim,
    @krzyzanowskim@mastodon.social avatar

    deleted_by_author

  • Loading...
  • shadowfacts,
    @shadowfacts@social.shadowfacts.net avatar

    @krzyzanowskim @bigzaphod @Migueldeicaza @heckj the compiler doesn't know that the codepath exists. it just can't prove that the codepath doesn't exist. if you can make that guarantee, then you can use nonisolated(unsafe)

    krzyzanowskim,
    @krzyzanowskim@mastodon.social avatar

    deleted_by_author

  • Loading...
  • Migueldeicaza,
    @Migueldeicaza@mastodon.social avatar

    @krzyzanowskim @shadowfacts @bigzaphod @heckj no, it means that clients could initialize your library accidentally twice. Which is correct

    Migueldeicaza,
    @Migueldeicaza@mastodon.social avatar

    @krzyzanowskim @shadowfacts @bigzaphod @heckj his example shows the race, the external caller influences this. You might not like it, but it is catching a real problem :-)

    krzyzanowskim,
    @krzyzanowskim@mastodon.social avatar

    deleted_by_author

  • Loading...
  • Migueldeicaza,
    @Migueldeicaza@mastodon.social avatar

    @krzyzanowskim @shadowfacts @bigzaphod @heckj I think the compiler would need to prove that none of the initializers have side effects

    shadowfacts,
    @shadowfacts@social.shadowfacts.net avatar

    @krzyzanowskim @Migueldeicaza @bigzaphod @heckj it could be accessed from a different thread than it was initialized on, so it needs to be either Sendable (letting the compiler prove that's safe) or it needs to be nonisolated(unsafe) (telling the compiler that you are guaranteeing it's safe).

    I'm not saying blindly slapping @MainActor on things is the best solution—here it looks like CS.BigUInt should be Sendable

    bigzaphod,
    @bigzaphod@mastodon.social avatar

    @krzyzanowskim @Migueldeicaza @heckj unless something is isolated to a specific actor somewhere, I think the compiler's assumption is that all actors are potentially able to use anything anywhere and so it warns about stuff. It's annoying for sure. It has seriously harmed the learnability of Swift. It drives me crazy. I'm just saying... this is why it's telling you that - not that I agree with it or that it couldn't do a better job being smart.

    krzyzanowskim,
    @krzyzanowskim@mastodon.social avatar

    deleted_by_author

  • Loading...
  • palmin,
    @palmin@mastodon.social avatar

    @krzyzanowskim Not trying to defend this direction of Swift but wouldn’t unchecked Sendable solve this specific issue?

    krzyzanowskim,
    @krzyzanowskim@mastodon.social avatar

    deleted_by_author

  • Loading...
  • palmin,
    @palmin@mastodon.social avatar

    @krzyzanowskim You just wrote that you didn’t need the sendable requirements. 😄

    bigzaphod,
    @bigzaphod@mastodon.social avatar
    krzyzanowskim,
    @krzyzanowskim@mastodon.social avatar

    deleted_by_author

  • Loading...
  • Migueldeicaza,
    @Migueldeicaza@mastodon.social avatar

    @krzyzanowskim @bigzaphod @heckj this deserves an enterprise label

    krzyzanowskim,
    @krzyzanowskim@mastodon.social avatar

    deleted_by_author

  • Loading...
  • Migueldeicaza,
    @Migueldeicaza@mastodon.social avatar

    @krzyzanowskim @bigzaphod @heckj once again, Apple saves the day and the industry

    icanzilb,
    @icanzilb@mastodon.social avatar

    @krzyzanowskim @bigzaphod @Migueldeicaza @heckj i like it but it's paid, one star ⭐️

    titociuro,
    @titociuro@mstdn.social avatar

    @krzyzanowskim @Migueldeicaza @heckj Strict concurrency has completely taken the fun out of programming.

    Migueldeicaza,
    @Migueldeicaza@mastodon.social avatar

    @titociuro @krzyzanowskim @heckj I see it the other way around, I won’t spent countless hours tracking down a nasty and irreproducible bug, which have done more times than I would have ever wanted in my life.

    titociuro,
    @titociuro@mstdn.social avatar

    @Migueldeicaza @krzyzanowskim @heckj The thing is… I just haven’t suffered that many cases like you describe. Sure! Some, but I honestly had a pretty good grasp on GCD. Crashes are rare and far between. So spending all this time adopting SC is not productive. Let alone fun. 🤷🏻‍♂️

    Migueldeicaza,
    @Migueldeicaza@mastodon.social avatar

    @titociuro @krzyzanowskim @heckj you are just a better programmer than I am :-)

    titociuro,
    @titociuro@mstdn.social avatar

    @Migueldeicaza @krzyzanowskim @heckj Yeah, right. 🙄 I love your work, and I think it’s more complex than the average iOS app. While SC will help you conquer those daemons, I think it’s overkill for most apps. I dunno… I must be old school, but I sure feel more comfortable with GCD.

    me,
    @me@cafe.marquiskurt.net avatar

    @titociuro @Migueldeicaza I'm in that transitional area where I learned both GCD and SC, but I can see both sides. There's a nice simplicity to GCD that "just works". At the same time, SC follows up on Swift's explicit philosophies, and I do feel it has its place as a language-wide concurrency solution (I think GCD is Apple-specific, last I checked). So I can't really say one is better than the other, but I can say both opinions are valid.

    Migueldeicaza, (edited )
    @Migueldeicaza@mastodon.social avatar

    @me @titociuro anecdotally, every time I go “I know perfectly well that this can never be nil, so I can safely do “x!.foo”, some months later I get a crash report because someone managed to make the x a nil in production.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • tacticalgear
  • DreamBathrooms
  • cisconetworking
  • magazineikmin
  • InstantRegret
  • Durango
  • thenastyranch
  • Youngstown
  • rosin
  • slotface
  • mdbf
  • khanakhh
  • kavyap
  • everett
  • provamag3
  • modclub
  • Leos
  • cubers
  • ngwrru68w68
  • ethstaker
  • osvaldo12
  • GTA5RPClips
  • anitta
  • megavids
  • normalnudes
  • tester
  • JUstTest
  • lostlight
  • All magazines