Couchbase sync websocket exception

Our iOS application crashes when device is locked and unlocked.

[0:] INFO) CHANGE TRACKER (WebSocketChangeTracker): [40] 2017-6-23 03:52:18.199+05:30 WebSocketChangeTracker[smalldemo_bucket4] is closed
Thread finished: #23
2017-06-23 15:52:18.204 TESSALinkiOS[4485:3794887] 6/23/2017 3:52:18 PM|Fatal|c__AnonStorey4.<>m__2|System.ObjectDisposedException: Cannot access a disposed object.
Object name: ‘SslStream’.
at System.Net.Security.SslStream.CheckDisposed () [0x0000b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/System/System.Net.Security/SslStream.cs:338
at System.Net.Security.SslStream.get_Impl () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/System/System.Net.Security/SslStream.cs:83
at System.Net.Security.SslStream.EndRead (System.IAsyncResult asyncResult) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/System/System.Net.Security/SslStream.cs:376
at WebSocketSharp.Ext+c__AnonStorey3.<>m__0 (System.IAsyncResult ar) [0x
00000] in /Users/jenkins/jenkins/workspace/couchbase-lite-net-build@2/1.3.1/iOS/couchbase-lite-net/vendor/websocket-sharp/websocket-sharp/Ext.cs:648

Can’t we keep sync running in background when device is locked?

Following is our Pull replication function -

public void StartPull()
{
StopPull();

        pullReplicationHandler = database.CreatePullReplication(syncGatewayUri);
        pullReplicationHandler.Continuous = true;
        pullReplicationHandler.SetCookie(RepositoryService.SessionCookie.Name, RepositoryService.SessionCookie.Value, RepositoryService.SessionCookie.Path, RepositoryService.SessionCookie.Expires, RepositoryService.SessionCookie.Secure, RepositoryService.SessionCookie.HttpOnly);
        pullReplicationHandler.Headers.Add("Authorization", "Bearer " + UserData.IdToken);
        //As channel management is done by Sync Function, no need to specify the channels while syncing. [Updates from Chris].
        //pull.Channels = null != RepositoryService.UserContext.Channels ? RepositoryService.UserContext.Channels : new string[] { };
        pullReplicationHandler.Changed += Changed;
        pullReplicationHandler.Start();
    }

and push replication function -

    public void StartPush()
    {
        StopPush();

        pushReplicationHandler = database.CreatePushReplication(syncGatewayUri);

        pushReplicationHandler.Continuous = true;

        pushReplicationHandler.Headers.Add("Authorization", "Bearer " + UserData.IdToken);
        pushReplicationHandler.SetCookie(RepositoryService.SessionCookie.Name, RepositoryService.SessionCookie.Value, RepositoryService.SessionCookie.Path, RepositoryService.SessionCookie.Expires, RepositoryService.SessionCookie.Secure, RepositoryService.SessionCookie.HttpOnly);
        pushReplicationHandler.Start();
    }

The stop replication functions are -

    private void StopPush()
    {
        if (null != pushReplicationHandler)
        {
            pushReplicationHandler.Stop();
            pushReplicationHandler.ClearAuthenticationStores();
            pushReplicationHandler = null;
        }
    }

    private void StopPull()
    {
        if (null != pullReplicationHandler)
        {
            pullReplicationHandler.Stop();
            pullReplicationHandler.ClearAuthenticationStores();
            pullReplicationHandler = null;
        }
    }

Environment details -

Couchbase Lite - 1.3.1
Xamarin - 4.2.2.6
Xamarin Android - 7.0.2.42
Xamarin Forms - 10.3.1.7

Sync should work even when device is locked unless you have the fileProtection (on CBLManager) set to something like NSDataWritingFileProtectionComplete that would prevent access to database unless unlocked. (Even then I’m not sure it would crash rather than gracefully disallowing it). @borrrden may have some insights .

No, we don’t have any such settings set.

The Xamarin iOS version does not put as much effort into running in the background as regular iOS does and as such you cannot run in the background without starting a background process. Furthermore it will not automatically stop and so you must manually stop it when you app goes into the background if you do not start a background process.

The StartPull and StartPush are called on background process. I also have tried stopping the sync when the application goes into background, it says sync stopped, but still continues to receive the document changed events for sync. And after some time the same above websocket exception is thrown.

In one of the xamarin forum I read that the couchbase is having issues to close the websocket in iOS when OS restricts the access. I’ll share the same link if found.