-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worker: add option to track unmanaged file descriptors
Add a public option for Workers which adds tracking for raw file descriptors, as currently, those resources are not cleaned up, unlike e.g. `FileHandle`s. PR-URL: #34303 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
- Loading branch information
1 parent
0f6805d
commit 9e04070
Showing
5 changed files
with
89 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { Worker } = require('worker_threads'); | ||
const { once } = require('events'); | ||
const fs = require('fs'); | ||
|
||
// All the tests here are run sequentially, to avoid accidentally opening an fd | ||
// which another part of the test expects to be closed. | ||
|
||
const preamble = ` | ||
const fs = require("fs"); | ||
const { parentPort } = require('worker_threads'); | ||
const __filename = ${JSON.stringify(__filename)}; | ||
process.on('warning', (warning) => parentPort.postMessage({ warning })); | ||
`; | ||
|
||
(async () => { | ||
// Consistency check: Without trackUnmanagedFds, FDs are *not* closed. | ||
{ | ||
const w = new Worker(`${preamble} | ||
parentPort.postMessage(fs.openSync(__filename)); | ||
`, { eval: true, trackUnmanagedFds: false }); | ||
const [ [ fd ] ] = await Promise.all([once(w, 'message'), once(w, 'exit')]); | ||
assert(fd > 2); | ||
fs.fstatSync(fd); // Does not throw. | ||
fs.closeSync(fd); | ||
} | ||
|
||
// With trackUnmanagedFds, FDs are closed automatically. | ||
{ | ||
const w = new Worker(`${preamble} | ||
parentPort.postMessage(fs.openSync(__filename)); | ||
`, { eval: true, trackUnmanagedFds: true }); | ||
const [ [ fd ] ] = await Promise.all([once(w, 'message'), once(w, 'exit')]); | ||
assert(fd > 2); | ||
assert.throws(() => fs.fstatSync(fd), { code: 'EBADF' }); | ||
} | ||
|
||
// There is a warning when an fd is unexpectedly opened twice. | ||
{ | ||
const w = new Worker(`${preamble} | ||
parentPort.postMessage(fs.openSync(__filename)); | ||
parentPort.once('message', () => { | ||
const reopened = fs.openSync(__filename); | ||
fs.closeSync(reopened); | ||
}); | ||
`, { eval: true, trackUnmanagedFds: true }); | ||
const [ fd ] = await once(w, 'message'); | ||
fs.closeSync(fd); | ||
w.postMessage(''); | ||
const [ { warning } ] = await once(w, 'message'); | ||
assert.match(warning.message, | ||
/File descriptor \d+ opened in unmanaged mode twice/); | ||
} | ||
|
||
// There is a warning when an fd is unexpectedly closed. | ||
{ | ||
const w = new Worker(`${preamble} | ||
parentPort.once('message', (fd) => { | ||
fs.closeSync(fd); | ||
}); | ||
`, { eval: true, trackUnmanagedFds: true }); | ||
w.postMessage(fs.openSync(__filename)); | ||
const [ { warning } ] = await once(w, 'message'); | ||
assert.match(warning.message, | ||
/File descriptor \d+ closed but not opened in unmanaged mode/); | ||
} | ||
})().then(common.mustCall()); |