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 ()
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)

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

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 51421 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

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.