Skip to content

[SM-EVOLUTION][JobQueue] Unify SpiderMonkey jobs, Bao event loop, Servo tasks and Node/Bun microtask semantics #25

Description

@putao520

Parent: #22
Depends on: #23 for final Realm ownership
Feeds: #15 Runtime, #12 Node, #13 Bun, #19 Performance

目标

把 Bao 已有的 SpiderMonkey JobQueue 从“能正确跑 Promise jobs”升级为全 Runtime 单一、可观测、生命周期正确的 microtask/job scheduling contract,与 Servo task、Bao event loop、Node/Bun nextTick/timers/async completion 明确排序。

当前基线

src/bao_engine/src/job_queue.rs 已经:

  • 使用 mozjs CreateJobQueue/SetJobQueue/RunJobs
  • 为 job 做 GC-safe rooting;
  • 在正确 Realm 运行 job;
  • 路由 uncaught job exception;
  • drain 尾部 flush unhandled rejections。

这说明本 Issue 不是重写队列,而是审计和消除“多个 scheduler 之间谁先谁后、何时 drain、谁负责 wakeup”的隐式合同。

Audit inventory

  • 列出所有 JobQueue::drain / RunJobs 调用点。
  • Node process.nextTick、queueMicrotask、Promise jobs、timers、fetch/fs/crypto completion、Bun.serve callbacks、Servo tasks、CDP evaluate completion 的调度入口。
  • 所有 wakeup/idle/pump 路径。
  • browser/CLI/worker 各模式是否存在不同 drain 语义。

Required scheduling model

建立 machine-testable ordering contract,至少覆盖:

  • current JS stack
  • nextTick-like queue where Node semantics require it
  • SM Promise JobQueue / microtasks
  • timers
  • native async completion
  • Servo web tasks
  • CDP/browser control tasks
  • idle/reclamation work

不要先假定 Node 或 Bun 的顺序,应分别针对目标兼容面定义并在共享 scheduler 中实现必要 policy。

Key correctness work

  • Job enqueue 时 global/Realm ownership 不丢。
  • navigation/page close 后旧 Realm 的 queued job 不执行到新 document。
  • queue drain 抛异常后后续 job 与 rejection tracker 语义明确。
  • nested drain/reentrant callback 不造成 double execution。
  • worker shutdown/drop 时 queue 能清理。
  • runtime close 时 pending job 有明确 settle/cancel policy。
  • queue empty/wakeup 不丢事件,不靠 busy-spin。

Performance work

  • 记录 enqueue/drain overhead。
  • 审计每 job 用 global property rooting 的成本;只有实测证明必要时才优化存储结构。
  • 禁止为了性能降低 GC safety。
  • high-volume microtask benchmark 与 starvation test。

Tests

  • Promise → queueMicrotask → timer ordering
  • nextTick/Promise ordering under Node target
  • native async completion followed by Promise continuation
  • nested microtasks
  • job throws + remaining queue
  • unhandled rejection lifecycle
  • navigation with pending jobs
  • page close with pending jobs
  • 100k microtask stress / starvation
  • multi-page queue isolation

Definition of Done

禁止

  • 不允许用 sleep/额外 drain 次数碰巧修顺序。
  • 不允许为 Node/Bun/Web 各复制一份 JobQueue。
  • 不允许把 exception clear 当成功继续而不记录语义。

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions