Skip to content

Commit a4514bc

Browse files
committed
doc: add worker thread limitations to VFS documentation
Document that VFS instances are not shared across worker threads. Each worker has its own V8 isolate and module cache, so workers must create their own VFS instances. Includes workarounds: - Recreate VFS in each worker using workerData - Use RealFSProvider to mount same real directory Addresses @jasnell's review feedback about worker thread considerations.
1 parent d6db585 commit a4514bc

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

doc/api/vfs.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,46 @@ myVfs.symlinkSync('/etc/passwd', '/passwd-link');
713713
// myVfs.readFileSync('/passwd-link'); // Throws ENOENT
714714
```
715715

716+
## Worker threads
717+
718+
VFS instances are **not shared across worker threads**. Each worker thread has
719+
its own V8 isolate and module cache, which means:
720+
721+
* A VFS mounted in the main thread is not accessible from worker threads
722+
* Each worker thread must create and mount its own VFS instance
723+
* VFS data is not synchronized between threads - changes in one thread are not
724+
visible in another
725+
726+
If you need to share virtual file content with worker threads, you must either:
727+
728+
1. **Recreate the VFS in each worker** - Pass the data to workers via
729+
`workerData` and have each worker create its own VFS:
730+
731+
```cjs
732+
const { Worker, isMainThread, workerData } = require('node:worker_threads');
733+
const vfs = require('node:vfs');
734+
735+
if (isMainThread) {
736+
const fileData = { '/config.json': '{"key": "value"}' };
737+
new Worker(__filename, { workerData: fileData });
738+
} else {
739+
// Worker: recreate VFS from passed data
740+
const myVfs = vfs.create();
741+
for (const [path, content] of Object.entries(workerData)) {
742+
myVfs.writeFileSync(path, content);
743+
}
744+
myVfs.mount('/virtual');
745+
// Now the worker has its own copy of the VFS
746+
}
747+
```
748+
749+
2. **Use `RealFSProvider`** - If the data exists on the real file system, use
750+
`RealFSProvider` in each worker to mount the same directory.
751+
752+
This limitation exists because implementing cross-thread VFS access would
753+
require moving the implementation to C++ with shared memory management, which
754+
significantly increases complexity. This may be addressed in future versions.
755+
716756
## Security considerations
717757

718758
### Path shadowing

0 commit comments

Comments
 (0)