crossnote icon indicating copy to clipboard operation
crossnote copied to clipboard

Doesn't exit correctly

Open leozzyzheng opened this issue 6 years ago • 11 comments

const mume = require("@shd101wyy/mume");

async function main() {
  await mume.init();

  const engine = new mume.MarkdownEngine({
    filePath: "./xxx.md",
    config: {
      previewTheme: "github-light.css",
      codeBlockTheme: 'github.css', //"auto.css",
      breakOnSingleNewLine: true,
      printBackground: true,
      enableScriptExecution: false, // <= for running code chunks
      mathRenderingOption: ''
    },
  });

  // html export
  await engine.htmlExport({ offline: true});
}

main();

It won't exit forever, I have tried to debug it and break it when the process need exit. The callstack is here:

async_hooks.js in nodejs

function emitHookFactory(symbol, name) {
  // Called from native. The asyncId stack handling is taken care of there
  // before this is called.
  // eslint-disable-next-line func-style
  const fn = function(asyncId) {
    active_hooks.call_depth += 1;
    // Use a single try/catch for all hook to avoid setting up one per
    // iteration.
    try {
      for (var i = 0; i < active_hooks.array.length; i++) {  // <------- BREAK HERE
        if (typeof active_hooks.array[i][symbol] === 'function') {
          active_hooks.array[i][symbol](asyncId);
        }
      }
    } catch (e) {
      fatalError(e);
    } finally {
      active_hooks.call_depth -= 1;
    }

    // Hooks can only be restored if there have been no recursive hook calls.
    // Also the active hooks do not need to be restored if enable()/disable()
    // weren't called during hook execution, in which case
    // active_hooks.tmp_array will be null.
    if (active_hooks.call_depth === 0 && active_hooks.tmp_array !== null) {
      restoreActiveHooks();
    }
  };

  // Set the name property of the anonymous function as it looks good in the
  // stack trace.
  Object.defineProperty(fn, 'name', {
    value: name
  });
  return fn;
}

It seems to do something with an asyncId which doesn't exist in AsyncHook's promiseIds array. I haven't used nodejs for long time so I can't tell where goes wrong.

leozzyzheng avatar May 23 '18 07:05 leozzyzheng

This one seems to work for me in one experiment:

async function renderFile(relativePathToFile) {
  const filePath = resolve(__dirname, relativePathToFile);
  console.log(`==== FILE ${relativePathToFile}`);
  console.log("initializing...");
  await mume.init();

  console.log("starting engine...");

  const engine = new mume.MarkdownEngine({
    filePath,
    config: {
      previewTheme: "github-light.css",
      enableScriptExecution: true,
      // revealjsTheme: "white.css"
      codeBlockTheme: "default.css",
      printBackground: true,
    },
  });

  console.log("exporting to html...");
  await engine.htmlExport({ offline: false, runAllCodeChunks: true });

  console.log("done.");
}

Does this code work for you?

kachkaev avatar May 23 '18 08:05 kachkaev

@kachkaev Unfortunately not, the output is

==== FILE xxx.md initializing... starting engine... exporting to html... done.

And it doesn't exit.

leozzyzheng avatar May 24 '18 11:05 leozzyzheng

I'm using window7 with node 9.11.1 and run it in vscode's terminal(use cmd).

leozzyzheng avatar May 24 '18 11:05 leozzyzheng

It may be caused by some exception in a promise that's hard to track. It'd be great if I could share a small git repo that reproduces the behaviour, that is including mume and a markdown file.

kachkaev avatar May 24 '18 11:05 kachkaev

@kachkaev I have just used the empty markdown file to do the test, the result is the same. The mume is installed by npm just yesterday. Maybe it is related to node's version?

leozzyzheng avatar May 24 '18 12:05 leozzyzheng

Not sure. This could be to do with Windows 7 though. Do you have an opportunity to try your code on the other os?

kachkaev avatar May 24 '18 15:05 kachkaev

@kachkaev No, I have another windows 10, I will try it later.

leozzyzheng avatar May 25 '18 01:05 leozzyzheng

Using pkg 4.3.1 to package script to exe file and run it on my windows 10. Same result, the process doesn't exit.

leozzyzheng avatar May 25 '18 05:05 leozzyzheng

Will this still happen if you use an empty markdown file?

shd101wyy avatar May 25 '18 15:05 shd101wyy

@shd101wyy Yes, used the same code provided by kachkaev with empty markdown file.

leozzyzheng avatar May 28 '18 02:05 leozzyzheng

Can reproduce. Using Windows 7 32bit here.

garyng avatar May 15 '19 06:05 garyng