Skip to content

Cannot construct new Buffer in worker thread after transferring buffer out #32752

@daguej

Description

@daguej

After transferring a Buffer instance to a different thread, it becomes impossible to construct new Buffers in the current thread.

  • Version: v12.16.2
  • Platform: win10 x64
  • Subsystem: buffer, worker_threads

What steps will reproduce the bug?

var worker_threads = require('worker_threads');

if (worker_threads.isMainThread) {
	var worker = new worker_threads.Worker(__filename);

	worker.on('message', function(msg) {
		console.log(msg);
	});
	
} else {
	
	try {
		sendBuf(Buffer.from('hello world'));
		sendBuf(Buffer.from('hello world'));
	} catch (ex) {
		console.error(ex);
	}


}

function sendBuf(buf) {
	worker_threads.parentPort.postMessage({
		buf: buf
	}, [ buf.buffer ]);
}

What is the expected behavior?

It should not crash node

What do you see instead?

{
  buf: Uint8Array(11) [
    104, 101, 108, 108,
    111,  32, 119, 111,
    114, 108, 100
  ]
}
TypeError: Cannot perform Construct on a neutered ArrayBuffer
    at new Uint8Array (<anonymous>)
    at new FastBuffer (internal/buffer.js:945:1)
    at fromStringFast (buffer.js:427:11)
    at fromString (buffer.js:452:10)
    at Function.from (buffer.js:302:12)
    at Object.<anonymous> (/worker-test.js:14:18)
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)

It looks like after the first Buffer is transferred out, the allocPool contains a now-neutered ArrayBuffer.

If I call createPool() before

let b = new FastBuffer(allocPool, poolOffset, length);

then the operation succeeds.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions