Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

107
Visualizações
Node async operations on child_process are not ending

I have a little script that executes a childprocess using execFile. This child process is a node script too that runs async operations but it seems like that the async are never ending so the terminal and all the processes are on hold.

This is the main script that runs the execFile for the child process:

fs.readdir(directoryPath, function(err, files) {
if (err) console.log(`Error: ${err}`);

files.map((file) => {
    execFile(`node`, ["updater.js", "BMW", file], (error, stdout, stderr) => {
        if (error) {
            red(`error: ${error.message}`);
            return;
        }

        if (stderr) {
            red(`stderr: ${stderr}`);
            return;
        }

        console.log(stdout);
    });
 });
});

And this is the node script executed as child process:

const args = process.argv.slice(2);
const brand = args[0];
const model = args[1];

const data = readJSON(`./json-export/${brand}/${model}`);
const generations = data.generations;

const generationsDB = await getGenerationsByModelAndBrand(brand, model);
console.log(generationsDB);

generations.map((generation) => {
   const lastModification =
       generation.modifications.modification[
           generation.modifications.modification.length - 1
        ];

  console.log(lastModification);
});

All the code works if I comment the const generationsDB line and the next console.log. If not when execution hits to the async request the execution gets stucked there. Tested the getGenerationsByModelAndBrand on the main script and works with no issue.

The getGenerationsByModelAndBrand runs a query on database and returns a Promise.

This is the getGenerationsByModelAndBrand method code:

export const getGenerationsByModelAndBrand = (brand, model) => {
    return new Promise((resolve, reject) => {
        const sql = `DATABASE SELECT`;
        connection.query(sql, function(error, result) {
            if (error) return reject(error);
            return resolve(result);
        });
    });
};

connection comes from mysql.createConnection method from the mysql package.

I believe that the issue comes from the promise handling on the child process is like I'm missing something bu couldn't find what it is.

Edit: After researching I didn't found a solution or explanation for this issue therefore in the meantime I moved the getGenerationsByModelAndBrand to the parent script and pass the result as parameter.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda