Chrome Node inspector causes code to execute differently?

48 views Asked by At

I'm running into a strange problem where some code takes different paths depending on whether Chrome devtools is open or not.. or at least it seems that way.

Example

Transaction is just a simple Mongoose Model. I'm using MongoDB Atlas.

import Transaction from '../models/Transaction'

export default async function sendMessage({ textMsg, recipient, event }) {
  
  debugger;
  // pause here to inspect DB

  try {
    const { transactionHash } = event
    // transactionHash is a string
    const existingTx = await Transaction.exists({ transactionHash })
   
    // don't want dupe transactions, check if hacker is trying `replay` an old transaction..
    if (existingTx) {
      console.log('This transaction has already happened')
      // tx was already sent, early return..
      return
    }

    const transactionRecord = new Transaction({textMsg, recipient})
    await transactionRecord.save()
    
   ... Else send the message..

This is a function that is called in a routeHandler.

But something weird happens when I run the Node/ExpressJS server with --inspect and open the Chrome Node inspector devtools: By the time the inspector pauses at line 1 (the debugger; statement) a Mongo record for Transaction (transactionHash) is already created. It doesn't even hit the line where transactionRecord.save() sits. Thus, existingTx always returns true and the function early returns.

This doesn't happen when Node debugger is closed

Running node with --inspect but not opening Chrome Node inspector devtools never early returns. The existingTx variables returns false, it doesn't early return, a mongo record is saved, and a message is sent (the final goal of the function).

Why does it do this? I've been racking my brains as to why this is. I was thinking it was a sourcemaps problem, but even a sourcemaps problem wouldn't cause the code to take different paths.

Has any ever run into something similar?

0

There are 0 answers