11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/45421
51cb0ef41Sopenharmony_ci//
61cb0ef41Sopenharmony_ci// Check that node will NOT call v8::Isolate::SetIdle() when exiting
71cb0ef41Sopenharmony_ci// due to an unhandled exception, otherwise the assertion(enabled in
81cb0ef41Sopenharmony_ci// debug build only) in the SetIdle(), which checks that the vm state
91cb0ef41Sopenharmony_ci// is either EXTERNAL or IDLE will fail.
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The root cause of this issue is that before PerIsolateMessageListener()
121cb0ef41Sopenharmony_ci// is invoked by v8, v8 preserves the JS vm state, although it should
131cb0ef41Sopenharmony_ci// switch to EXTERNEL. https://bugs.chromium.org/p/v8/issues/detail?id=13464
141cb0ef41Sopenharmony_ci//
151cb0ef41Sopenharmony_ci// Therefore, this commit can be considered as an workaround of the v8 bug,
161cb0ef41Sopenharmony_ci// but we also find it not useful to call SetIdle() when terminating.
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
191cb0ef41Sopenharmony_ci  const { Worker } = require('worker_threads');
201cb0ef41Sopenharmony_ci  new Worker('', { eval: true });
211cb0ef41Sopenharmony_ci  throw new Error('xxx');
221cb0ef41Sopenharmony_ci} else {
231cb0ef41Sopenharmony_ci  const assert = require('assert');
241cb0ef41Sopenharmony_ci  const { spawnSync } = require('child_process');
251cb0ef41Sopenharmony_ci  const result = spawnSync(process.execPath, [__filename, 'child']);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  const stderr = result.stderr.toString().trim();
281cb0ef41Sopenharmony_ci  // Expect error message to be preserved
291cb0ef41Sopenharmony_ci  assert.match(stderr, /xxx/);
301cb0ef41Sopenharmony_ci  // Expect no crash
311cb0ef41Sopenharmony_ci  assert(!common.nodeProcessAborted(result.status, result.signal), stderr);
321cb0ef41Sopenharmony_ci}
33