Stop replication, then close/delete fails

TLDR: If a user requests to delete all their data during a replication… I seem to be unable to stop the replication.

I set up my replication thus:

var target = new URLEndpoint(url);
var auth = new BasicAuthenticator(id, server_password);
var sync_config = new ReplicatorConfiguration(database, target)
{
   ReplicatorType = ReplicatorType.PushAndPull,
   Continuous = true,
   Authenticator = auth
};
replicator = new Replicator(sync_config);
listenerToken = replicator.AddChangeListener(SyncStatusUpdate);
replicator.Start();

If there is a lot of data to replicate, the replication runs happily, and I get the notifications.
However, during that replication, if the user selects “delete all my local data”, I want to stop replication and then delete the database.

I do it like this:

log.Debug("Stopping sync agent");
// Check if syncing hasn't even started yet.
if (replicator == null)
    return;
try
{
    replicator.Stop();
    replicator.RemoveChangeListener(listenerToken);
}
catch (Exception e)
{
    log.Error(e, "Failed to stop sync agent");
    // we need to keep going...
}
replicator = null;

I’m sure the exception is not being thrown - that is the stop seems to succeed.
I then wait a couple of seconds (elsewhere it was mentioned that things might take a couple of seconds to stop) before I start testing for the status == ReplicatorActivityLevel.Stopped. When this is true, I keep going. NOTE that I have extended this wait out to 20 seconds, but that doesn’t help.

Note that at this point, I am still receiving update events to my SyncStatusUpdate function. I shouldn’t be - but it is an indication that sync is not stopping.

Lastly I attempt to delete the database. I have tried Close then Delete, and Delete then Close. Both raise “please stop all replicators” errors. I have a “desperation” measure where I simply trash the files:

public void CloseAndDeleteDB()
{
    try
    {
        database.Delete();
        database.Close();
        database = null;
    }
    catch (Exception e)
    {
        log.Error(e, "Failed to delete database!");
        //  Lets try another route - by deleting it
        try
        {
            database.Close();
        }
        catch { }
        database = null;
        var dir = Path.Combine(path, config.DBName + ".cblite2");
        Directory.Delete(dir, true);
    }
    database = null;
}

A little later I create a fresh new database with no data, and my application then hangs.

So the question is… what else do I need to do to ensure replication stops, even when it is in the middle of a large replication, potentially with a lot of images (few MB in size).

Thx.
Paul

Even if the replication did not stop, you shouldn’t be because you called RemoveChangeListener. That function is not very complicated and it is unlikely that there is a flaw in it, so my mind automatically begins to question “Are you using that callback on multiple replicators and could that callback be from a different one?” or “Are you using the wrong listenerToken?”

Some logs might shed some light on what is going on. There is nothing you can do to force the replicator to stop (A lack that we heavily debate internally), but there are no known bugs with a replicator not stopping at the moment. It doesn’t really matter how many things are in progress because they are all on the same connection, and stop disconnects that connection. There are a few things I could theorize about (Are you blocking inside your callback? Etc) but that is a bit premature.

Damn! (slaps forehead).
That was it. Somehow the sync startup function was called twice.
Thanks.