Skip to content

React Root Index with Math.random() causes Invariant Violations with React compiled as V8 Snapshot #6192

@virgofx

Description

@virgofx

Using V8 as a backend with snapshots enabled. To optimize performance, it is beneficial to create snapshots to improve server side rendering performance. After creating snapshot with React.js + ReactServer.js, the resulting server side component rendering errors as follows:

Invariant Violation: Trying to release an instance into a pool of a different type.

The logic that is causing the error is the Math.random() function, which has issues with snapshots and V8. There are 5 areas where this function is used. At the moment, the biggest problem for server side rendering is the following location; however, the other 4 will also impact if code reached the Math.random() at runtime.

/**
 * Size of the reactRoot ID space. We generate random numbers for React root
 * IDs and if there's a collision the events and DOM update system will
 * get confused. In the future we need a way to generate GUIDs but for
 * now this will work on a smaller scale.
 */
var GLOBAL_MOUNT_POINT_MAX = Math.pow(2, 53);

var ServerReactRootIndex = {
  createReactRootIndex: function () {
    return Math.ceil(Math.random() * GLOBAL_MOUNT_POINT_MAX);
  }
};

Would it be possible to fix this such that server side heap snapshots can be generated without having to worry about issues with server Math.random()? Could some sort of algorithm that doesn't rely on Math.random() be used to generate the nodes, root indexes, etc.?

OS: Ubuntu 14.04 x64
V8: 5.0.104
React: 0.14.7

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