We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 2379

Summary: App freezes on android when killed from Task Manager
Product: SDL Reporter: Peter Jankuliak <p.jankuliak>
Component: mainAssignee: Gabriel Jacobo <gabomdq>
Status: RESOLVED INVALID QA Contact: Sam Lantinga <slouken>
Severity: major    
Priority: P2 CC: gabomdq, martin, p.jankuliak
Version: 2.0.1   
Hardware: All   
OS: Android (All)   

Description Peter Jankuliak 2014-02-02 19:23:45 UTC
To reproduce:

* Open log output and start a SDL application
* Pres the Home button
* When in home screen, press and hold the home button till task manager shows up
* Kill your application using swipe gesture
* Observe in the log output that after few seconds the system terminates
  your app because it is not responding.

The issue is mostly annoying when app needs to run other services or do service cleanup after the main activity exits.

The freeze happens inside the SDLActivity.onDestroy function on line:

SDLActivity.mSDLThread.join()

and the reason is this:

When app goes to background, the call to SDL_PollEvent causes the main thread
to block for reasons explained in README-android.txt. This block is only
released when app goes back to foreground, which is when
SDLActivity.handleResume is called (not when a new message arrives!).

However, when SDL_PollEvent is blocked, it won't receive any messages pushed to
it using the SDL_PushEvent, so the line:

SDLActivity.nativeQuit() // which pushes SDL_QUIT message to the message queue

has no effect.

I'm guessing the proper solution would be to unblock SDL_PollEvent when a
message arrives in the queue. But as a fast workaround I propose to put this
code:

if (SDLActivity.mIsPaused) {
  SDLActivity.mIsPaused = false;
  SDLActivity.nativeResume();
}

inside the SDLActivity.onDestroy function right before the call to

SDLActivity.nativeQuit();
Comment 1 Gabriel Jacobo 2014-02-20 18:53:41 UTC
SDL already takes care of the case you mention inside Java_org_libsdl_app_SDLActivity_nativeQuit.

I was just testing this with testgles2.c on my Nexus 4 and it seems Android sometimes gives you a few seconds to finish up (in which case SDL completes the exit process cleanly in the test case you mentioned), and sometimes it just kills the app directly (you can see the kill message in the log), and you might not even get the "onDestroy()" message on the log. In either case, I'm pretty certain that there's no deadlock occurring.

So, I'm going to mark this as invalid given that it seems we are doing as best as we can to handle the quit from task manager situation. 

If you'd like to suggest an alternative fix or improvement, feel free to reopen.

Thanks for the feedback!