Bug 19027 - Can't debug multiple Android app processes at the same time
Summary: Can't debug multiple Android app processes at the same time
Alias: None
Product: Android
Classification: Xamarin
Component: Debugger ()
Version: 4.13.x
Hardware: PC Windows
: Low enhancement
Target Milestone: ---
Assignee: dean.ellis
Depends on:
Reported: 2014-04-14 06:59 UTC by Waldemar Zubik
Modified: 2014-04-29 18:30 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Xamarin based service example (12.05 KB, application/octet-stream)
2014-04-14 06:59 UTC, Waldemar Zubik
Java based service example (45.47 KB, application/octet-stream)
2014-04-14 06:59 UTC, Waldemar Zubik

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 19027 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 Waldemar Zubik 2014-04-14 06:59:21 UTC
Created attachment 6572 [details]
Xamarin based service example

I'm having a simple service that has been moved to a separate process.
Everything works fine instead one thing.
After I call BindService method the onconnection event is executed after almost a minute.

On the other hand I've created exactly the same service in java and it starts after 1-2 seconds.
Is there some kind of limitation in this solution?
Comment 1 Waldemar Zubik 2014-04-14 06:59:55 UTC
Created attachment 6573 [details]
Java based service example
Comment 2 Jonathan Pryor 2014-04-15 13:02:00 UTC
I am not able to reproduce the described behavior.

What device are you running this on? Which Android version?

I applied the following patch, which just adds additional logging statements: https://gist.github.com/jonpryor/fc2292ae0094724e1d2b

(It also drops the minSdkVersion so that I can deploy to my API-15 test device.)

I ran the app, then filtered the `logcat` output:

$ adb logcat > log.txt
# launch the app...
$ grep ': #' log.txt

Resulting output:

> /mono-stdout( 6762): # Activity1.OnCreate: at: 4/30/2014 11:51:36 AM
> /mono-stdout( 6762): # Activity.OnStart: at: 4/30/2014 11:51:37 AM
> /mono-stdout( 6776): # TestService..ctor: Constructed At: 4/30/2014 11:51:38 AM
> /mono-stdout( 6776): # TestService.OnBind: at: 4/30/2014 11:51:38 AM
> /mono-stdout( 6776): # ServiceBinder..ctor: at: 4/30/2014 11:51:38 AM
> /mono-stdout( 6762): # ServiceConnection.OnServiceConnected: at: 4/30/2014 11:51:38 AM

Note that on this particular device (Motorola Xoom w/ API-15), there is a ~1s pause between Activity1.OnStart() and the construction of the TestService class, which is approximately what you report for the Java sample.

Note: It isn't necessary to use <service/> within AndroidManifest.xml to run the Service in a separate process. You can instead set the ServiceAttribute.Process property, as is done here:

Comment 3 Waldemar Zubik 2014-04-15 14:32:18 UTC
So the device I was running on was 
Motorola Moto G with Android 4.3 onboard.

I wasn't checking if it works below API 16 while API 16 is the minimum version we are using to develop aplications (Nokia X is API 16).
Comment 4 Waldemar Zubik 2014-04-16 04:08:45 UTC
I've checked logs on my device and yes, this occurs only when debugging an app.
If I deploy an apk then everything is working fine.
Comment 5 Jonathan Pryor 2014-04-16 10:43:30 UTC
I have re-tested on a Nexus 5 w/ ART and Android v4.4.2, and with the AndroidManifest.xml change reverted (i.e. minSdkVersion is back to 16), and I see the same behavior as on the Xoom: a 0-1s delay between Activity.OnStart() and construction of the TestService instance.
Comment 6 Jonathan Pryor 2014-04-16 10:57:53 UTC
> If I deploy an apk then everything is working fine.

Not sure what you mean by "deploy an apk"; the .apk needs to be deployed in order to execute anything.

> I've checked logs on my device and yes, this occurs only when debugging an app.

I believe that this is the problem.


1. the debugger can attach to only one process; 
2. Whether the process attempts to attach to the debugger depends on:
2.a: whether the debug.mono.extra system property has been set; and
2.b: the timeout specified within the debug.mono.extra system property hasn't expired.

At no point is debug.mono.extra system property cleared out as part of process startup (the IDE/deployment process sets it); the timeout value is relied upon to prevent debugger attach when you "walk away from the IDE".

Consequently, what I suspect is happening is:

1. You start debugging the app.
2. The IDE sets the debug.mono.extra system property to tell the process to attach to the debugger.
3. The _first_ process is launched, attaches to the debugger.
4. (3) calls Activity1.OnStart(), which creates the Service in a new process.
5. The new Service process launches, checks debug.mono.extra, _tries_ to attach to the debugger.
6. The debugger already attached in (3), causing the Service process to timeout when trying to attach.

The network timeout would explain the ~1m+ delay you see when debugging the app.
Comment 7 Jonathan Pryor 2014-04-16 11:00:24 UTC
Workaround: Don't Do That™. Specifically, if you're going to attach the debugger to the process, don't split out the Service into a separate process, keep everything in the same process when using the debugger.
Comment 8 Jonathan Pryor 2014-04-16 11:13:44 UTC
As part of Comment #7, you can follow Comment #2, remove the <service/> from Properties/AndroidManifest.xml, and instead use C# conditional compilation:

    [Service (
#if !DEBUG
    partial class TestService : Service {
Comment 9 Jeffrey Stedfast 2014-04-23 14:01:01 UTC
We can look into supporting the debugging of multiple processes at the same time in a future version of Xamarin Studio
Comment 10 Waldemar Zubik 2014-04-25 10:28:51 UTC
As we are using DI in our mutliplatform solution, switching off two process mode for debugging is not an option for us.

We belive that the best solution in this case is to have an option to attach any mono process on demand (like on Windows Phone).
At this moment it is really hard to debug anything that is running in the background - the only option is to use logcat or our internal logging system.
That cost us additional time we need to spend on investigating issues.
Comment 11 Mikayla Hutchinson [MSFT] 2014-04-29 18:30:17 UTC
Unfortunately attaching to any process on demand is very difficult as unlike Microsoft we do not have control over the OS. We would need some way to start the processes in "attachable" mode and have them broadcast their debug ports.