Error while working with 'worker_threads'
Hello, currently I'm working on a experiment. The experiment is about 'worker_threads' module but I can't solve an error. When I'm trying to import 'msnodesqlv8' to a worker, the error occurs. Output logs and source codes are in the below:
Note: I made a structure to make every request from one connection
- db.js:
const sql = require('mssql/msnodesqlv8')
const poolPromise = new sql.ConnectionPool({
database: 'Northwind',
server: 'localhost',
driver: 'msnodesqlv8',
options: {
trustedConnection: true
}
})
.connect()
.then(pool => {
console.log("Connected to MSSQL")
return pool
})
.catch(err => console.log('Connection Error! ', err))
module.exports = {
sql, poolPromise
}
- bridge.js:
const { poolPromise, sql } = require("./db");
async function main() {
let connection = await poolPromise
this.connection = connection
}
main.makeRequest = async function (queryString) {
const result = await this.connection.request().query(queryString)
return result
}
main()
module.exports = main.makeRequest
- port.js:
const port = require('./bridge.js')
module.exports = port
- testParentPort.js:
const { Worker } = require('worker_threads')
const port = require("./port.js")
const runService = (WorkerData) => {
return new Promise((resolve, reject) => {
const worker = new Worker('./testWorker.js', {
workerData: WorkerData
});
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0)
reject(new Error(`stopped with ${code} exit code`));
})
})
}
const run = async () => {
const result = await runService('Test')
console.log(result);
}
run().catch(err => console.error(err))
- testWorker.js:
const { parentPort, workerData } = require('worker_threads')
const port = require("./port.js")
console.log(workerData)
parentPort.postMessage({ test: workerData })
Executed node .\testParentPort.js
- Logs:
$ node .\testParentPort.js
Connected to MSSQL
Error: Unable to initialize msnodesql
at Object.Module._extensions..node (node:internal/modules/cjs/loader:1187:18)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at C:\Users\botekrem\OneDrive\Desktop\threadingexample\node_modules\msnodesqlv8\lib\connection.js:10:21
at Object.<anonymous> (C:\Users\botekrem\OneDrive\Desktop\threadingexample\node_modules\msnodesqlv8\lib\connection.js:657:3)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
---------
Node.js version: v16.14.2
npm version: 7.5.6
OS: Windows 10
---------
I can import it at testParentPort.js but can't at testWorker.js, It's not crashing without importing at worker
What is my mistake? Are worker threads not supported by msnodesqlv8?
Thanks for help!
have you tried this v simple example as a start
https://github.com/TimelordUK/node-sqlserver-v8/blob/master/samples/javascript/thread-workers.js
worker receive task 32
worker receive task 34
worker receive task 36
worker receive task 38
{
"command": "task_result",
"data": "spid 60",
"num": 2,
"fib": 1
}
{
"command": "task_result",
"data": "spid 61",
"num": 0,
"fib": 0
}
{
"command": "task_result",
"data": "spid 65",
"num": 6,
"fib": 8
}
{
"command": "task_result",
"data": "spid 67",
"num": 4,
"fib": 3
}
{
"command": "task_result",
"data": "spid 71",
"num": 10,
"fib": 55
}
{
"command": "task_result",
"data": "spid 72",
"num": 12,
"fib": 144
}
{
"command": "task_result",
"data": "spid 70",
"num": 8,
"fib": 21
}
{
"command": "task_result",
"data": "spid 75",
"num": 14,
"fib": 377
}
I have the same problem, but only on the second worker instance created. Running alpine Linux.
The code above won’t work. It is not possible to share a connection over threads
you either can use pattern described here
https://github.com/TimelordUK/node-sqlserver-v8/issues/228
where master holds connection and workers post sql to a pool and results posted back or you can use the example where each thread has it own connection.
I'm getting a very similar error. In my case I am not using workers though - I am doing a require inside an Electron BrowserWindow instance. When it first loads there is no problem... I'm using it in conjunction with mssql and am able to make SQL queries just fine. However, if I refresh the window (without actually closing it) I get this "unable to initialize" error. If I close the window and re-open it, everything is fine again.
I recently migrated from Electron 6 to Electron 14 which also meant bumps in Node and Chromium. It worked fine on Electron 6 though. Is it possible there was a change in Node or Chromium that could be causing this?
One final note: The error I see is slightly different - the stack ends in /lib/connection.js at const cppDriver = require('../build/Release/sqlserverv8.node') which appears to be native code.
electron/js2c/asar_bundle.js:formatted:168 Uncaught (in promise) Error: Unable to initialize msnodesql
at process.func [as dlopen] (electron/js2c/asar_bundle.js:formatted:168)
at Object.Module._extensions..node (internal/modules/cjs/loader.js:1143)
at Object.func [as .node] (electron/js2c/asar_bundle.js:formatted:168)
at Module.load (internal/modules/cjs/loader.js:940)
at Module._load (internal/modules/cjs/loader.js:781)
at Function.f._load (electron/js2c/asar_bundle.js:formatted:803)
at Function.o._load (electron/js2c/renderer_init.js:29)
at Module.require (internal/modules/cjs/loader.js:964)
at require (internal/modules/cjs/helpers.js:88)
at VM382 C:\Users\Me\It\Thing\node_modules\msnodesqlv8\lib\connection.js:10