workers icon indicating copy to clipboard operation
workers copied to clipboard

Cannot read properties of undefined (reading 'exports')

Open bitmage opened this issue 9 months ago • 0 comments

Sometimes the wrapper JS is blowing up trying to access this._inst.exports. This is usually caused by a separate error, but when the wrapper code fails in this way, it obscures the underlying error.

I've been able to trigger this in at least two different ways:

  1. calling http.Get inside my handler - which I realize now is unsupported and I should use the fetch api per this example
  2. responding to OPTIONS in order to implement CORS headers

The code that's erroring is here:

"syscall/js.valueNew": (sp) => {
  sp >>>= 0;
  try {
    const v = loadValue(sp + 8);
    const args = loadSliceOfValues(sp + 16);
    const result = Reflect.construct(v, args);
    sp = this._inst.exports.getsp() >>> 0; // see comment above
    storeValue(sp + 40, result);
    this.mem.setUint8(sp + 48, 1);
  } catch (err) {
    sp = this._inst.exports.getsp() >>> 0; // see comment above
    storeValue(sp + 40, err);
    this.mem.setUint8(sp + 48, 0);
  }
},

In the code, this._inst is undefined, because prior to this code running, wasmExit has been called:

"runtime.wasmExit": (sp) => {
  sp >>>= 0;
  const code = this.mem.getInt32(sp + 8, true);
  this.exited = true;
  delete this._inst;
  delete this._values;
  delete this._goRefCounts;
  delete this._ids;
  delete this._idPool;
  this.exit(code);
},

It seems that under normal circumstances, we should not be calling wasmExit while there is still code running (perhaps callbacks/promises/goroutines). One of the things I tried to do to work around this was to put this code in a 500ms setTimeout block, which can be used after following the Cloudflare instructions on timers. This did eliminate this particular error, but:

  1. Did not reveal the underlying error
  2. Eventually blew up with a null value even deeper in the wasm code with "null function or function signature mismatch" inside wasm_pc_f_loop.

Maybe someone else can understand what's going on here, or what to do about it.

bitmage avatar Apr 07 '25 04:04 bitmage