Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module is not Context-Aware. Creates module registration error with node js worker threads #14

Closed
Ethan-Arrowood opened this issue Nov 30, 2018 · 5 comments

Comments

@Ethan-Arrowood
Copy link

Ethan-Arrowood commented Nov 30, 2018

Update 12/2/18 -
This is context-aware issue with C++ addons. See this issue thread for more details and potential fixes: nodejs/node#21783

--

I'm trying to use this library with my multithreading (Node.js worker-threads). I am encountering this error message when I use mmap-io in both my main thread and worker thread. See the following code and error out in order to reproduce.

script.js

const fs = require('fs')
const mmap = require('mmap-io')
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads')
const { getRecord } = require('./helpers')

// Open File
const fdArgs = ['./data_test', 'r+']
const fd = fs.openSync(...fdArgs)
// Get Size
const size = fs.fstatSync(fd).size
// Define record size in bytes
const recordSize = 64
// Calculate number of records in file
const nRecords = parseInt((size + 1) / recordSize)
const worker = new Worker('./worker.js', {
  workerData: {
    size,
    fdArgs,
    swapItemPos: [3, 11],
    recordSize
  }
});
worker.on('message', res => {
  console.log(`Response from thread: ${res.threadId}`)
  const buffer = mmap.map(size, mmap.PROT_WRITE, mmap.MAP_SHARED, fd)
  console.log(`Record 3: ${getRecord(buffer, recordSize, 3).toString()}`)
  console.log(`Record 11: ${getRecord(buffer, recordSize, 11).toString()}`)
});
worker.on('error', err => {
  console.error(err)
});
worker.on('exit', (code) => {
  if (code !== 0)
    throw new Error(`Worker stopped with exit code ${code}`)
})

worker.js

const fs = require('fs')
const mmap = require('mmap-io')
const { parentPort, workerData, threadId } = require('worker_threads')
const { swapRecords } = require('./helpers')

const { size, fdArgs, swapItemPos, recordSize } = workerData
const fd = fs.openSync(...fdArgs)
const buffer = mmap.map(size, mmap.PROT_WRITE, mmap.MAP_SHARED, fd)
swapRecords(buffer, recordSize, swapItemPos[0], swapItemPos[1])
parentPort.postMessage({ threadId })

std.err (Ignore line numbers as I have removed some comments and extra console.log statements to simplify the code in this issue)

size: 2047
nRecords: 32
Error: Module did not self-register.
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:718:18)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/Users/ethanarrowood/Documents/Wentworth/Semesters/Fall2018/OperatingSystems/Project/node_modules/mmap-io/mmap-io.js:3:10)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
/Users/ethanarrowood/Documents/Wentworth/Semesters/Fall2018/OperatingSystems/Project/script.js:41
      throw new Error(`Worker stopped with exit code ${code}`)
      ^

Error: Worker stopped with exit code 1
    at Worker.worker.on (/Users/ethanarrowood/Documents/Wentworth/Semesters/Fall2018/OperatingSystems/Project/script.js:41:13)
    at Worker.emit (events.js:182:13)
    at Worker.[kOnExit] (internal/worker.js:285:10)
    at Worker.(anonymous function).onexit (internal/worker.js:241:51)

Once I remove mmap-io from my script.js the program runs as expect without errors (and the records are swapped in the file as intended by the logic in worker.js). Could this have something to do with the read/write access on the "./build/Release/mmap-io.node" file?

@Ethan-Arrowood
Copy link
Author

I've discovered this has to do with making the module context-aware. There is an open issue on nodejs detailing some other affected modules as well as some solutions.

nodejs/node#21783 (comment)

Some more documentation:
https://nodejs.org/docs/latest/api/addons.html#addons_context_aware_addons

I'm attempting a fix but have not found any luck thus far. My C++ skills are very novice so this stuff is going over my head and I'm really just trying to emulate what others have done to migrate their modules (like here: https://github.com/schroffl/node-lzo/pull/11/files)

@Ethan-Arrowood Ethan-Arrowood changed the title Error: Module did not self-register. with Node worker-threads Module is not Context-Aware. Creates module registration error with node js worker threads Dec 3, 2018
ozra added a commit that referenced this issue Dec 10, 2018
@ozra
Copy link
Owner

ozra commented Dec 10, 2018

@Ethan-Arrowood — I quickly looked at the linked commentary, and did some very minor edits, not sure if the macro use changes does anything at all for you, but please try it out.

@ozra
Copy link
Owner

ozra commented Feb 5, 2019

No follow up, so I'm closing. Re-open if the problem subsists.

@ozra ozra closed this as completed Feb 5, 2019
@pdesantis
Copy link

I'm receiving this deprecation message when using mmap-io in Electron:

Electron: Loading non-context-aware native module in renderer. This is deprecated

This is fixed in master thanks to this change daeff71

@ozra could you release a new version?

@stan-ros
Copy link

stan-ros commented Oct 8, 2020

@ozra New release version with daeff71 will be great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants