mssql - async function or callback not finished

Post Reply
pcobee
Member
Posts: 21
Joined: Fri Apr 01, 2011 5:06 pm
Location: Greenville NC
Contact:

mssql - async function or callback not finished

Post by pcobee »

I'm getting this warning each time timerFired runs on an app I'm working on...

The script execution has ended while an async function or callback in the script might not have finished yet. Please check the script code for unresolved promises or missing await statements.

I've whittled the code down to the following bare minimum. The sp runs a "for xml" query and works perfectly except for the warning. I'm stumped.

Code: Select all

import * as sql from "mssql";

async function timerFired(s: Switch, flowElement: FlowElement) {

    // create db connection
    const sqlConfig:  sql.config = {
        user: "<uid>",
        password: "<pwd>",
        database: "<dbname>",
        server: "<sqlhost>",
        pool: {
            max: 10,
            min: 0,
            idleTimeoutMillis: 3000
        },
        options: {
            encrypt: false, // true for azure
            trustServerCertificate: true // change to true for local dev / self-signed certs
        }
    };

    let pool: sql.ConnectionPool = await sql.connect( sqlConfig );
    let request: sql.Request = await pool.request();
    let result: sql.IProcedureResult<any> = await request.execute( "GetXmlSP" );
    let resultXml: string = result.recordset[0]['XML_F52E2B61-18A1-11d1-B105-00805F49916B'];

    await flowElement.log( LogLevel.Info, resultXml );

    flowElement.setTimerInterval( 1 * 60 );

}
freddyp
Advanced member
Posts: 1023
Joined: Thu Feb 09, 2012 3:53 pm

Re: mssql - async function or callback not finished

Post by freddyp »

I do not see an obvious mistake. I have not yet worked with that package so check out the documentation: it is not because you add "await" to a statement that the function you are calling returns a Promise.

A side remark: calling a database is not guaranteed to work so you should place this in a try-catch construction.
pcobee
Member
Posts: 21
Joined: Fri Apr 01, 2011 5:06 pm
Location: Greenville NC
Contact:

Re: mssql - async function or callback not finished

Post by pcobee »

I patterned my code after this example...

https://tediousjs.github.io/node-mssql/#asyncawait

and if I run basically the same code from visual studio code it works fine.

And, yes, I would definately wrap code in a try-catch. As I mentioned, I whittled the code down to the bare minimum for demonstration purposes.

There's another package - Sequelize - that I'm going to give a try.
freddyp
Advanced member
Posts: 1023
Joined: Thu Feb 09, 2012 3:53 pm

Re: mssql - async function or callback not finished

Post by freddyp »

If you run a Node script from the command line Node exits only when all outstanding callbacks have been processed, or in more low-level terms, when the message queue of the event loop is empty.

When you run a Node script in Switch, however, that script is part of a larger one in Node process that keeps on running, so when your script finishes the message queue is not empty, but Switch knows if there are still messages in that queue that are from your script. As your script also uses code from packages it could theoretically be the case (I have never seen this myself) that there is still an outstanding callback that you are not aware of. As your code seems to work correctly, you could just ignore the warning message, or you could try and give your script a little bit more time so the callback can finalize. At the end of the script add:

Code: Select all

  await new Promise((r) => setTimeout(r, 1000));
This will wait for a second which will hopefully be enough. Let us know if it worked.
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: mssql - async function or callback not finished

Post by Padawan »

Is it possible you have to close the connection to the database?
https://tediousjs.github.io/node-mssql/#close

If that doesn't work, I've used this successfully in the past, but outside of Switch:
(I'm not sure if it will be useful in Switch)
https://github.com/mafintosh/why-is-node-running
pcobee
Member
Posts: 21
Joined: Fri Apr 01, 2011 5:06 pm
Location: Greenville NC
Contact:

Re: mssql - async function or callback not finished

Post by pcobee »

Adding wait - I tried up to 10 seconds - did not change result.

When I run code directly from VSC, there was a pause before script completed before adding the pool.close() statement. Once I added that, the script completed immediately in VSC. However, adding it to Switch script did not eilminate the warning.

I'm experimenting with why-is-node-running in VSC and hoping it will reveal something.
Post Reply