Repository navigation
Crash in mksnapshot when building with Electron 4 #19374
Description
When attempting to upgrade to Electron 4 in #19373, we fail the build with the following error when attempting to generate the startup snapshot. (I modified the build script slightly to pass stdio: "inherit" to mksnapshot for better error output)
Minifying startup script
Verifying if snapshot can be executed via `mksnapshot`
Generating startup blob with mksnapshot
Loading script for embedding: /Users/nathan/src/atom/out/startup.js
Error running mksnapshot.
Moving generated startup blob into "/Users/nathan/src/atom/out/Atom Dev.app/Contents/Frameworks/Electron Framework.framework/Resources/v8_context_snapshot.bin"
Error: ENOENT: no such file or directory, rename '/Users/nathan/src/atom/out/v8_context_snapshot.bin' -> '/Users/nathan/src/atom/out/Atom Dev.app/Contents/Frameworks/Electron Framework.framework/Resources/v8_context_snapshot.bin'
at Object.fs.renameSync (fs.js:766:18)
at electronLink.then (/Users/nathan/src/atom/script/lib/generate-startup-snapshot.js:137:10)
at <anonymous>:null:null
After hacking some logging into electron-mksnapshot, it appears that the mksnapshot is crashing with a segmentation fault. In the Node wrapper script, the exit code on the child process is null and the signal is SEGV.
Running mksnapshot directly as follows also produces a segfault, although electron-mksnaphsot seems to do some special magic to move files to a single working directory, so it's unclear whether this is even a good way to run mksnapshot.
Nathans-MacBook-Pro:atom nathan$ script/node_modules/electron-mksnapshot/bin/mksnapshot --startup_blob snapshot_blob.bin --turbo_instruction_scheduling /Users/nathan/src/atom/out/startup.js
Loading script for embedding: /Users/nathan/src/atom/out/startup.js
Segmentation fault: 11
Nathans-MacBook-Pro:atom nathan$ echo $?
139
Trying the latest electron-mksnapshot
I tried to generate the startup snapshot with [email protected] and got a bit further. I was able to generate snapshot_blob.bin, but the script failed when invoking v8_context_snapshot_generator. The output in this failure mode is pretty bad... the script just prints out the entire child process object, so I'm eliding some of its details here for readability.
Nathans-MacBook-Pro:atom nathan$ cat /Users/nathan/.nvm/versions/node/v8.15.1/lib/node_modules/electron-mksnapshot/package.json | grep "\"version\":"
"version": "6.0.0-beta.1"
Nathans-MacBook-Pro:atom nathan$ /Users/nathan/.nvm/versions/node/v8.15.1/lib/node_modules/electron-mksnapshot/mksnapshot.js /Users/nathan/src/atom/out/startup.js
Loading script for embedding: /Users/nathan/src/atom/out/startup.js
Error running the v8 context snapshot generator. { status: null,
signal: 'SIGTRAP',
output: [ null, null, null ],
pid: 94008,
stdout: null,
stderr: null,
envPairs: ELIDED
options: { cwd: '/Users/nathan/.nvm/versions/node/v8.15.1/lib/node_modules/electron-mksnapshot/bin',
env: ELIDED
args: [ '/var/folders/wy/zs1k37fn0d95lsfcv2d3z6m80000gn/T/mksnapshot-workdir119420-94006-vcutmj.h9pos/v8_context_snapshot_generator',
'--output_file=/Users/nathan/src/atom/v8_context_snapshot.bin' ],
file: '/var/folders/wy/zs1k37fn0d95lsfcv2d3z6m80000gn/T/mksnapshot-workdir119420-94006-vcutmj.h9pos/v8_context_snapshot_generator' }
I'm not sure where to go from here. To some degree, we're abusing V8 startup snapshots in Atom. They weren't designed for public consumption, and the snapshot that ships with Chrome is much smaller and probably way simpler than what we are attempting in Atom's startup.js script.
Activity
winstliu commentedon May 20, 2019
Yeah, I get exit code 3221225477 reported through Node's child_process, and -1073741819 from cmd when trying to run mksnapshot.
nathansobo commentedon May 20, 2019
I ran
mksnapshotunder LLDB in a temporary working directory as theelectron-mksnapshotscript does, but didn't come up with much. We may need a debug build:BinaryMuse commentedon May 20, 2019
/cc @jkleinsc for info
nathansobo commentedon May 20, 2019
As discussed with @BinaryMuse, as painful as this is, perhaps our best bet is to bisect the files we include in
startup.jsand try to narrow down the culprit(s).nathansobo commentedon May 21, 2019
@rafeca found that we're including Atom's
package.jsonin the startup script, which is a very large object. Apparently removing it allows the startup snapshot to build.rafeca commentedon May 22, 2019
This is the actual object that we're including: https://gist.github.com/rafeca/49379b5a0af04e542504b098b7ec3197
This object has a
_atomModuleCachekey with a ton of information:dependencies: An array with an object for each npm dependency. This object contains 3 keys:name: the npm package name.version: the resolved version for that package.path: the main file for that package.folders: An array with an object for each package found in Atom (a package is a folder with apackage.json). This object contains 2 keys:paths: an array with all the subfolders of that package.dependencies: an object with all the npm dependencies of that package (key is the npm package name and value is the semver range for that dependency).extensions: An object with all the project files indexed by extension (key is the extension and value is an array of files).Apart from this, in that huge object there's a
_atomPackageskey which contains every single bundled package metadata (including the_atomModuleCacheinformation for each bundled package).Why does
_atomModuleCacheexists?I still have to understand why this
_atomModuleCacheobject is created and how is it used (haven't been able to find any documentation that explains it, so I'm going to dive into the code).So far, by grepping I've fond these places where it's used in
atom/atom:src/module-cache.js: This is where this object is created and saved into thepackage.jsonfile (this was originally added in Cache requires across installed packages #3761).src/generate-module-cache.js: Looks just like a hack to modify thefolderobject ofatom/atom(this was added in Replace Grunt-based build system with simple scripts #12410, but that PR has >200 commits with no specific information about the module cache).script/lib/generate-metadata.js: This seems to be similar than the previous usage: it's just mutating the metadata to remove thepackage.jsonfile from the list of files withjsonextension (this was added in Replace Grunt-based build system with simple scripts #12410, but that PR has >200 commits with no specific information about the module cache).src/package.js: Here theextensionsobject is used to speed up finding all the native modules (files with.nodeextension) and require them to check if they are compatible with the current Atom version (this was added in Read _atomModuleCache to get all the native dependencies while initializing package #8826).So all of this originally comes from #3761, which aims to speed up the startup time by caching parts of the resolution of JS files (since Node.js resolution logic is so horribly complicated and slow).
I wonder though if this strategy is still impactful (or even if it makes more harm than good) taking into account that we're currently using v8 snapshots.
as-cii commentedon May 28, 2019
I think this may have something to do with the work I recently did on Electron 3 (see electron/electron#18420).
mksnapshotmay be segfaulting due to creating aPromiseduring Atom's preload phase. The use ofPromiseobjects should be avoided, as the Node runtime requires promises to contain an internal field (nodejs/node#13242), which makes them unsuitable for serialization. In the electron-3.1 branch, I worked around the problem by avoiding to create a Promise altogether (see 95fe952).I suspect that modifying
package.jsonto remove_atomPackagesis preventing packages from being preloaded and, as such, may be hiding the underlying promise creation issue illustrated above. Perhaps it would be worth cherry-picking 95fe952 into #19373 without further changes and checking whether it resolves the issue?as-cii commentedon May 28, 2019
For what it's worth, I just tested running Electron 4's and Electron 5's
mksnapshoton the file generated in the electron-3.1 branch with the modification suggested above, and it successfully generates a snapshot on my machine. So I think applying 95fe952 to #19373 will fix the issue.That fix is something that we will need in Electron 3.1 as well, so it may be sufficient to update from master once that branch is merged (we are currently waiting on a hotfix after electron/electron#18426 goes green).
rafeca commentedon May 28, 2019
Awesome @as-cii ! If 95fe952 is independant of the electron 3.1 upgrade, can we just create a PR with this commit and merge it into
master?lock commentedon Nov 26, 2019
This issue has been automatically locked since there has not been any recent activity after it was closed. If you can still reproduce this issue in Safe Mode then please open a new issue and fill out the entire issue template to ensure that we have enough information to address your issue. Thanks!