"This step was still trying to run code when the step ended error" when inserting documents into Mongo

This topic was automatically generated from Slack. You can find the original thread here.

Steve Juth : Hi Everyone! I’m brand new to Pipedream. For some reason I keep getting this error:

ACTIVE_HANDLEThis step was still trying to run code when the step ended. Make sure you promisify callback functions and await all Promises. (Reason: TLSSocket, Learn more: https://pipedream.com/docs/workflows/steps/code/async)

for this code:

const companies = steps.leadfeeder.$return_value

const MongoClient = require('mongodb').MongoClient
const {
  database,
  hostname,
  username,
  password,
} = auths.mongodb

const url = `mongodb+srv://${username}:${password}@${hostname}/test?retryWrites=true&w=majority`
const client = await MongoClient.connect(url, { 
  useNewUrlParser: true, 
  useUnifiedTopology: true 
})

const db = await client.db(database)
let savedCompanies = []

for(let i = 0; i < companies.length; i++) {
  let company = companies[i]
  
  try {
    await db.collection(params.collection).insertOne(company)
    savedCompanies.push(company)
  }
  catch(error) {
    console.log(`...skipped saving company ${company.attributes.name}`)
  }
}

await client.close()

return savedCompanies

I thought I had ‘await’ in the right lines but can’t see what is causing this issue. Any ideas?

Dylan Sather (Pipedream) : Welcome !

Are your documents being correctly inserted into Mongo?

Steve Juth : Hey ! For this test they are not, since they already exist (with a unique index constraint). So, for the 9 companies I’m testing with, 9 ‘skipped saving…’ messages get logged.

Dylan Sather (Pipedream) : Thanks. Would you mind trying to insert a new document and see if that works? I believe in this specific case, the message is a false positive. I’ll take a look at the Mongo code but I believe they probably fail to close the connection fully by the time the client.close() Promise is resolved.

Steve Juth : Also… I modified my code to findOne (w/ await), and then insertOne (w/ await) if nothing was found. Everything seems to function correctly except for the fact that I’m still seeing this message:

ACTIVE_HANDLEThis step was still trying to run code when the step ended. Make sure you promisify callback functions and await all Promises. (Reason: TLSSocket, Learn more: https://pipedream.com/docs/workflows/steps/code/async)

Steve Juth : My revised code (findOne + insertOne) did successfully insert a new document while showing this error still.

Steve Juth : Does this mean I can safely ignore this error message and continue with the next step in my workflow?

Dylan Sather (Pipedream) : That’s right. This is just a warning so you’re good to proceed. Some client libraries that don’t fully close all outstanding handles (sockets, files) can yield false positives. But if you’re awaiting all Promises and everything else works, you should be ok.

Steve Juth : Good to know. Thanks for the help!

Steve Juth : BTW, I’m really glad I found Pipedream! For quite some time I’ve been writing functions and deploying to Heroku, implementing some integrations with Zapier, and wishing Runkit was more private. I found you guys via a requestbin search in Google. :slightly_smiling_face:

Dylan Sather (Pipedream) : That’s great to hear! Really glad it’s working for you. Is there anything else you think we can improve?

Steve Juth : The only thing I thought of so far (and maybe this already exists but I just don’t know it) is the ability to (unit) test blocks of code - one at a time.

Dylan Sather (Pipedream) : That’s great feedback. Do you want to unit test Node functions, or whole steps of code that might make an API request to an external service?

Steve Juth : For me it would be whole steps of code, including API calls. Of course, a step-through debugger would be awesome, but I know such things aren’t simple to implement.

Steve Juth : Also, I would love to have the ability to easily (via one click) collapse all of a block’s assets (e.g., auth, params and code).

Dylan Sather (Pipedream) : Makes a lot of sense. We’re tracking the collapse all feature here if you’d like to follow. We’re also working on a new-and-improved version of the workflow builder that will change the way that some of this information is presented, and I believe it will help with that use case.

The new builder allows for the execution of individual steps, as well, which I think will help with the debugging use case. You won’t be able to attach true tests to a step (such that they would run automatically each time you deployed a new version of a workflow, for example), but we’re thinking a lot about what a testing framework might look like. I think proper mocking of destination APIs might be the hardest thing for us to solve.

Steve Juth : Sounds good! Thanks, Dylan.