System.FieldAccessException in CBL .NET Xamarin.Android; ReplicationChangeEventArgs.LastError

Sometimes (maybe 50% of the time) we get the following error reported from the ReplicationChangeEventArgs LastError:

{System.FieldAccessException: Field `System.Net.HttpWebRequest:ResendContentFactory' is inaccessible from method `System.Net.Http.HttpClientHandler/<SendAsync>c__async0:MoveNext ()'

  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[TResult].Start[TStateMachine] (TStateMachine& stateMachine) [0x00031] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:471 
  at System.Net.Http.HttpClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x0001f] in <6e92ad09d0bd49b996fe288564eee203>:0 
  at System.Net.Http.DelegatingHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00000] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/System.Net.Http/System.Net.Http/DelegatingHandler.cs:76 
  at System.Net.Http.MessageProcessingHandler.<SendAsync>__BaseCallProxy0 (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x7f6632fb10 + 0x0005f> in <6e92ad09d0bd49b996fe288564eee203>:0 
  at System.Net.Http.MessageProcessingHandler+<SendAsync>c__async0.MoveNext () [0x0003e] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/System.Net.Http/System.Net.Http/MessageProcessingHandler.cs:51 }

Currently, we are only handling the error HttpStatusCode UNAUTHORISED. When the above error happens, the replication takes a long time but succeeds, however, the internet is not able to be accessed via a WebRequest and our app crashes.

We are using the latest CI build 1.4 build 12. This is fairly urgent.

This is an annoying ongoing bug with Xamarin ( Personally I have never encountered it (or it’s been a while since I have), but I kept my environment back on Xamarin Android 6 because I noticed some problems happening with Xamarin Android 7. If you aren’t using version 6 then I suggest trying that. Another suggestion from that bug report is to switch your http client implementation to “AndroidClientHandler” (should be in general settings I guess).

@borrrden this is coming from Couchbase Lite SDK side, so there is not much I can do about it from my side right? It seems like the best course of action would be to somehow restart the app, or the replication service if this happens?

@borrrden I have tried setting the Http client to AndroidClientHandler, but still getting the same issue. This is very frustrating, and renders our app useless half of the time.

I could say the same thing. The error is coming from within the Xamarin Android runtime. Are you running Xamarin Android 6 or 7? I would recommend downgrading and trying 6 because that’s the version it is built with. The explanation I’ve heard before is that it is a bug in the JIT compiler that corrupts access.

We are using Xamarin Android 6. Is there any merit in giving the CouchbaseLite-PCL nuget package a go - maybe changing the library to a PCL might help? I know this is not built by couchbase…

I’m not sure what that project’s goal is since the title is “Couchbase.Lite assemblies from net45 folder”

By the way, out of curiosity are you using an https endpoint? If so, could you see if the problems happen with http as well?

We are using Syng Gateway through Http only currrently.

I just realized that setting AndroidClientHandler through the GUI is not going to have any effect since Couchbase Lite manually sets the HttpMessagingHandler so I put in some logic to use AndroidClientHandler on platforms where it is available (>= 5.0). Unlike HttpClientHandler it doesn’t use ResendContentFactory so hopefully it can avoid the issue. The next build (build 14 or above) will have this change.

Awesome thanks. When is this build available?

Usually it would be within 15 minutes of the commit, but evidently some of our build machines are being moved to a data center and one of them is used for the .NET builds so the build cannot proceed until it is back online (and it is now late in the evening in the time zone that our build engineer is in, and I don’t have access to the Jenkins server that controls the build). I pinged him so he will get the message in about 11 hours or so…

If you cannot wait you will have to build the library yourself from the latest commit on master.

All good, thanks for your help !

Good news, the build machine is back online and build 14 is out.

Great! giving it a go now.

@borrrden it seems to be working nicely now, will report if any issues. thanks again !