11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst domain = require('domain');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// Make sure that the domains stack is cleared after a top-level domain
71cb0ef41Sopenharmony_ci// error handler exited gracefully.
81cb0ef41Sopenharmony_ciconst d = domain.create();
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cid.on('error', common.mustCall(() => {
111cb0ef41Sopenharmony_ci  // Scheduling a callback with process.nextTick _could_ enter a _new_ domain,
121cb0ef41Sopenharmony_ci  // but domain's error handlers are called outside of their domain's context.
131cb0ef41Sopenharmony_ci  // So there should _no_ domain on the domains stack if the domains stack was
141cb0ef41Sopenharmony_ci  // cleared properly when the domain error handler was called.
151cb0ef41Sopenharmony_ci  process.nextTick(() => {
161cb0ef41Sopenharmony_ci    if (domain._stack.length !== 0) {
171cb0ef41Sopenharmony_ci      // Do not use assert to perform this test: this callback runs in a
181cb0ef41Sopenharmony_ci      // different callstack as the original process._fatalException that
191cb0ef41Sopenharmony_ci      // handled the original error, thus throwing here would trigger another
201cb0ef41Sopenharmony_ci      // call to process._fatalException, and so on recursively and
211cb0ef41Sopenharmony_ci      // indefinitely.
221cb0ef41Sopenharmony_ci      console.error('domains stack length should be 0, but instead is:',
231cb0ef41Sopenharmony_ci                    domain._stack.length);
241cb0ef41Sopenharmony_ci      process.exit(1);
251cb0ef41Sopenharmony_ci    }
261cb0ef41Sopenharmony_ci  });
271cb0ef41Sopenharmony_ci}));
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_cid.run(() => {
301cb0ef41Sopenharmony_ci  throw new Error('Error from domain');
311cb0ef41Sopenharmony_ci});
32