Bug 51421 - System.Environment.Exit hangs when android:enables is set on TextView
Summary: System.Environment.Exit hangs when android:enables is set on TextView
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler (show other bugs)
Version: 7.0 (C8)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Marek Habersack
Depends on:
Reported: 2017-01-11 18:42 UTC by Jeremy Kolb
Modified: 2018-02-01 00:36 UTC (History)
4 users (show)

See Also:
Tags: bb
Is this bug a regression?: ---
Last known good build:

Sample project duplicating the bug. (39.14 KB, application/zip)
2017-06-26 14:07 UTC, Tom Opgenorth

Description Jeremy Kolb 2017-01-11 18:42:57 UTC
See: https://github.com/MvvmCross/MvvmCross/issues/1538

I think this may be a Xamarin bug.  The repro test project is at https://github.com/lauxjpn/ExitTest02
Comment 1 Marek Habersack 2017-01-13 09:58:34 UTC
I might be a bad interaction between Mono and Dalvik/Art/Java, I'll investigate. But, in general, it's better to call Activity.Finish() in the launcher activity to quit the Android app - much safer.
Comment 2 Tom Opgenorth 2017-06-26 14:06:19 UTC
I can confirm this behaviour. Repeating the reproduction steps here:

1. Open the attached project in Visual Studio for Mac
2. Set a break point in the Android project in the `SomeActivity.OnBackPressed` method.
3. Run the application
4. Click the "Disable" button
5. Click on the system back button
6. The app will hang.

All this said, it would be more appropriate to call `Activity.Finish()` than to call `System.Environment.Exit()`
Comment 3 Tom Opgenorth 2017-06-26 14:07:05 UTC
Created attachment 23116 [details]
Sample project duplicating the bug.
Comment 4 Jonathan Pryor 2017-06-29 13:06:42 UTC
Why is `Environment.Exit()` being called in the first place? Normally the Android process should not be exited by user code; Android will kill the process when necessary.
Comment 5 Jeremy Kolb 2017-06-29 13:31:26 UTC
No idea why the user is doing this.  It was originally a bug filed with MvvmCross and I punted it here.
Comment 6 pmaske 2018-02-01 00:36:18 UTC
I am running into this issue. It is holding up our unit test automation. The  have a usecase for calling Environment.Exit(). After unit tests are run and test run is complete, I want to exit the app/process. I have a batch script that looks for running process via package name. As soon as the pid becomes empty, I want to fetch the unit test results file from sdcard to local disk.

So in my app I called System.Enviornment.Exit(1); This causes the app to hang. which leaves the process running forever. Jenkins job never finishes because pid is not empty.
I would really appreciate any workaround or insight on how can I achieve this. 
Basically I want my app to terminate after test run is complete and the app knows when tests runs are complete. I want to exit right after that.

Note You need to log in before you can comment on or make changes to this bug.