Stream.return() on realm websdk crashes mobile chrome

Calling return() on a change stream using the Websdk on Chrome Android causes the browser to crash, showing “Aw, Snap! Something went wrong…” Works fine on Desktop.

Stepping through the sdk source it seems like on desktop, i.return() is a wrapper which then calls cancel() on the underlying stream reader. On mobile, i.return is a native fn.

return(value) {
        iterator.then((i) => i.return ? i.return(value) : void 0);  // <--- i.return is native on mob
        return originalReturn.call(stream, value); // this is fine
}

On mob, it calls return() on the native fn and then steps into the watch() impl. After that the browser crashes.

Specifically I noticed that in the asyncIteratorFromResponseBody function, requests on a mobile browser seem to have a Symbol.asyncIterator in the body and so don’t get wrapped as do the requests from a desktop browser. If I comment the ‘else if’ block out, everything “seems” to work. Although I’m sure there’s a better fix

function asyncIteratorFromResponseBody(body) {
  if (typeof body !== "object" || body === null) {
    throw new Error("Expected a non-null object");
  } else if (Symbol.asyncIterator in body) {   // <---------- this kills chrome on android
    return body;                              // commenting this if block out "fixes" it
  } else if ("getReader" in body) {
    const stream = body;
    return {
      [Symbol.asyncIterator]() {
        const reader = stream.getReader();
        return {
          next() {
            return reader.read();
          },
          async return() {
            await reader.cancel();
            return { done: true, value: null };
          }
        };
      }
    };
  } else {
    throw new Error("Expected an AsyncIterable or a ReadableStream");
  }
}

I’ve yet to test on IOS.

Mobile Chrome: 124.0.6367.54
Android 13; KB2003 Build/RKQ1.211119.001

Desktop Chrome: 123.0.6312.106
Windows 11 Pro, OS build 22631.3447, Feature Experience Pack 1000.22688.1000.0

Seems it’s due to chrome 124. Tracking issue here: calling stream.return() on mobile chrome crashes browser · Issue #6634 · realm/realm-js · GitHub

The same issue occurred on the desktop version of Chrome.

Did you take any measures to address this matter?

For now I just comment out this in node_modules/realm-web :

else if (Symbol.asyncIterator in body) {  
    return body;   

As commented, I’ve raised an issue with the realm-js team. You can track it there.

1 Like