Bug 763 - Monodroid: Marking a service to run in new process causes exception
Summary: Monodroid: Marking a service to run in new process causes exception
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 1.0
Hardware: PC Windows
: Normal critical
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-09-12 13:58 UTC by Brett
Modified: 2012-06-28 17:16 UTC (History)
7 users (show)

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


Attachments
Visual Studio Monodroid project that illustrates this bug. (861.23 KB, application/octet-stream)
2011-09-12 13:58 UTC, Brett
Details
MultiProcessTest-Working.zip (880.65 KB, application/zip)
2011-09-13 20:42 UTC, Jonathan Pryor
Details


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 on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Brett 2011-09-12 13:58:18 UTC
Created attachment 329 [details]
Visual Studio Monodroid project that illustrates this bug.

This is a bug in monodroid, but there is no option to select it as the product.

Setting the service process attribute to something other than the default causes a runtime exception at startup. I have attached a Visual Studio project that contains a test case that reproduces this bug.

Here is the output from logcat leading up to and including the exception:

09-12 10:49:27.808 11479 11479 I ActivityThread: Pub MultiProcessTest.MultiProcessTest.__mono_init__: mono.MonoRuntimeProvider
09-12 10:49:27.816 11479 11479 D dalvikvm: Trying to load lib /data/data/MultiProcessTest.MultiProcessTest/lib/libmonodroid.so 0x40517a30
09-12 10:49:27.816 11479 11479 D dalvikvm: Added shared lib /data/data/MultiProcessTest.MultiProcessTest/lib/libmonodroid.so 0x40517a30
09-12 10:49:27.816 11479 11479 I MonoDroid-Debugger: Not starting the debugger as the timeout value has been reached; current-time: 1315849767  timeout: 1315603
198
09-12 10:49:29.847  1318  1323 I ActivityManager: Start proc MultiProcessTest.MultiProcessTest:myService for service MultiProcessTest.MultiProcessTest/multiproc
esstest.MyService: pid=11488 uid=10067 gids={3003}
09-12 10:49:29.965  1318  1349 I ActivityManager: Displayed MultiProcessTest.MultiProcessTest/multiprocesstest.Activity1: +2s246ms
09-12 10:49:29.988 11488 11488 W dalvikvm: No implementation found for native Lmono/android/Runtime;.register (Ljava/lang/String;Ljava/lang/Class;Ljava/lang/Str
ing;)V
09-12 10:49:29.988 11488 11488 W dalvikvm: Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lmultiprocesstest/MyService;
09-12 10:49:29.988 11488 11488 W dalvikvm: Class init failed in newInstance call (Lmultiprocesstest/MyService;)
09-12 10:49:29.988 11488 11488 D AndroidRuntime: Shutting down VM
09-12 10:49:29.988 11488 11488 W dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40018560)
09-12 10:49:29.996 11488 11488 E AndroidRuntime: FATAL EXCEPTION: main
09-12 10:49:29.996 11488 11488 E AndroidRuntime: java.lang.ExceptionInInitializerError
09-12 10:49:29.996 11488 11488 E AndroidRuntime:        at java.lang.Class.newInstanceImpl(Native Method)
09-12 10:49:29.996 11488 11488 E AndroidRuntime:        at java.lang.Class.newInstance(Class.java:1409)
09-12 10:49:29.996 11488 11488 E AndroidRuntime:        at android.app.ActivityThread.handleCreateService(ActivityThread.java:2002)
09-12 10:49:29.996 11488 11488 E AndroidRuntime:        at android.app.ActivityThread.access$2500(ActivityThread.java:124)
09-12 10:49:29.996 11488 11488 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1022)
09-12 10:49:29.996 11488 11488 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:99)
09-12 10:49:29.996 11488 11488 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:123)
09-12 10:49:29.996 11488 11488 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:3806)
09-12 10:49:29.996 11488 11488 E AndroidRuntime:        at java.lang.reflect.Method.invokeNative(Native Method)
09-12 10:49:29.996 11488 11488 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Method.java:507)
09-12 10:49:29.996 11488 11488 E AndroidRuntime:        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-12 10:49:29.996 11488 11488 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-12 10:49:29.996 11488 11488 E AndroidRuntime:        at dalvik.system.NativeStart.main(Native Method)
09-12 10:49:29.996 11488 11488 E AndroidRuntime: Caused by: java.lang.UnsatisfiedLinkError: register
09-12 10:49:29.996 11488 11488 E AndroidRuntime:        at mono.android.Runtime.register(Native Method)
09-12 10:49:29.996 11488 11488 E AndroidRuntime:        at multiprocesstest.MyService.<clinit>(MyService.java:14)
Comment 1 Sebastien Pouliot 2011-09-12 14:18:56 UTC
-> mono for android
Comment 2 Brett 2011-09-12 17:34:26 UTC
Created attachment 332 [details]
A ported version of the attached Visual Studio project for Eclipse (pure Android).

This project creates 2 services that each run in their own processes. It proves that the android:process attribute does work correctly outside of monodroid.
Comment 3 Jonathan Pryor 2011-09-13 15:29:56 UTC
Here's the problem: We use a ContentProvider in order to initialize the Mono runtime, and when the Service creates a new process, Android doesn't do the "normal" process creation steps (which usually involves creating/registering the ContentProviders). As a result, our ContentProvider isn't executed for the service process, which means Mono is never loaded, and things go very bad from there.
Comment 4 Jonathan Pryor 2011-09-13 16:14:20 UTC
Comment #3 is wrong, but almost right. Specifically, Android _will_ do the "normal" process creation steps, and it will read the <provider/> elements.

HOWEVER (which is what mislead me), Android requires that the //provider/@android:name be UNIQUE. Thus, the hack.

After a build:

 1. Copy $(ProjectDir)\obj\Debug\android\src\mono\MonoRuntimeProvider.java as
    e.g. $(ProjectDir)\MonoRuntimeProvider2.java.

 2. Edit MonoRuntimeProvider2.java and
    s/MonoRuntimeProvider/MonoRuntimeProvider2/g .
    This should fix the constructor, at minimum.

 3. Edit your Project.csproj, and add:

  <ItemGroup>
    <AndroidJavaSource Include="MonoRuntimeProvider2.java" />
  </ItemGroup>

 4. Edit your Properties\AndroidManifest.xml. (This may require creating it within
    the Project Properties screen, as was necessary with the original attached project.)

 5. Add the following line within the <application/> element:

    <provider
        android:name="mono.MonoRuntimeProvider2"
        android:initOrder="2147483647"
        android:process=":myService"
        android:authorities="MultiProcessTest.MultiProcessTest.__mono_init__" />

Specifically, the //provider/@android:name attribute MUST match the filename+classname from steps (1) and (2), and the //provider/@android:process attribute MUST match the value of your ServiceAttribute.Process value.

 6. Rebuild, reinstall.

It should now work.
Comment 6 Jonathan Pryor 2011-09-13 20:42:10 UTC
Created attachment 357 [details]
MultiProcessTest-Working.zip

Attached is a copy of the original project modified as per Comment #4.
Comment 7 Jonathan Pryor 2012-06-28 13:08:44 UTC
Fixed in c10af36f.
Comment 8 Miha Markic 2012-06-28 13:35:04 UTC
Eh, what's exactly c10af36f and what exactly is fixed?
Comment 9 Jonathan Pryor 2012-06-28 13:44:08 UTC
c10af36f is a commit within our source control system, and what's fixed is...bug #763.
Comment 10 Miha Markic 2012-06-28 14:22:53 UTC
Perhaps it might be better to add the M4A version where the fix appears.
As per what is fixed I meant, will these kind of services run without any configuration (from developer's point of view).

Thanks and sorry for nitpicking, I just want to understand...
Comment 11 Jonathan Pryor 2012-06-28 15:40:26 UTC
> add the M4A version where the fix appears 

I'm not sure which MfA version the fix will be in. It will either be in 4.2.4 or 4.2.5.

> will these kind of services run without any configuration (from developer's point of view).

Yes, the release that contains this fix will allow you to set the e.g. ServiceAttribute.Process property and have it Just Work™, without you needing to change AndroidManifest.xml and add custom Java code (as described in Comment #5).
Comment 12 Miha Markic 2012-06-28 17:16:55 UTC
Excellent, thanks.