Skip to content

Android: shouldn't call std::process:exit in EventLoop::run #2709

Closed

Description

@rib

Winit shouldn't be assuming that it's safe to simply terminate the current process via std::process:exit after the event loop returns.

On Android it's possible to run multiple activities in a single process and it's also possible that an application is based on some Java/Kotlin subclass of an Activity that may be expecting various lifecycle events that could be short circuited when Winit simply calls std::process:exit.

Instead there should probably be some logic that leads to calling ANativeActivity_finish (i.e. something to indirectly call Activity::finish() in the JVM) and then entering an infinite AndroidApp::poll_events() loop instead.

Activity

  1. kchibisov commented on Mar 1, 2023

    @kchibisov
    Member

    But that's the reason you can use run_return? The original issue is that some platforms don't allow creating event loop multiple times. Well, that's just ios at this point...

    I really don't like that we do -> ! in winit, but it's more of a soundness thing from what I remember. Though, I wonder if we can simply fail in functions, which could be called only once during lifetime.

  2. rib commented on Mar 1, 2023

    @rib
    ContributorAuthor

    Yep, I realize it's possible to use run_return to avoid this if you know how run is implemented on Android but if you don't know about the platform-specific details it's reasonable for an application to use run, but it's not really reasonable that the backend should avoid returning via std::process:exit.

    Exiting that way will short normal circuit lifecycle handling - not allowing the Activity to stop and call onDestroy and it also assumes that there is no other Activity running in another thread in the same process.

    I also don't really like that run is -> ! but I think handling this on Android can be done a bit more gracefully than by calling std::process:exit. Instead the backend can call the Activity::finish() method in the JVM which should lead to that activity being destroyed - which may even lead to the JVM deciding to kill the process but at least that will be done with consideration of other activities.

    After calling Activity::finish() the thread can enter an AndroidApp::poll_events() loop and wait for the activity to be destroyed.

    If the JVM doesn't kill the process when destroying the activity I'm not sure there's much that can be done at that point except to stay in a AndroidApp::poll_events() busy loop because it's still not reasonable to call std::process:exit but there also isn't a way to kill the current thread in Rust afik.

    I wonder if it would be better if run() could actually return on all platforms, and just make it a platform specific detail that it won't return on iOS.

  3. kchibisov commented on Mar 1, 2023

    @kchibisov
    Member

    I wonder if it would be better if run() could actually return on all platforms, and just make it a platform specific detail that it won't return on iOS.

    That will break type system, what I think we should do, is to simply disallow calling run again on ios by returning Error, same as event loop creation. Yes, it's a bit more annoying, but the huge advantage would be for things drop naturally, since exit breaks all destructors/unwinding and such.

    In general, I think it'll be wise to return some meaningful results then it's right now.

  4. rib commented on Mar 1, 2023

    @rib
    ContributorAuthor

    Not quite sure what you're referring to with breaking the type system but generally agree with being able return a meaningful Result - and disallowing repeat calls on iOS either via an Error sounds perfectly reasonable to me if it can't be supported.

    (In case it was misleading, I wasn't meaning to suggest that on iOS it would still return -> !, I was assuming it would consistently return some kind of Result and just meant that if the implementation for iOS uses some platform-specific API that doesn't return then there would be some iOS specific documentation that clarifies that in practice it will not actually return a Result once the event loop starts)

    On Android there is meaningful cleanup we want to do when an application's android_main() returns and it's not ideal that the recommended way of running a Winit event loop makes it impossible to ever return from android_main().

  5. madsmtm commented on Mar 2, 2023

    @madsmtm
    Member

    Another reason we do -> ! is that on web, we have to leak the closure, as well as every static variable created before run.

    Perhaps it would really make sense to make a #[winit::main] attribute similar to bevy's? And then have main return the closure instead of calling run?

  6. added
    DS - android
    S - api
    C - needs investigation
    on Mar 2, 2023
  7. rib commented on Mar 2, 2023

    @rib
    ContributorAuthor

    Even though a function can return a Result doesn't really mean that it has to do that in any finite amount of time. Not returning is valid behavior imho on any platform where it's not possible to return.

    Being able to return a value just gives back a bit more control on platforms where a meaningful status can be returned, without really taking anything away for platforms that can't return. It's the more portable / platform-agnostic approach imho.

    I tend to think #[main] like macros should just be optional sugar.

    In the case of Android the android_main() gets passed an AndroidApp for context, and there could be more than one android_main run in a single process. android_main also doesn't return a Result. This is awkward to abstract with a platform independent main macro.

    Once you introduce a main macro you also probably have to deal with logging initialization which can vary between platforms and it will start to become opinionated and take away some control from developers. (Which can be ok for convenience but other times it's not really what you want)

    I think the situation is a bit different with Bevy because it's a higher level framework that can be more opinionated, e.g. with choosing how logging should be handled. Bevy also doesn't really support Android yet (it just has some bare minimum support atm) so it may not be a good reference for being portable yet.

  8. added 6 commits that reference this issue on Apr 6, 2023
    c61e901
    16cbf13
    30c9eff
    00605ee
    84ffb45
    595909e
  9. 22 remaining items

  10. added 3 commits that reference this issue on Jun 25, 2023
    31a0e68
    10f2e07
    d8a5c21
  11. added a commit that references this issue on Jul 4, 2023
    951457c
  12. added 7 commits that reference this issue on Jul 24, 2023
    acfa0a4
    6c8dd9f
    f8ac5c4
    92e4c6a
    433bb2f
    1908dbd
    8a1a815
  13. added a commit that references this issue on Jul 27, 2023
    0d366ff
  14. added 2 commits that reference this issue on Aug 14, 2023
    11969a3
    e28974b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C - needs investigationDS - androidS - api

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions