From 3cdddf39a0e207088512f9d40d4effe108e8d762 Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Sat, 8 Jun 2024 19:45:17 +0900 Subject: [PATCH] Add details about tokio drop and rapid shutdown --- documentation/docs/graceful-shutdown.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/documentation/docs/graceful-shutdown.md b/documentation/docs/graceful-shutdown.md index 085b32a7..295bab7b 100644 --- a/documentation/docs/graceful-shutdown.md +++ b/documentation/docs/graceful-shutdown.md @@ -1,8 +1,8 @@ # Graceful Shutdown -When the Flutter app is closed, the entire `tokio` async runtime on the Rust side will be terminated automatically. +When the Flutter app is closed, the entire `tokio` async runtime on the Rust side will be terminated automatically. Even if the app is force-closed, the `tokio` async runtime will be properly dropped. -When using Rinf, the lifetime of the `tokio` runtime follows that of the Dart runtime. This behavior is different from typical `tokio` executables where its async runtime lives throughout the `main()` function of Rust. +When using Rinf, the lifetime of the `tokio` runtime follows that of the Dart runtime. This behavior is different from typical `tokio` executables where its async runtime lives throughout the async `main()` function of Rust. In some cases, you might need to run some finalization code in Rust before the app closes. This might involve saving files or disposing of resources. To achieve this, you can use Flutter's `AppLifecycleListener` to run something or to get user confirmation before closing the Flutter app. @@ -41,3 +41,7 @@ class _MyAppState extends State { } ... ``` + +It's worth noting that `AppLifecycleListener` or `dispose` cannot always be relied upon for app closings. Below is a text snippet quoted from the official [Flutter docs](https://api.flutter.dev/flutter/widgets/State/dispose.html): + +> There is no way to predict when application shutdown will happen. For example, a user's battery could catch fire, or the user could drop the device into a swimming pool, or the operating system could unilaterally terminate the application process due to memory pressure. Applications are responsible for ensuring they behave well even in the face of rapid, unscheduled termination.