Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Phantom application instance - how come?

Hello, all,

sometimes, I can see two instances of our application running at the same time. One of them is the normal responsive UiApplication - if we switch to it (via Switch Application screen) we can work normally. Another one cannot be switched to and, merely by existence, prevents the user from switching to our application by clicking its icon on the home screen. It cannot be killed and we need to pull the battery in order to rectify the situation.

 

The above is the description of the problem, now for some technical details:

 

Our application can be run in more than one way. In addition to having two entry points (one is a RUN_AT_STARTUP Application listening for push notifications, another is the full UiApplication), it can be raised (via global event) or run (if there is no UI application instance running) from http filtering (say, the user clicks a link in an e-mail) as well as on the push message arriving (notified via global event or run, this time with a special parameter indicating that new push information has arrived).

 

In any piece of code that needs to notify or run our application, we first look for it using ApplicationManager.getVisibleApplications(), comparing the module name to the one we need, then get the process id if found, and finally either send it a global event (if the process id is found) or create a descriptor templated after one found by the CodeModuleManager.getApplicationDescriptors using the module name. Than new descriptor usually has different arguments but is otherwise the same. We then use ApplicationManager.runApplication() to start it.

 

When our application starts, it first check whether another UI instance of it is already running and, if found, raises that instance and exits.

 

Here are my two questions:

1) What can cause such behaviour?

2) If I want to implement some "sanity check" to treat the symptoms, what should I look for? What conditions to check?

 

Thanks in advance!

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Phantom application instance - how come?

I'd like to know what happens when you try to switch to this phantom instance?  Do you get a white screen?

 

Can you replicate this on the Simulator?

 

You can have many instances of your Application running, provided they all have different arguments.  But if you try to launch one with duplicate arguments, it will not get started. 

 

Clicking the icon on the Home screen will start the Application with the identified arguments.  If it is already started, then this process will only bring it to the foreground. 

 

Since the Run-at-startup Application will not show up on the Task Switcher (not a UiApplication), then I think we can safely rule that out as a possible cause.

 

The fact that the icon won't start a new instance, indicates that the spurious/phantom one has been started with the same arguments.  The fact that there is one that can be switched to, indicates an instance that was started by http filtering, and has taken over the full UI processing. 

 

I'm concerned about this statement:

"usually has different arguments"

For this problem to occur, the phantom instance has to have the same arguments as the one started by the icon.

 

OK enough theory, some suggestions.

 

I would think about designing this so that there is only ever one UiApplication. 

 

I presume that you use the arguments to indicate that there is some data that is available to be processed immediately, as opposed to the standard icon related start-up which does not have any data to process.  I would try to pass this information some other way, perhaps by stalling the global event until the real UI Application is visible, or creating a RuntimeStore Queue that the Ui Application must process on start-up.  Another place to put this RuntimeStore Queue check in would be in activate(), so that the queue is checked whenever the application is 'foregrounded'. 

 

Hope this helps.  Might add more once I know the answers to the questions. 

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Phantom application instance - how come?

 

Thanks for the ideas - I'll try that.
My answers are after your questions in blue.

peter_strange wrote:

I'd like to know what happens when you try to switch to this phantom instance?  Do you get a white screen?

Nothings changes - it seems like the phantom is not painting as even the "Switch application" screen is not removed. Looks as though the event thread is blocked, but it is never killed by the framework.

 

Can you replicate this on the Simulator?

I've seen it happen, but the overall problem is that I cannot replicate it at will. Seems to be synchronization related, because the appearance of this "phantom" is quite random.

 

You can have many instances of your Application running, provided they all have different arguments.  But if you try to launch one with duplicate arguments, it will not get started. 

Aha! Didn't know that. I'll look at our code with the new knowledge in mind.

 

Since the Run-at-startup Application will not show up on the Task Switcher (not a UiApplication), then I think we can safely rule that out as a possible cause.

I agree here - previously, I ran it as UiApplication without any screens and did see the issue. I switched to Application and it disappeared.  Only after I created the push listener and began to notify / run the application with the corresponding parameters the issue began manifesting itself.

 

The fact that the icon won't start a new instance, indicates that the spurious/phantom one has been started with the same arguments.  The fact that there is one that can be switched to, indicates an instance that was started by http filtering, and has taken over the full UI processing. 

This got me thinking. As I briefly explained in my original message, the application always checks for another instance and foregrounds that and exits if found. It does check for those "push notification"-only parameters and ignores such instances. Thus the icon click might indeed start the application which then notices another one, raises it and exits. I'll investigate more. I hope to get that situation in simulator and debug the start of the new instance via Home Screen.

 

I would think about designing this so that there is only ever one UiApplication. 

I would love to do it that way - I'd only say "at most one" is more appropriate here. Our application uses enough resources, so we decided to provide "Shutdown application" option as a courtesy to our customers. Now, we have a release coming, so I'll have to live with the way it is right now until the next one. I expect to have a chance to do it properly during our next cycle, though. We've got enough legacy code so an occasional re-factoring will only help. (And, fortunately, is acceptable with our managers!)

 

I presume that you use the arguments to indicate that there is some data that is available to be processed immediately, as opposed to the standard icon related start-up which does not have any data to process.  I would try to pass this information some other way, perhaps by stalling the global event until the real UI Application is visible, or creating a RuntimeStore Queue that the Ui Application must process on start-up.  Another place to put this RuntimeStore Queue check in would be in activate(), so that the queue is checked whenever the application is 'foregrounded'. 

I must admit that I haven't used RuntimeStore yet, but it might be a good opportunity to start doing that. Again, not in this release, 


 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Phantom application instance - how come?

"I'd like to know what happens when you try to switch to this phantom instance?  Do you get a white screen?

Nothings changes - it seems like the phantom is not painting as even the "Switch application" screen is not removed. Looks as though the event thread is blocked, but it is never killed by the framework."

 

Interesting. Don't understand that.  like you I suspect something is stopping the Event Thread.  No idea what.

 

When you application checks to see if another has already been started, is this processing done in 'main'?  I can't see that you would get this icon in the Task Switcher unless the UiApplication has actually 'entered the Dispatcher'.  So I think you are looking for an error of some description that prevents this code actually running to the System.exit() that I think you expect to go to, if it not the only Ui interaction.  Perhaps something in a static initialiser?

 

Good luck.

Highlighted
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Phantom application instance - how come?

Yes, the check is done in the application constructor, and, after raising the existing application (if any), it throws an exception which prevents this new one from entering the event dispatcher.

 

It is interesting that you mention the application never running to System.exit() - we have just found a rare occurrence when too many "synchronized" blocks could cause the application to hang forever in "shutdown" state - the "shutting down..." screen is up, most worker threads are stopped, but the logging system never returns from logFile.flush(). Fixed that.

 

In fact, I've made some other changes after your first reply and now it seems that the problem has disappeared, or at least the probability went way down. I discovered that sometimes we could launch an application from push listener or http filter having copied the descriptor with FLAG_SYSTEM and FLAG_RUN_ON_STARTUP instead of the regular one with no flags. I made sure this never happens again - don't know whether it's related to the problem.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!