Fixing a WebKit Bug: Are Promises as Reliable as Train Tracks?

Through a case study of fixing a WebKit Promise bug, this discusses the reliability of browser engine asynchronous implementations and engineering verification thinking.
Starting from a Hacker News post about fixing a WebKit Promise bug, the article uses the "train tracks" metaphor to examine the reliability of the Promise mechanism. The author analyzes the complex interactions involving the microtask queue, state machine transitions, and garbage collection in Promise implementations, pointing out that subtle deviations in engines like WebKit can lead to unhandled rejections, timing disorder, or memory leaks. The article discusses common areas for bug localization (microtask scheduling, thenable resolution, cross-context passing) and the engineering approach of fixing as verification, distilling three insights for developers: don't blindly trust the runtime, pay attention to microtask timing, and actively participate in open-source engine fixes. Overall, it emphasizes that continuous scrutiny of foundational mechanisms is essential for building reliable web applications.
Introduction: Starting from an HN Post
On Hacker News, a post titled "Fixing a WebKit bug to see if promises are made of train tracks" sparked a small discussion. The "train tracks" in the title is a metaphor—it suggests that JavaScript's Promise mechanism may appear solid like train tracks but harbors hidden forks and pitfalls. By fixing a real bug in WebKit, the author attempts to verify the reliability boundaries of Promises. Although the post didn't gain much traction (11 points, no comments yet), its technical perspective is worth exploring.
The "Train Tracks" Metaphor for Promises: Why It Matters
Promises are the cornerstone of modern JavaScript asynchronous programming. They promise a future value and provide chaining (then/catch) to organize asynchronous flows. But the "train tracks" metaphor points to a core question: when the implementation of Promises (such as WebKit's JavaScriptCore engine) has defects, will the tracks derail? The author chose to fix a WebKit bug rather than stay in theoretical discussion, reflecting a pragmatic engineering attitude.
From a technical perspective, WebKit's Promise implementation involves the interaction of the microtask queue, state machine transitions, and garbage collection. Any subtle deviation can lead to unhandled rejections, timing disorder, or memory leaks. Such defects are often hard to reproduce but have a profound impact on the stability of web applications.
Microtask Queue is key to understanding Promise timing behavior. The JavaScript runtime maintains a microtask queue, into which every Promise's .then, .catch, .finally callbacks and queueMicrotask calls are enqueued. Unlike macrotasks (such as setTimeout and I/O callbacks), the microtask queue is completely drained after the current macrotask ends and before the next macrotask begins. This means that multiple consecutive .then in a Promise chain execute synchronously within the same event loop turn, without yielding control to rendering or other macrotasks. WebKit's JavaScriptCore engine must strictly follow the ECMAScript specification's abstract operations such as PerformPromiseThen and PromiseReactionJob to ensure correct enqueue order and execution timing of microtasks. Once the scheduling logic deviates, it can cause serious issues such as out-of-order callback execution, delayed UI rendering, or even infinite loops.
Engineering Challenges of Fixing WebKit Bugs
Bug Localization: From Symptoms to Root Cause
Although the original post didn't disclose specific bug details, based on common WebKit issue patterns, Promise-related bugs usually cluster in several areas: microtask scheduling order, thenable object resolution, and Promise passing across contexts (such as iframes). Fixing such bugs requires a deep understanding of the ECMAScript specification's abstract operation PromiseResolveThenableJob and a line-by-line review against WebKit's implementation.
PromiseResolveThenableJob is the core abstract operation in the ECMAScript specification for handling thenable objects (i.e., any object with a then method). When a Promise's resolve function receives a thenable, the specification requires the engine to asynchronously call the thenable's then method rather than executing it synchronously immediately. This design avoids the Zalgo effect—where the same API sometimes calls back synchronously and sometimes asynchronously, making it hard for callers to reason about execution order. When implementing this operation, WebKit needs to create a special microtask job and correctly handle edge cases such as the thenable's then method throwing an exception or multiple calls to resolve/reject. If the engine mistakenly treats a thenable as a plain value and fulfills directly, or synchronously unwraps nested thenables within the same microtask, it would break the guarantees of the Promise/A+ specification and produce subtle timing bugs. This is exactly a typical source of WebKit Promise defects.
Fix and Verification: Are the Tracks Solid?
The author "tests" Promise reliability through actual fixing actions. The cleverness of this approach lies in treating the bug fix itself as a means of verification. If the fixed Promise behavior conforms to the specification's expectations, it indicates that the tracks are solid at key nodes; otherwise, it may expose deeper design issues.
Specific practices of defensive programming in asynchronous logic include: setting timeout fallbacks for critical Promise chains (e.g., Promise.race([task(), timeout(5000)])) to ensure that even if the underlying Promise never settles due to engine defects, it won't cause a hang; registering global listeners for unhandled rejections (window.addEventListener('unhandledrejection', ...)) to catch errors missed due to microtask scheduling anomalies; and in cross-iframe or Worker communication, explicitly verifying whether incoming thenable objects behave compliantly, avoiding reliance on the engine's implicit handling. These measures are not distrust of the engine but rather adding observability and fault tolerance at asynchronous boundaries, especially suitable for web application scenarios with extremely high timing correctness requirements, such as finance and healthcare.
Implications for Developers
- Don't blindly trust the runtime: Even widely used engines like WebKit may have Promise implementation defects. In critical asynchronous logic, it's necessary to appropriately add defensive code (such as timeout fallbacks and error boundaries).
- Pay attention to microtask timing: Promise's .then callbacks are microtasks, and their execution timing differs from macrotasks (setTimeout, etc.). The process of fixing WebKit bugs reminds us that timing-sensitive logic requires extra care.
- Participate in open-source fixes: The author directly fixed the WebKit bug rather than merely reporting the issue; this deep involvement is worth learning from. For front-end developers, understanding the internal mechanisms of browser engines can significantly improve debugging capabilities.
Conclusion: The Train Tracks Metaphor Continues
The openness of the title "Promises are made of train tracks" aptly reflects the complexity of the Promise mechanism. It provides a reliable asynchronous abstraction while also being filled with tracks at the implementation level that require careful inspection by engineers. By fixing a WebKit bug, the author not only solved a specific problem but also sent a signal to the community: continuous scrutiny of foundational mechanisms is an essential path to building reliable web applications.
For developers interested in JavaScript engines, asynchronous programming, and browser low-level implementations, this post offers a small but concrete entry point. Although information is limited, its methodological value outweighs the conclusions themselves.
Related articles

Zero-Code Replication of a $10,000 3D Website with Claude Code: A Six-Tool Breakdown
Bilibili creator Yomi used Claude Code plus five free tools to build a 3D product landing page quoted at $10,000 overseas—zero code. Breakdown of the six-tool division of labor, image-to-3D workflow, and the core collaboration method: "give AI a reference, not 'make it look better.'"

Claude Code in Practice: Install, Connect Models, Calculate Tokens, and Ship a Full-Stack Project with a Single Document
Based on a Bilibili public course demo, this covers Claude Code installation, model connection, token cost calculation, and the practical path of shipping a full-stack project with a single document, plus comparisons with Copilot, Cursor, Trae, and other coding assistants.

Who Accepts AI-Written Code? A 25-Minute Deep Dive into Verification Loops and the New Code Review Paradigm
After AI writes lots of code, how should Code Review verify it? This article breaks down the Claude Code team's verification loop framework: surface-level end-to-end verification, setup/drive/check three-stage skills, hybrid end-to-end testing and reliability checklists, and how to tighten the loop when issues slip through.