Device Sync stopped working

Using Atlas Device Sync with React Native SDK. And in general it works fine, but we found one user with which READ or WRITE operations are blocked and user is stuck. We should expect at least a client reset in this case to unblock user. Getting constant error on server logs:

could not decode next message: failed to get reader: WebSocket closed: failed to acquire lock: context deadline exceeded

Error:

could not decode next message: failed to get reader: WebSocket closed: failed to acquire lock: context deadline exceeded

Logs:

[
“Connection was active for: 1m4s”
]
Function Call Location:

DE-FF

Remote IP Address:

151.47.237.19

SDK:

JS v12.6.0

Framework:

react-native v0.71.8

Platform:

iOS v16.3

Hi, can you provide a link to your application in the App Services UI? Also, can you clarify if this is happening consistently? It appears that the server is unable to make basic websocket operations with the client which is generally indicative of a network misconfiguration (or really poor network). In these cases, Device Sync relies on the client to just reconnect and when the network is functioning properly it will sync normally.

Best,
Tyler

Hello! Thanks for your answer, sending you the link to AppService logs for this user: App Services

We can confirm that this user had issues with sync, but network connection was working fine at the time

Thanks for getting back so quickly. I looked at the user, and it does run into an error, but I think it is harmless and you can see it disconnect, reconnect, and resume syncing normally. See here

Can you confirm what effects this user saw? The error WebSocket closed: failed to acquire lock: context deadline exceeded is generally a red herring and something we should avoid logging to the public logs (I have filed a ticket to do this), but generally just means that things will resume normally afterwards.

As an aside, I also see that within about a minute or 2 the subscription is “changed” 193 times but it never actually changes. This should not cause any issues as we do have some fast paths to avoid doing anything if the subscription does not change, but it does point to perhaps some bad practices as it will clog up the pipes a bit.

Best,
Tyler

I see, thanks for the feedback, yeah we noticed one collection had issue to be synced. But we will follow up with the user to understand better the scenario. And regarding this subscription change, can you give us more information on what could be the cause of these changes, so we’ll try to investigate on the frontend side what could be the solution.

Hi, I can’t be certain what your application code is doing, but the effect it is having is that the subscription is being “updated” constantly, but it is not actually changing. I would take a look at anywhere that you update the sync subscriptions (perhaps add some logging), as it would seem something it causing it to keep running this function without making any changes.

To clarify, I don’t think this is related to the issue at hand, and it shouldn’t generally cause issues, but it is an anti-pattern and could result in some unnecessary billing (more messages between the client and server).

Best,
Tyler

We have across our components multiple useEffects to subscribe to changes to each collection, for example:

 
useEffect(() => {
    realm.subscriptions.update(mutableSubs => {
      mutableSubs.add(realm.objects(MyCollection));
    });
  }, [realm]);

Could this be an issue? Because I see in docs the new RealmJS version 12 the recommendation is to subscribe to the query directly:

realm@12.0.0 adds experimental APIs that subscribe to and unsubscribe from a query’s results. These APIs abstract away the details of manually adding and removing subscriptions.

Let us know thank you!

Hi, hope to be able to come closer to pinpointing the issue you’re having. From Tyler’s observation of having the subscriptions updated almost 200 times in a minute, it’s likely that the components where you are using the ‘useEffect’ are rerendering unnecessarily. Given your dependency list, you’re either (a) updating the realm reference, or (b) your component(s) is being unmounted and remounted several times.

From not knowing your component hierarchy and seeing more of the code I can’t give too specific of an answer, but I’d suggest trying to pinpoint what makes those components render or unmount.

If you’re using the ‘@realm/react’ library and the ‘RealmProvider’ component for opening the realm, another approach you can try to be less affected by rerenders of lower level components, is to add your subscriptions as part of the initialSubscriptions instead of in ‘useEffects’.