You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Android: shouldn't call std::process:exit in EventLoop::run #2709
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.
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.
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.
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.
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().
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.
Reacted by Mads Marquart, Robin Thunström and Markus Røyset
Activity
kchibisov commentedon Mar 1, 2023
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.rib commentedon Mar 1, 2023
Yep, I realize it's possible to use
run_returnto avoid this if you know howrunis implemented onAndroidbut if you don't know about the platform-specific details it's reasonable for an application to userun, but it's not really reasonable that the backend should avoid returning viastd::process:exit.Exiting that way will short normal circuit lifecycle handling - not allowing the Activity to stop and call
onDestroyand it also assumes that there is no other Activity running in another thread in the same process.I also don't really like that
runis-> !but I think handling this on Android can be done a bit more gracefully than by callingstd::process:exit. Instead the backend can call theActivity::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 anAndroidApp::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 callstd::process:exitbut 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.kchibisov commentedon Mar 1, 2023
That will break type system, what I think we should do, is to simply disallow calling
runagain oniosby returningError, same as event loop creation. Yes, it's a bit more annoying, but the huge advantage would be for things drop naturally, sinceexitbreaks all destructors/unwinding and such.In general, I think it'll be wise to return some meaningful results then it's right now.
rib commentedon Mar 1, 2023
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 anErrorsounds 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 ofResultand 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 aResultonce 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 aWinitevent loop makes it impossible to ever return fromandroid_main().madsmtm commentedon Mar 2, 2023
Another reason we do
-> !is that on web, we have to leak the closure, as well as every static variable created beforerun.Perhaps it would really make sense to make a
#[winit::main]attribute similar to bevy's? And then havemainreturn the closure instead of callingrun?rib commentedon Mar 2, 2023
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 anAndroidAppfor context, and there could be more than oneandroid_mainrun in a single process.android_mainalso doesn't return aResult. This is awkward to abstract with a platform independentmainmacro.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.
Re-work event loop run APIs
Re-work event loop run APIs
Re-work event loop run APIs
Re-work event loop run APIs
Re-work event loop run APIs
Re-work event loop run APIs
22 remaining items
Re-work event loop run() API so it can return a Result
Re-work event loop run() API so it can return a Result
Re-work event loop run() API so it can return a Result
Re-work event loop run() API so it can return a Result
Re-work event loop run() API so it can return a Result
Re-work event loop run() API so it can return a Result
Re-work event loop run() API so it can return a Result
Re-work event loop run() API so it can return a Result
Re-work event loop run() API so it can return a Result
Re-work event loop run() API so it can return a Result
Re-work event loop run() API so it can return a Result
Re-work event loop run() API so it can return a Result
Re-work event loop run() API so it can return a Result
Re-work event loop run() API so it can return a Result