Skip to content

Access violation writing location in ConnectionHandles::clear() due to racecondition #378

@gazsi150

Description

@gazsi150

Using msnodesqlv8 v5.1.1 with sqlserver2022

The following line sporadically crashes with error message Unhandled exception at 0x00007FF84E30D7CD (ntdll.dll) in <xxx.exe>.dmp: 0xC0000005: Access violation writing location 0x0000000000000010. because the handle was already freed

auto cancelResult = SQLCancel(handle->get_handle());

i suspect the following function has already cleared the statement form the collection

void ConnectionHandles::checkin(long statementId) {
SQL_LOG_DEBUG_STREAM("ConnectionHandles::checkin - statementId = " << statementId);
const auto itr = _statementHandles.find(statementId);
if (itr == _statementHandles.end()) {
SQL_LOG_ERROR_STREAM(
"ConnectionHandles::checkin - no handle found for statementId = " << statementId);
return;
}
// Free the handle through SafeHandle
itr->second->free();
_statementHandles.erase(statementId);
}

Because the calling function OdbcConnection::RemoveStatement() doesn't lock the _connectionMutex and OdbcConnection::Close() doesn't lock the _statementMutex

The client application runs a query using the following function chain

sql.open(connectionString, (err, conn) => {
  if (err) {
  ...
  } else {
  var pm = conn.procedureMgr();
  pm.setTimeout(timeout);
  pm.getProc(queryName, (err, procedure) => {
  if(err) {
    conn.close(() => {...});
   } else {
     var query = procedure.call(args);
     query.on('done', () => {
         conn.close(() => {...});
     });
     query.on('error', err => {
         conn.close(() => {...});
     });
     query.on('info', i => {...});
     query.on('meta', meta => {...});
     query.on('column', (index, data) => {...});
     query.on('row', index => {...});
     query.on('free', () => {...});
    }
  }
});

There wasn't any running parallel API calls.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions