| Index: runtime/bin/main.cc
 | 
| diff --git a/runtime/bin/main.cc b/runtime/bin/main.cc
 | 
| index ee7b80d8efaee4622b4aa7b54bc4f5d73f5c93b6..16b5d3358e9a6d906dd2f3d458fb3266699f44df 100644
 | 
| --- a/runtime/bin/main.cc
 | 
| +++ b/runtime/bin/main.cc
 | 
| @@ -110,6 +110,7 @@ static const int kCompilationErrorExitCode = 254;
 | 
|  // Exit code indicating an unhandled error that is not a compilation error.
 | 
|  static const int kErrorExitCode = 255;
 | 
|  
 | 
| +extern bool do_vm_shutdown;  // Defined in bin/process.cc
 | 
|  static void ErrorExit(int exit_code, const char* format, ...) {
 | 
|    va_list arguments;
 | 
|    va_start(arguments, format);
 | 
| @@ -120,11 +121,19 @@ static void ErrorExit(int exit_code, const char* format, ...) {
 | 
|    Dart_ExitScope();
 | 
|    Dart_ShutdownIsolate();
 | 
|  
 | 
| -  Dart_Cleanup();
 | 
| +  // Terminate process exit-code handler.
 | 
| +  Process::TerminateExitCodeHandler();
 | 
|  
 | 
| -  DebuggerConnectionHandler::StopHandler();
 | 
| -  // TODO(zra): Stop the EventHandler once thread shutdown is enabled.
 | 
| -  // EventHandler::Stop();
 | 
| +  char* error = Dart_Cleanup();
 | 
| +  if (error != NULL) {
 | 
| +    Log::PrintErr("VM cleanup failed: %s\n", error);
 | 
| +    free(error);
 | 
| +  }
 | 
| +
 | 
| +  if (do_vm_shutdown) {
 | 
| +    DebuggerConnectionHandler::StopHandler();
 | 
| +    EventHandler::Stop();
 | 
| +  }
 | 
|    exit(exit_code);
 | 
|  }
 | 
|  
 | 
| @@ -430,30 +439,60 @@ static bool ProcessTraceLoadingOption(const char* arg,
 | 
|  }
 | 
|  
 | 
|  
 | 
| +
 | 
| +static bool ProcessShutdownOption(const char* arg,
 | 
| +                                  CommandLineOptions* vm_options) {
 | 
| +  ASSERT(arg != NULL);
 | 
| +  if (*arg == '\0') {
 | 
| +    do_vm_shutdown = true;
 | 
| +    vm_options->AddArgument("--shutdown");
 | 
| +    return true;
 | 
| +  }
 | 
| +
 | 
| +  if ((*arg != '=') && (*arg != ':')) {
 | 
| +    return false;
 | 
| +  }
 | 
| +
 | 
| +  if (strcmp(arg + 1, "true") == 0) {
 | 
| +    do_vm_shutdown = true;
 | 
| +    vm_options->AddArgument("--shutdown");
 | 
| +    return true;
 | 
| +  } else if (strcmp(arg + 1, "false") == 0) {
 | 
| +    do_vm_shutdown = false;
 | 
| +    vm_options->AddArgument("--no-shutdown");
 | 
| +    return true;
 | 
| +  }
 | 
| +
 | 
| +  return false;
 | 
| +}
 | 
| +
 | 
| +
 | 
|  static struct {
 | 
|    const char* option_name;
 | 
|    bool (*process)(const char* option, CommandLineOptions* vm_options);
 | 
|  } main_options[] = {
 | 
|    // Standard options shared with dart2js.
 | 
| -  { "--version", ProcessVersionOption },
 | 
| -  { "--help", ProcessHelpOption },
 | 
| +  { "-D", ProcessEnvironmentOption },
 | 
|    { "-h", ProcessHelpOption },
 | 
| -  { "--verbose", ProcessVerboseOption },
 | 
| -  { "-v", ProcessVerboseOption },
 | 
| -  { "--package-root=", ProcessPackageRootOption },
 | 
| +  { "--help", ProcessHelpOption },
 | 
|    { "--packages=", ProcessPackagesOption },
 | 
| -  { "-D", ProcessEnvironmentOption },
 | 
| +  { "--package-root=", ProcessPackageRootOption },
 | 
| +  { "-v", ProcessVerboseOption },
 | 
| +  { "--verbose", ProcessVerboseOption },
 | 
| +  { "--version", ProcessVersionOption },
 | 
| +
 | 
|    // VM specific options to the standalone dart program.
 | 
|    { "--break-at=", ProcessBreakpointOption },
 | 
|    { "--compile_all", ProcessCompileAllOption },
 | 
| -  { "--gen-precompiled-snapshot", ProcessGenPrecompiledSnapshotOption },
 | 
| -  { "--run-precompiled-snapshot", ProcessRunPrecompiledSnapshotOption },
 | 
|    { "--debug", ProcessDebugOption },
 | 
| -  { "--snapshot=", ProcessGenScriptSnapshotOption },
 | 
|    { "--enable-vm-service", ProcessEnableVmServiceOption },
 | 
| +  { "--gen-precompiled-snapshot", ProcessGenPrecompiledSnapshotOption },
 | 
|    { "--observe", ProcessObserveOption },
 | 
| +  { "--run-precompiled-snapshot", ProcessRunPrecompiledSnapshotOption },
 | 
| +  { "--shutdown", ProcessShutdownOption },
 | 
| +  { "--snapshot=", ProcessGenScriptSnapshotOption },
 | 
|    { "--trace-debug-protocol", ProcessTraceDebugProtocolOption },
 | 
| -  { "--trace-loading", ProcessTraceLoadingOption},
 | 
| +  { "--trace-loading", ProcessTraceLoadingOption },
 | 
|    { NULL, NULL }
 | 
|  };
 | 
|  
 | 
| @@ -660,6 +699,7 @@ static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri,
 | 
|                                 error);
 | 
|  
 | 
|    if (isolate == NULL) {
 | 
| +    delete isolate_data;
 | 
|      return NULL;
 | 
|    }
 | 
|  
 | 
| @@ -728,7 +768,8 @@ static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri,
 | 
|  
 | 
|    Platform::SetPackageRoot(package_root);
 | 
|  
 | 
| -  DartUtils::SetupIOLibrary(script_uri);
 | 
| +  result = DartUtils::SetupIOLibrary(script_uri);
 | 
| +  CHECK_RESULT(result);
 | 
|  
 | 
|    // Make the isolate runnable so that it is ready to handle messages.
 | 
|    Dart_ExitScope();
 | 
| @@ -1121,18 +1162,22 @@ void main(int argc, char** argv) {
 | 
|    }
 | 
|  
 | 
|    // Initialize the Dart VM.
 | 
| -  if (!Dart_Initialize(vm_isolate_snapshot_buffer, instructions_snapshot,
 | 
| -                       CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate,
 | 
| -                       DartUtils::OpenFile,
 | 
| -                       DartUtils::ReadFile,
 | 
| -                       DartUtils::WriteFile,
 | 
| -                       DartUtils::CloseFile,
 | 
| -                       DartUtils::EntropySource)) {
 | 
| -    fprintf(stderr, "%s", "VM initialization failed\n");
 | 
| +  char* error = Dart_Initialize(
 | 
| +      vm_isolate_snapshot_buffer, instructions_snapshot,
 | 
| +      CreateIsolateAndSetup, NULL, NULL, ShutdownIsolate,
 | 
| +      DartUtils::OpenFile,
 | 
| +      DartUtils::ReadFile,
 | 
| +      DartUtils::WriteFile,
 | 
| +      DartUtils::CloseFile,
 | 
| +      DartUtils::EntropySource);
 | 
| +  if (error != NULL) {
 | 
| +    if (do_vm_shutdown) {
 | 
| +      DebuggerConnectionHandler::StopHandler();
 | 
| +      EventHandler::Stop();
 | 
| +    }
 | 
| +    fprintf(stderr, "VM initialization failed: %s\n", error);
 | 
|      fflush(stderr);
 | 
| -    DebuggerConnectionHandler::StopHandler();
 | 
| -    // TODO(zra): Stop the EventHandler once thread shutdown is enabled.
 | 
| -    // EventHandler::Stop();
 | 
| +    free(error);
 | 
|      exit(kErrorExitCode);
 | 
|    }
 | 
|  
 | 
| @@ -1143,7 +1188,6 @@ void main(int argc, char** argv) {
 | 
|  
 | 
|    // Call CreateIsolateAndSetup which creates an isolate and loads up
 | 
|    // the specified application script.
 | 
| -  char* error = NULL;
 | 
|    int exit_code = 0;
 | 
|    char* isolate_name = BuildIsolateName(script_name, "main");
 | 
|    Dart_Isolate isolate = CreateIsolateAndSetupHelper(script_name,
 | 
| @@ -1156,10 +1200,18 @@ void main(int argc, char** argv) {
 | 
|    if (isolate == NULL) {
 | 
|      Log::PrintErr("%s\n", error);
 | 
|      free(error);
 | 
| +    error = NULL;
 | 
|      delete [] isolate_name;
 | 
| -    DebuggerConnectionHandler::StopHandler();
 | 
| -    // TODO(zra): Stop the EventHandler once thread shutdown is enabled.
 | 
| -    // EventHandler::Stop();
 | 
| +    Process::TerminateExitCodeHandler();
 | 
| +    error = Dart_Cleanup();
 | 
| +    if (error != NULL) {
 | 
| +      Log::PrintErr("VM cleanup failed: %s\n", error);
 | 
| +      free(error);
 | 
| +    }
 | 
| +    if (do_vm_shutdown) {
 | 
| +      DebuggerConnectionHandler::StopHandler();
 | 
| +      EventHandler::Stop();
 | 
| +    }
 | 
|      exit((exit_code != 0) ? exit_code : kErrorExitCode);
 | 
|    }
 | 
|    delete [] isolate_name;
 | 
| @@ -1286,11 +1338,15 @@ void main(int argc, char** argv) {
 | 
|    // Terminate process exit-code handler.
 | 
|    Process::TerminateExitCodeHandler();
 | 
|  
 | 
| -  Dart_Cleanup();
 | 
| -
 | 
| -  DebuggerConnectionHandler::StopHandler();
 | 
| -  // TODO(zra): Stop the EventHandler once thread shutdown is enabled.
 | 
| -  // EventHandler::Stop();
 | 
| +  error = Dart_Cleanup();
 | 
| +  if (error != NULL) {
 | 
| +    Log::PrintErr("VM cleanup failed: %s\n", error);
 | 
| +    free(error);
 | 
| +  }
 | 
| +  if (do_vm_shutdown) {
 | 
| +    DebuggerConnectionHandler::StopHandler();
 | 
| +    EventHandler::Stop();
 | 
| +  }
 | 
|  
 | 
|    // Free copied argument strings if converted.
 | 
|    if (argv_converted) {
 | 
| 
 |