realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.62k stars 558 forks source link

Unhandled session token refresh error #1116

Closed mklb closed 5 years ago

mklb commented 6 years ago

I get a session refresh error after running an app for a view minutes in react native on iOS:

Unhandled session token refresh error: could not look up session at path /Users/XXXXXXX/Library/Developer/CoreSimulator/Devices/85A472DA-CBCB-416B-871B-50CA0DC03DFD/data/Containers/Data/Application/865345A1-6C84-4470-8055-0F9A65A86452/Documents/realm-object-server/dfeddc7733c15fb2b3108c217c1ce0db/realm%3A%2F%2Flocalhost%3A9080%2F%7E%2F__management

React native: 0.45 Realm: 1.8.3 Realm Obj. Server: 1.7.6 Simulator Device Version: iOS 10.3

bmunkholm commented 6 years ago

@mklb Can you post more details? Possibly ways for us to reproduce please?

shenjiayu commented 6 years ago

@bmunkholm same here...

error log from app:

console.error: "Unhandled session token refresh error: could not look up session at path /simulator-path/Documents/realm-object-server/6ea9a988673f211a42b90bdaa2c73b82/realm%3A%2F%2F192.168.1.78%3A9080%2F%7E%2Ftasks"

error
    YellowBox.js:67:16
print_error
    user-methods.js:59:41
<unknown>
    user-methods.js:109:20
tryCallOne
    core.js:37:14
<unknown>
    core.js:123:25
callTimer
    JSTimersExecution.js:95:8
callImmediatesPass
    JSTimersExecution.js:199:36
callImmediates
    JSTimersExecution.js:214:11
__guard
    MessageQueue.js:213:6
__callImmediates
    MessageQueue.js:223:17
<unknown>
    MessageQueue.js:102:6
__guard
    MessageQueue.js:213:6
callFunctionReturnFlushedQueue
    MessageQueue.js:100:17

log from server:

2017-07-05T07:31:38.160Z - info: sync-server: Sync Connection[40]: Session[6]: Received: REFRESH(signed_user_token='...YUf61E3dA6g8a/Q/d/OY9rH7v9oQ==')

It's probably triggered by refreshing the token.

fealebenpae commented 6 years ago

Did you open a realm with sync that you then close()-d or left for the garbage collector?

shenjiayu commented 6 years ago

@fealebenpae Yes, I did open a realm with sync. But I never called close()...

fealebenpae commented 6 years ago

Do you save it in a field somewhere, or just in a local variable?

shenjiayu commented 6 years ago

@fealebenpae

let realm = null;
...
export default {
    ...,
    realm
};

I exported it for global use. I followed the React-native example from demo folder in Realm-mobile-platform bundle.

fealebenpae commented 6 years ago

Would it be possible to send your project to help@realm.io?

shenjiayu commented 6 years ago

@fealebenpae Yeah..Sure..

shenjiayu commented 6 years ago

@fealebenpae I sent the my project to the email, but I haven't gotten the reply yet.. Same error occurred when I called Realm.open to asynchronously open a realm and assign it the a variable like this

Realm.open({...}).then(realm => {
    this.realm = realm;
    // rest actions
}).catch(error => {
    // handle error 
});
shenjiayu commented 6 years ago

@fealebenpae Once the realm is instantiated with

Realm.open({
    schema: [...],
    sync: {
        user,
        url: ..
    },
    path: ... <= the customised path ('tasks.realm' in my case) triggered the error
});

the default one is working perfectly, could you please look into it?

fealebenpae commented 6 years ago

Hey @shenjiayu,

I’m sorry, I have been busy with other tasks and haven’t had the chance to look at your project yet. Thanks for the path hint - this helps me narrow down my search area.

mklb commented 6 years ago

this is how I use - and I think should use the library.

Realm.Sync.User[action](Constants.dBUrl, username, password, (error, user) => {
  if (error) {
    // ....
  } else {
    this.realm = new Realm({
      schema: [..,..,..],
      schemaVersion: 5,
      sync: {
        user,
        url: XXXXXXX,
      },
    })

    this.realm.objects('XYYY').addListener((allObjects, changes) => {
      // ...
    })
  }
})

I could imagine that the error is thrown when I login with a not logged out user. Maybe because I often close and delete/reinstall the app without logging the user out..

However: I logged every user out before deleting the app today but I had this error every ~10 min... I planed to release my App this week but there is now way I can do it like that.

Edit:

fealebenpae commented 6 years ago

@mklb is your error also could not look up session at path?

mklb commented 6 years ago

I opened the issue (first post). Yes ;) Same stack trace as @shenjiayu

fealebenpae commented 6 years ago

You could go to node_modules/realm/lib/user-methods.js and comment out this line: https://github.com/realm/realm-js/blob/master/lib/user-methods.js#L110 to disable the error. I still haven't been able to reproduce it and find out what's happening - it could be a false positive, but it could indicate a deeper issue. If you disable the error, does syncing still work - can you see changes reflected in the Realm Browser?

Also, in your app's stdout log, do you get a line that says Closing Realm file and the path you get in the error before you get the error?

mklb commented 6 years ago

I did not uncomment the code but I can tell you that syncing does work after clicking the dismiss button of the react native error popup view. So I guess your suggestion as a temporary workaround for a production app would be okay.. ¯_(ツ)_/¯

fealebenpae commented 6 years ago

I made a complete and total stab-in-the-dark sort of fix. @mklb @shenjiayu could you please try it out?

realm-1.9.0.tgz.zip

(I had to zip it because GitHub doesn't like .tgz)

mklb commented 6 years ago

Did I do it right? I changed the package content of the realm folder in /node_modules with your package.

During the build phase it said:

Downloading dependency: core 2.8.6
https://static.realm.io/downloads/core/realm-core-2.8.6.tar.xz
Downloading dependency: sync 1.10.5
https://static.realm.io/downloads/sync/realm-sync-cocoa-1.10.5.tar.xz

Does that mean your changes are overwritten because I did not change the version nr. in the package.json or are these just new dependencies?

Anyway, I´m testing it now and will edit this post in 15 min ;) Hopefully it will work. Thanks in advance for your effort and quick replies!


EDIT: Damn (╯°□°)╯︵ ┻━┻ After 5 min the same but different phrased error appeared.

Could not look up session belonging to realm /Users/XXX/Library/Developer/CoreSimulator/Devices/EC50FDC6-FE1B-4500-B645-88C0E52EC73F/data/Containers/Data/Application/631E1A8B-1255-459E-A523-633C100A225D/Documents/realm-object-server/002a2c5ae27451544ffd2f92a3913759/realms%3A%2F%2FMY_SECRET_REALM_URL%3A9443%2F%7E%2F__management

error
    YellowBox.js:67:16
print_error
    user-methods.js:61:41
<unknown>
    user-methods.js:112:20
tryCallOne
    core.js:37:14
<unknown>
    core.js:123:25
callTimer
    JSTimersExecution.js:95:8
callImmediatesPass
    JSTimersExecution.js:199:36
callImmediates
    JSTimersExecution.js:214:11
__guard
    MessageQueue.js:213:6
__callImmediates
    MessageQueue.js:223:17
<unknown>
    MessageQueue.js:102:6
__guard
    MessageQueue.js:213:6
callFunctionReturnFlushedQueue
    MessageQueue.js:100:17

before that I see these logs. So, at least in my case, everything refreshes correctly accept the _management realm:

refreshed access token on /Users/XXX/Library/Developer/CoreSimulator/Devices/EC50FDC6-FE1B-4500-B645-88C0E52EC73F/data/Containers/Data/Application/631E1A8B-1255-459E-A523-633C100A225D/Documents/realm-object-server/002a2c5ae27451544ffd2f92a3913759/realm%3A%2F%2FMY_SECRET_REALM_URL%3A9080%2F%7E%2FmainDB
refreshed access token on /Users/XXX/Library/Developer/CoreSimulator/Devices/EC50FDC6-FE1B-4500-B645-88C0E52EC73F/data/Containers/Data/Application/631E1A8B-1255-459E-A523-633C100A225D/Documents/realm-object-server/002a2c5ae27451544ffd2f92a3913759/realm%3A%2F%2FMY_SECRET_REALM_URL%3A9080%2F_userDir4
refreshed access token on /Users/XXX/Library/Developer/CoreSimulator/Devices/EC50FDC6-FE1B-4500-B645-88C0E52EC73F/data/Containers/Data/Application/631E1A8B-1255-459E-A523-633C100A225D/Documents/realm-object-server/002a2c5ae27451544ffd2f92a3913759/realm%3A%2F%2FMY_SECRET_REALM_URL%3A9080%2F0846ce1505dccb0139d36d51f8d2cc7e%2Fpublic
refreshed access token on /Users/XXX/Library/Developer/CoreSimulator/Devices/EC50FDC6-FE1B-4500-B645-88C0E52EC73F/data/Containers/Data/Application/631E1A8B-1255-459E-A523-633C100A225D/Documents/realm-object-server/002a2c5ae27451544ffd2f92a3913759/realm%3A%2F%2FMY_SECRET_REALM_URL%3A9080%2F%7E%2Fpublic
fealebenpae commented 6 years ago

Does that mean your changes are overwritten

Nah, it’s only downloading the core engine binaries - React Native is a layer above that.

I really hope this helps.

mklb commented 6 years ago

Edited my post. Sadly an error is thrown T_T

fealebenpae commented 6 years ago

How did you use the management realm? Did you log out any users?

mklb commented 6 years ago

Sorry for the late reply! I tried it again as follows:

  1. Deleted the app from the virtual device
  2. installed my app and registered as a new user to create fresh realms
  3. ~10 min later the error occurs as above (Realm.Sync: Could not look up session belonging to realm..). just the __management realm does not refresh the session correctly

PS: I want to point out that this error is thrown from Realm.Sync which react-native log-ios indicates (which I did not mention/saw before because I copied the error from the React Native Error View..)

mklb commented 6 years ago

I will delete the realm server and install a fresh build. maybe the problem will be fixed this way..

Lets say I release an app with the monkey patch above ( https://github.com/realm/realm-js/issues/1116#issuecomment-314741116 ). We learned / saw that only the users __management realms would not be synced after their first token expired.

In my case this would not be a problem since my users can´t change anything in the management realm anyway after the account is created... The question is: is there any other drawback that I am missing or is it okay to just don't care about the __management realm in my case?

kneth commented 6 years ago

@mklb Did the fresh installation solve the issue?

kneth commented 6 years ago

@mklb I close the issue. Don't hesitate to reopen or create a new.

mklb commented 6 years ago

@kneth sorry for the late reply. No it did not solve anything. this are my dependencies from package.json

"realm": "^2.0.4",
"react": "16.0.0-alpha.12",
"react-native": "0.45",

this is my ROS log output the same second the error is thrown. I cant see any error message in the ROS logs. _management realm shows 200:

info: [http] 127.0.0.1 - GET /realms/files/%2F5bb861a20ec60038912f0af87b995b82%2Fpublic HTTP/1.1 200 73 - 8.084 ms
info: [http] 127.0.0.1 - POST /auth HTTP/1.1 200 912 - 15.927 ms
info: [http] 127.0.0.1 - GET /realms/files/%2F_userDir4 HTTP/1.1 200 43 - 13.982 ms
info: [http] 127.0.0.1 - GET /realms/files/%2F5bb861a20ec60038912f0af87b995b82%2FmainDB HTTP/1.1 200 73 - 13.852 ms
info: [http] 127.0.0.1 - POST /auth HTTP/1.1 200 821 - 21.199 ms
info: [http] 127.0.0.1 - POST /auth HTTP/1.1 200 912 - 23.900 ms
info: [http] 127.0.0.1 - GET /realms/files/%2F5bb861a20ec60038912f0af87b995b82%2F__management HTTP/1.1 200 79 - 7.156 ms
info: [http] 127.0.0.1 - POST /auth HTTP/1.1 200 926 - 11.901 ms

No Idea what is happening. Thought that the problem solved itself somehow a few hours ago..than the error came back. Maybe this is because I deleted the app without logging a user out? the flow would be: 1) delete the app...the file from the error message gets destroyed 2) install the app again and log in 3) ..sync realms are there again and sdk looks for the old lokal file?


EDIT: Just invalidated my thoughts above. Tested it on a real iPhone. No difference, same error message! But this time I see an error in the ROS logs as well

info: [http] 192.168.0.4 - POST /auth HTTP/1.1 200 821 - 12.686 ms
info: [http] 127.0.0.1 - GET /realms/files/%2F5bb861a20ec60038912f0af87b995b82%2Fpublic HTTP/1.1 200 73 - 11.697 ms
info: [http] 192.168.0.4 - POST /auth HTTP/1.1 200 912 - 18.865 ms
error: [sync] Sync Connection[125]: Session[2]: Token expired (signed_user_token='...KHGwpb98uOYAZvp36UZRBKF3pOXw==').
error: [sync] Sync Connection[125]: Session[2]: Token expired (message_type='bind', signed_user_token='...KHGwpb98uOYAZvp36UZRBKF3pOXw==').
info: [http] 127.0.0.1 - GET /realms/files/%2F5bb861a20ec60038912f0af87b995b82%2F__management HTTP/1.1 200 79 - 1.961 ms
error: [sync] Sync Connection[126]: Session[3]: Token expired (signed_user_token='...GA54xXXvL5fRQZvew62FMl5DeZOQ==').
error: [sync] Sync Connection[126]: Session[3]: Token expired (message_type='bind', signed_user_token='...GA54xXXvL5fRQZvew62FMl5DeZOQ==').
info: [http] 192.168.0.4 - POST /auth HTTP/1.1 200 926 - 17.143 ms
info: [http] 127.0.0.1 - GET /realms/files/%2F5bb861a20ec60038912f0af87b995b82%2Fpublic HTTP/1.1 200 73 - 1.665 ms
info: [http] 192.168.0.4 - POST /auth HTTP/1.1 200 912 - 5.402 ms
info: [http] 127.0.0.1 - GET /realms/files/%2F_userDir4 HTTP/1.1 200 43 - 3.230 ms
info: [http] 192.168.0.4 - POST /auth HTTP/1.1 200 821 - 9.559 ms
mklb commented 6 years ago

My plan for finally releasing the app is to suppress as @fealebenpae suggested the error message. I will uncomment the line 118 from the Realm version 2.0.4. Hope that helps some people out there as well..

Why version 2.0.4? Because the flow allows the app to fully function after that error is thrown / surppressed. This is not the case for version 2.0.12 btw! Line 103 throws an undefined error for the session variable because the if statement from line 99 does not catch that..

kneth commented 6 years ago

And you didn't add an error handler? It might help understand what the error is.

mklb commented 6 years ago

as far as I can see the version 2.0.4 just refreshes the token from the server so I dont need to do anything..? ¯_(ツ)_/¯

kneth commented 6 years ago

@mklb Do you have a proxy in front of your object server? 2.1.1 has a fix for that situation.

mklb commented 6 years ago

I do not think so. But I have no idea to be honest. The problem occurs with a local dev AND with a production object server on digital ocean. No idea why the problem is consistent through all the different SDKs and object server versions I have tried this year (see posts above)..

To recap: as far as I understand the SDK is unable to find the local token. I suppressed the error message (SDK 2.0.4) so the app can continue to run. the sdk then just refreshes the token in ram (I guess) from the object server. maybe it even saves it locally on the device and is just unable to find it again.. I don´t know. It's also weird that I am the only one with this issue

I am testing my app in Testflight right now and the solution seems to work

kneth commented 6 years ago

@mklb If TestFlight can provide some insights, please share.

mklb commented 6 years ago

@kneth I invited you via your gmail address ;)

kneth commented 6 years ago

@mklb Thanks but I am an Android user 🤖 . @fealebenpae Can you help us?

fealebenpae commented 6 years ago

@mklb do you still get the could not look up session at path error for the __management realm?

mklb commented 6 years ago

@fealebenpae no, since I suppressed the error as you suggested. So indirekt: of course but It just does not matter. See -> https://github.com/realm/realm-js/issues/1116#issuecomment-349116464

fealebenpae commented 6 years ago

I think I understand. You correctly identified a bug with my original implementation where I never expected the print_error call to be recoverable, so the undefined error when accessing the session error handler wouldn't have been reached.

The relevant code in user-methods.js has been refactored and improved for this condition, I think, so perhaps the latest version should fix this problem for you altogether. Have you tried any newer version than 2.0.12?

mklb commented 6 years ago

I just installed / tested V.2.2.0 with my local ROS V.2.0.16 and received the same error after a few minutes of running the app.. frustrating :(

kneth commented 6 years ago

@mklb Thanks for the update.

@fealebenpae Does it ring a :bell:?

fealebenpae commented 6 years ago

I’m afraid not. I don’t understand how this happens in the first place.

kneth commented 6 years ago

@mklb Please try to upgrade (to 2.2.10). We have fixed a few bugs related to similar reports.

bmunkholm commented 5 years ago

Closing due to inactivity. Please reopen when/if you can provide more details or the problem persist.

lyseiha commented 4 years ago

+1

mklb commented 4 years ago

The problem persisted. This is why I pulled the plug from the realm server for the production app.