@@ -973,25 +973,29 @@ ExitCode BuildSnapshotWithoutCodeCache( |
973 | 973 | } |
974 | 974 | }); |
975 | 975 |
|
| 976 | + Context::Scope context_scope(setup->context()); |
| 977 | + Environment* env = setup->env(); |
| 978 | + |
976 | 979 | // Run the custom main script for fully customized snapshots. |
977 | 980 | if (snapshot_type == SnapshotMetadata::Type::kFullyCustomized) { |
978 | | - Context::Scope context_scope(setup->context()); |
979 | | - Environment* env = setup->env(); |
980 | 981 | #if HAVE_INSPECTOR |
981 | 982 | env->InitializeInspector({}); |
982 | 983 | #endif |
983 | 984 | if (LoadEnvironment(env, builder_script_content.value()).IsEmpty()) { |
984 | 985 | return ExitCode::kGenericUserError; |
985 | 986 | } |
| 987 | + } |
986 | 988 |
|
987 | | - // FIXME(joyeecheung): right now running the loop in the snapshot |
988 | | - // builder might introduce inconsistencies in JS land that need to |
989 | | - // be synchronized again after snapshot restoration. |
990 | | - ExitCode exit_code = |
991 | | - SpinEventLoopInternal(env).FromMaybe(ExitCode::kGenericUserError); |
992 | | - if (exit_code != ExitCode::kNoFailure) { |
993 | | - return exit_code; |
994 | | - } |
| 989 | + // Drain the loop and platform tasks before creating a snapshot. This is |
| 990 | + // necessary to ensure that the no roots are held by the the platform |
| 991 | + // tasks, which may reference objects associated with a context. For |
| 992 | + // example, a WeakRef may schedule an per-isolate platform task as a GC |
| 993 | + // root, and referencing an object in a context, causing an assertion in |
| 994 | + // the snapshot creator. |
| 995 | + ExitCode exit_code = |
| 996 | + SpinEventLoopInternal(env).FromMaybe(ExitCode::kGenericUserError); |
| 997 | + if (exit_code != ExitCode::kNoFailure) { |
| 998 | + return exit_code; |
995 | 999 | } |
996 | 1000 | } |
997 | 1001 |
|
|