Skip to content

Exposed function deadlocks if called too early #665

@EarlMilktea

Description

@EarlMilktea

Problem description

Wrapped function does not return when called before the worker is ready.

Minimum codes to reproduce

Deps.

  • comlink 4.4.1
  • esbuild 0.23.0 for bundling
// task.ts: Dummy task
// task.ts

// Dummy task
export async function dummyTask(time: number) {
  return new Promise((resolve) => {
    setTimeout(resolve, time);
  });
}
// worker.ts
import * as Comlink from "comlink";
import { dummyTask } from "./task.js";

// Wait for 500 ms
// Modeling long-running tasks (ex. WASM compilation)
await dummyTask(500);

Comlink.expose({
  dummy: () => {
    return 42;
  },
});
// index.ts
import * as Comlink from "comlink";
import { dummyTask } from "./task.js";

const url = new URL("./worker.js", import.meta.url);

const wrapper: Comlink.Remote<{
  dummy: () => number;
}> = Comlink.wrap(new Worker(url, { type: "module" }));

// Works without problem with this line
// await dummyTask(1000);

// Never returns
const x = await wrapper.dummy();

// Unreachable
console.log(x);

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