surrealdb / surrealdb.wasm

A WebAssembly engine for the SurrealDB JavaScript SDK
https://surrealdb.com
Apache License 2.0
112 stars 17 forks source link

Bug: recursive use of an object detected which would lead to unsafe aliasing in rust #87

Open orimay opened 2 months ago

orimay commented 2 months ago

Describe the bug

I keep getting the same error, can't track the root of where it's coming from.

patched.js:1121 
 Uncaught 
Error: recursive use of an object detected which would lead to unsafe aliasing in rust
    at __wbindgen_throw (patched.js:1121:19)
    at 036c96f6:0xbb3af6
    at 036c96f6:0xbb3b05
    at 036c96f6:0xa8912e
    at 036c96f6:0x4c57dc
    at 036c96f6:0x7d5013
    at 036c96f6:0xab61d3
    at __wbg_adapter_58 (patched.js:264:10)
    at real (patched.js:231:20)
__wbindgen_throw    @   patched.js:1121
$func17604  @   036c96f6:0xbb3af6
$func17605  @   036c96f6:0xbb3b05
$func12906  @   036c96f6:0xa8912e
$func782    @   036c96f6:0x4c57dc
$func3470   @   036c96f6:0x7d5013
$__wbindgen_export_5    @   036c96f6:0xab61d3
__wbg_adapter_58    @   patched.js:264
real    @   patched.js:231
IndexedDB (async)       
(anonymous) @   patched.js:751
handleError @   patched.js:269
__wbg_get_5361b64cac0d0826  @   patched.js:750
$func8944   @   036c96f6:0xa1c086
$func1879   @   036c96f6:0x68527a
$func2734   @   036c96f6:0x7256e9
$func4368   @   036c96f6:0x8a7f36
$func1194   @   036c96f6:0x58df33
$func15310  @   036c96f6:0xac05be
$func169    @   036c96f6:0x1c8bc9
$func144    @   036c96f6:0x8ed9c
$func172    @   036c96f6:0x1d343b
$func2943   @   036c96f6:0x73e7ef
$func1642   @   036c96f6:0x61884f
$func854    @   036c96f6:0x4f7e35
$func782    @   036c96f6:0x4d3466
$func3470   @   036c96f6:0x7d5013
$__wbindgen_export_5    @   036c96f6:0xab61d3
__wbg_adapter_58    @   patched.js:264
real    @   patched.js:231

Steps to reproduce

Not sure if this is enough (the project is big), but here's my setup:

import { Emitter, Engine, Surreal, type EngineEvents } from 'surrealdb.js';
import { surrealdbWasmEngines } from 'surrealdb.wasm';

const db = new Surreal({
  engines: surrealdbWasmEngines({
    strict: false,
    capabilities: {
      guest_access: true,
      functions: true,
      network_targets: true,
    },
  }) as Record<string, new (emitter: Emitter<EngineEvents>) => Engine>, // this is not properly typed in surrealdb.wasm
});

Expected behaviour

Should not error out

SurrealDB version

surrealdb.js: 1.0.0-beta.9, surrealdb.wasm: 1.0.0-beta.12

Contact Details

dmitrii.a.baranov@gmail.com

Is there an existing issue for this?

Code of Conduct

orimay commented 2 months ago

It turns out, it is related to this issue: https://stackoverflow.com/questions/61296252/failed-to-execute-put-on-idbobjectstore-the-transaction-has-finished

IndexDB doesn't really like multiple asynchronous requests. Once I wrapped query in mutex, it worked:

import { Mutex } from 'mutex-ts';

const mutex = new Mutex();

const query = db.query;
db.query = async <T extends unknown[]>(
  ...args: [string | PreparedQuery, Record<string, unknown> | undefined]
) => {
  const release = await mutex.obtain();
  try {
    return await (query.apply(db, args) as Promise<T>);
  } finally {
    release();
  }
};

mutex-ts is my library.

orimay commented 1 month ago

I am also still getting it sometimes, but not sure how to fix it on my end

limcheekin commented 1 month ago

Facing the same issue here on surrealdb.js: 1.0.0-beta.14, surrealdb.wasm: 1.0.0-beta.14.

limcheekin commented 3 weeks ago

Just re-test in surrealdb.js: 1.0.0-beta.20 and surrealdb.wasm: 1.0.0-beta.16, it is blocking issue holding back surrealdb.wasm to be used in any web application.

orimay commented 2 weeks ago

@limcheekin, you may try using my approach above for the time being

limcheekin commented 2 weeks ago

@orimay Thanks, I am aware about your approach, but I am using surrealdb.wasm in a Flutter Web application.

ibilux commented 2 weeks ago

+1