Skip to content

[BUG]: Firefox: PostGIS fails at random with RuntimeError: indirect call to null #1254

Backlog

Description

@Gheop

Describe the bug

In Firefox, loading the PostGIS extension fails at random with RuntimeError: indirect call to null during CREATE EXTENSION postgis. A retry on a new instance (or a new clone()) usually succeeds. Chromium and Node never fail.

To Reproduce

A self-contained page (imports PGlite 0.6.1 and @electric-sql/pglite-postgis 0.3.1 from jsDelivr):

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>PGlite + PostGIS: "indirect call to null" in Firefox</title>
<style>body{font:15px/1.5 system-ui,sans-serif;max-width:860px;margin:2em auto;padding:0 1em}pre{background:#f4f4f4;padding:1em;white-space:pre-wrap}</style>
<h1>PGlite 0.6.1 + PostGIS 0.3.1: <code>RuntimeError: indirect call to null</code> in Firefox</h1>
<p>Each attempt opens a PGlite instance with the PostGIS extension, runs <code>CREATE EXTENSION postgis</code> and one PostGIS function, then closes it.
In Chromium every attempt succeeds. In Firefox a large share of attempts fail at random with <code>indirect call to null</code>, and a retry on a new instance usually succeeds.</p>
<p>Parameters: <code>?n=20</code> (attempts), <code>?mode=clone</code> (default: clone one base instance per attempt) or <code>?mode=fresh</code> (a new <code>PGlite.create()</code> per attempt).</p>
<pre id="o">running…</pre>
<script type="module">
import { PGlite } from "https://cdn.jsdelivr.net/npm/@electric-sql/pglite@0.6.1/dist/index.js";
import { postgis } from "https://cdn.jsdelivr.net/npm/@electric-sql/pglite-postgis@0.3.1/dist/index.js";

const q = new URLSearchParams(location.search);
const n = Number(q.get("n") ?? 20), mode = q.get("mode") ?? "clone";
const out = document.getElementById("o");
const log = (line) => { out.textContent += `\n${line}`; console.log(line); };
out.textContent = `${navigator.userAgent}\nmode=${mode} n=${n}`;

const SQL = "CREATE EXTENSION postgis; SELECT ST_AsText(ST_Buffer(ST_MakePoint(1, 2), 1, 2));";
const base = mode === "clone" ? await PGlite.create({ extensions: { postgis } }) : null;
const result = { ok: 0, failed: 0, errors: {}, firstFailure: null };
for (let i = 1; i <= n; i++) {
  const t0 = performance.now();
  const db = base ? await base.clone() : await PGlite.create({ extensions: { postgis } });
  try {
    await db.exec(SQL);
    result.ok++;
    log(`attempt ${i}: ok (${Math.round(performance.now() - t0)} ms)`);
  } catch (e) {
    result.failed++;
    result.errors[e.message] = (result.errors[e.message] ?? 0) + 1;
    result.firstFailure ??= String(e.stack ?? e).split("\n").slice(0, 12).join("\n");
    log(`attempt ${i}: FAILED (${Math.round(performance.now() - t0)} ms): ${e.message}`);
  } finally {
    await db.close().catch(() => {});
  }
}
window.result = result;
log(`\nsummary: ${result.ok} ok, ${result.failed} failed\n${JSON.stringify(result.errors)}`);
if (result.firstFailure) log(`\nfirst failure stack:\n${result.firstFailure}`);
log("done");
</script>
</html>

Open it with ?mode=clone&n=20 (one base instance cloned per attempt) or ?mode=fresh&n=20 (a new PGlite.create() per attempt).

Results

Browser, mode Wasm prefs OK Failed
Firefox 161, clone defaults 6 14
Firefox 161, fresh defaults 9 11
Firefox 161, clone javascript.options.wasm_optimizingjit = false (baseline only) 10 10
Firefox 161, clone javascript.options.wasm_baselinejit = false (optimizing only) 19 1
Firefox 161, clone javascript.options.wasm_lazy_tiering = false no result: the page made no progress for 20 minutes (not investigated)
Chromium 159, clone 20 0
Chromium 159, fresh 20 0

20 attempts per row, headless browsers driven by puppeteer-core (Firefox) and Playwright (Chromium), Linux x86_64.

Every failure:

could not load library "/pglite/lib/postgresql/postgis-3.so": could not load dynamic lib: /pglite/lib/postgresql/postgis-3.so
RuntimeError: indirect call to null

First frames of the stack (CDN build, not symbolized):

O@https://cdn.jsdelivr.net/npm/@electric-sql/pglite@0.6.1/dist/chunk-DFZEZSL3.js:1:11521
Oe@https://cdn.jsdelivr.net/npm/@electric-sql/pglite@0.6.1/dist/chunk-DFZEZSL3.js:1:18272
…
@https://cdn.jsdelivr.net/npm/@electric-sql/pglite@0.6.1/dist/chunk-GE4YWJAD.js line 1 > WebAssembly.Module:wasm-function[0]:0x0
@https://cdn.jsdelivr.net/npm/@electric-sql/pglite@0.6.1/dist/pglite.wasm:wasm-function[11422]:0x565b8b
@https://cdn.jsdelivr.net/npm/@electric-sql/pglite@0.6.1/dist/pglite.wasm:wasm-function[3764]:0x22c9dc

Analysis

Disabling Firefox's baseline wasm compiler (javascript.options.wasm_baselinejit = false) almost removes the failures (1 of 20). Disabling the optimizing one does not help (10 of 20 still fail). Chromium never fails. That points to a SpiderMonkey bug in baseline-compiled code around the function-table entries the Emscripten dynamic linker writes when it loads postgis-3.so. Reported to Mozilla: https://bugzilla.mozilla.org/show_bug.cgi?id=2099628

Two questions for the PGlite side:

  1. Could the extension loader avoid the trigger? For example, build PostGIS without dynamic linking, or link it eagerly at instance creation instead of at the first CREATE EXTENSION.
  2. Until Firefox is fixed, would you take a retry, or a documented note for Firefox users?

Workaround

We retry on a new clone when the error message matches indirect call to null (up to 40 times). In our workload that is about 1.4 s per failed attempt, and 3 to 5 failures per analysis on average.

Environment

  • PGlite 0.6.1 (PostgreSQL 19.0), @electric-sql/pglite-postgis 0.3.1
  • Firefox 161.0 on Linux x86_64; Chromium 159 for comparison

Activity

  1. changed the title [-]Firefox: PostGIS fails at random with `RuntimeError: indirect call to null`[/-] [+][BUG]: Firefox: PostGIS fails at random with `RuntimeError: indirect call to null`[/+] on Dec 1, 2026
  2. Gheop commented on Dec 1, 2026

    @Gheop
    Author

    More data: the same failure reproduces in a second, independent engine, deterministically.

    Ladybird 2026.48.2 (LibWasm interpreter, no JIT), Linux x86_64, headless via its WebDriver, same repro.html as above:

    Engine CREATE EXTENSION postgis Control: CREATE EXTENSION pgcrypto (also a dlopen()ed side module)
    Ladybird 2026.48.2 20 of 20 failed (10 clone, 10 fresh) 10 of 10 ok
    Firefox 161 12 to 14 of 20 failed 20 of 20 ok
    Firefox Nightly 163.0a1 13 to 16 of 20 failed not run
    Chromium 159 0 of 20 failed not run

    Ladybird error, at the same point as Firefox (while loading postgis-3.so):

    could not load library "/pglite/lib/postgresql/postgis-3.so": could not load dynamic lib: /pglite/lib/postgresql/postgis-3.so
    RuntimeError: Wasm execution trapped (WIP): uninitialized element
    

    That is a call_indirect to an uninitialized table element, the same condition Firefox reports as indirect call to null. The control page only swaps PostGIS for pgcrypto (CREATE EXTENSION pgcrypto; SELECT encode(digest('a', 'sha256'), 'hex');), so side-module loading works in general and the failure is specific to postgis-3.so.

    Two engines failing at the same call, only with PostGIS, while V8 never fails, suggests the PostGIS side module (or the order in which its table entries are filled during dlopen) may rely on behavior that V8 happens to tolerate, rather than a single engine bug. Ladybird's WebAssembly support is still incomplete, so this is a hint, not a proof. Its failure is deterministic, though, which may make it easier to debug than the intermittent Firefox case.

  3. Gheop commented on Dec 1, 2026

    @Gheop
    Author

    Root cause found. This is a race in how the extension's side modules are preloaded, not a browser engine bug. It explains Firefox (random), Ladybird (always) and Chromium (never).

    What happens

    1. loadExtensions untars postgis.tar.gz and calls FS.createPreloadedFile for each .so without awaiting the previous one, so postgis-3.so, postgis_raster-3.so and postgis_topology-3.so go through Emscripten's wasm preload plugin (loadWebAssemblyModule(..., {loadAsync: true})) concurrently.
    2. Each WebAssembly.instantiate reads its imports, which creates the module's GOT.mem / GOT.func entries in the global GOT with value -1.
    3. When one module finishes, postInstantiation runs updateGOT(exports) and then reportUndefinedSymbols(). That function scans the whole global GOT, including the -1 entries of a sibling whose imports have been read but whose postInstantiation has not run yet. Those symbols are not resolvable yet, so it throws (TypeError: can't access property "value", _ is undefined).
    4. The preload of that module fails after updateGOT registered its exports in the GOT, and before __wasm_apply_data_relocs and __wasm_call_ctors ran.
    5. At CREATE EXTENSION postgis, dlopen instantiates postgis-3.so again. updateGOT keeps the existing entries (value != -1), so the new instance resolves its own symbols to the zombie instance from step 4, whose data was never relocated. Its constructors call a function pointer that is still 0: indirect call to null in Firefox, uninitialized element in Ladybird.

    Whether this triggers depends only on the order in which the three compilations finish and on when each engine reads the imports, which explains the engine differences.

    Loader log (local copy of PGlite 0.6.1 with logging added in postInstantiation, the preload plugin, reportUndefinedSymbols and dlopenInternal). Failing Firefox 161 attempt:

    postInstantiation /pglite/lib/postgresql/postgis-3.so tableBase=8783 size=9231
    required unresolved _TIFFBuiltinCODECS
    preload FAIL /pglite/lib/postgresql/postgis-3.so: TypeError: can't access property "value", _ is undefined
    postInstantiation /pglite/lib/postgresql/postgis_raster-3.so tableBase=18014 size=9374
    required unresolved __gmpn_clz_tab
    preload FAIL /pglite/lib/postgresql/postgis_raster-3.so: TypeError: can't access property "value", _ is undefined
    postInstantiation /pglite/lib/postgresql/postgis_topology-3.so tableBase=27388 size=9224
    preload FAIL /pglite/lib/postgresql/postgis_topology-3.so: RuntimeError: indirect call to null
    postInstantiation /pglite/lib/postgresql/postgis-3.so tableBase=55215 size=9231
    dlopen FAIL /pglite/lib/postgresql/postgis-3.so: RuntimeError: indirect call to null
    

    Ladybird gives the same sequence on every attempt. Chromium always logs preload ok for the three modules, in order. Firefox attempts where only postgis_raster-3 or postgis_topology-3 fails to preload still succeed, because neither is dlopen()ed afterwards.

    Workaround, verified. Run the side-module instantiations one at a time, each starting only after the previous postInstantiation has run:

    const instantiate = WebAssembly.instantiate;
    let queue = Promise.resolve();
    const isSideModule = (src) => {
      if (src instanceof WebAssembly.Module) return WebAssembly.Module.customSections(src, "dylink.0").length > 0;
      const u = src instanceof ArrayBuffer ? new Uint8Array(src)
        : ArrayBuffer.isView(src) ? new Uint8Array(src.buffer, src.byteOffset, src.byteLength) : null;
      if (!u || u.length < 24 || u[8] !== 0) return false;   // dylink.0 is the first section
      let p = 9; while (u[p] & 0x80) p++; p++;
      return u[p] === 8 && String.fromCharCode(...u.subarray(p + 1, p + 9)) === "dylink.0";
    };
    WebAssembly.instantiate = function (source, imports) {
      if (!isSideModule(source)) return instantiate.call(WebAssembly, source, imports);
      const turn = queue.then(() => instantiate.call(WebAssembly, source, imports));
      const next = () => new Promise((r) => setTimeout(r, 0));   // postInstantiation runs in the microtask after resolution
      queue = turn.then(next, next);
      return turn;
    };

    Same repro.html, with only this added before the imports:

    Engine Before After
    Firefox 161 26 of 40 failed 0 of 40 failed
    Firefox Nightly 163.0a1 29 of 40 failed 0 of 40 failed
    Ladybird 2026.48.2 20 of 20 failed 0 of 20 failed
    Chromium 159 0 of 40 failed 0 of 40 failed

    In Node, the cost is within noise (8.4 s vs 8.3 s for our end-to-end check of a PostGIS model).

    Suggested fixes

    • In PGlite: preload the .so files of the extension bundles sequentially (await each createPreloadedFile before starting the next) instead of collecting the promises for Promise.all. That is the smallest change and removes the race for every multi-module extension, and also between bundles (e.g. postgis together with a contrib extension).
    • In Emscripten, probably worth an upstream issue: reportUndefinedSymbols() checks GOT entries belonging to other modules still in flight, and a failed load leaves its GOT entries pointing to an instance that was never relocated, which later loads then reuse.

    This also corrects my earlier comment: the module itself is fine, and the Ladybird data point was the same race with a deterministic order. I'm closing the Firefox bug (https://bugzilla.mozilla.org/show_bug.cgi?id=2099628) as not a SpiderMonkey issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions