Skip to content

Commit

Permalink
fixup! bootstrap: handle snapshot errors gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
joyeecheung committed Jul 13, 2022
1 parent e9765f3 commit 05fcdb8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
3 changes: 3 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -3822,6 +3822,9 @@ cases:
options were set, but the port number chosen was invalid or unavailable.
* `13` **Unfinished Top-Level Await**: `await` was used outside of a function
in the top-level code, but the passed `Promise` never resolved.
* `14` **Snapshot Failure**: Node.js was started to build a V8 startup
snapshot and it failed because certain requirements of the state of
the application were not met.
* `>128` **Signal Exits**: If Node.js receives a fatal signal such as
`SIGKILL` or `SIGHUP`, then its exit code will be `128` plus the
value of the signal code. This is a standard POSIX practice, since
Expand Down
21 changes: 12 additions & 9 deletions src/node_snapshotable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ void SnapshotBuilder::InitializeIsolateParams(const SnapshotData* data,
const_cast<v8::StartupData*>(&(data->v8_snapshot_blob_data));
}

constexpr int INTERNAL_ERROR = 12;
// TODO(joyeecheung): share these exit code constants across the code base.
constexpr int UNCAUGHT_EXCEPTION_ERROR = 1;
constexpr int BOOTSTRAP_ERROR = 10;
constexpr int SNAPSHOT_ERROR = 14;

int SnapshotBuilder::Generate(SnapshotData* out,
const std::vector<std::string> args,
Expand Down Expand Up @@ -237,12 +240,12 @@ int SnapshotBuilder::Generate(SnapshotData* out,
// without breaking compatibility.
Local<Context> base_context = NewContext(isolate);
if (base_context.IsEmpty()) {
return INTERNAL_ERROR;
return BOOTSTRAP_ERROR;
}

Local<Context> main_context = NewContext(isolate);
if (main_context.IsEmpty()) {
return INTERNAL_ERROR;
return BOOTSTRAP_ERROR;
}
// Initialize the main instance context.
{
Expand All @@ -259,7 +262,7 @@ int SnapshotBuilder::Generate(SnapshotData* out,

// Run scripts in lib/internal/bootstrap/
if (env->RunBootstrapping().IsEmpty()) {
return INTERNAL_ERROR;
return BOOTSTRAP_ERROR;
}
// If --build-snapshot is true, lib/internal/main/mksnapshot.js would be
// loaded via LoadEnvironment() to execute process.argv[1] as the entry
Expand All @@ -272,12 +275,12 @@ int SnapshotBuilder::Generate(SnapshotData* out,
env->InitializeInspector({});
#endif
if (LoadEnvironment(env, StartExecutionCallback{}).IsEmpty()) {
return 1;
return UNCAUGHT_EXCEPTION_ERROR;
}
// FIXME(joyeecheung): right now running the loop in the snapshot
// builder seems to introduces inconsistencies in JS land that need to
// be synchronized again after snapshot restoration.
int exit_code = SpinEventLoop(env).FromMaybe(1);
int exit_code = SpinEventLoop(env).FromMaybe(UNCAUGHT_EXCEPTION_ERROR);
if (exit_code != 0) {
return exit_code;
}
Expand All @@ -294,7 +297,7 @@ int SnapshotBuilder::Generate(SnapshotData* out,
#ifdef NODE_USE_NODE_CODE_CACHE
// Regenerate all the code cache.
if (!native_module::NativeModuleEnv::CompileAllModules(main_context)) {
return INTERNAL_ERROR;
return UNCAUGHT_EXCEPTION_ERROR;
}
native_module::NativeModuleEnv::CopyCodeCache(&(out->code_cache));
for (const auto& item : out->code_cache) {
Expand Down Expand Up @@ -325,7 +328,7 @@ int SnapshotBuilder::Generate(SnapshotData* out,
// We must be able to rehash the blob when we restore it or otherwise
// the hash seed would be fixed by V8, introducing a vulnerability.
if (!out->v8_snapshot_blob_data.CanBeRehashed()) {
return INTERNAL_ERROR;
return SNAPSHOT_ERROR;
}

// We cannot resurrect the handles from the snapshot, so make sure that
Expand All @@ -338,7 +341,7 @@ int SnapshotBuilder::Generate(SnapshotData* out,
PrintLibuvHandleInformation(env->event_loop(), stderr);
}
if (!queues_are_empty) {
return INTERNAL_ERROR;
return SNAPSHOT_ERROR;
}
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/snapshot/error.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
throw new Error('test');
throw new Error('test');

0 comments on commit 05fcdb8

Please sign in to comment.