-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
[v18.x backport] fs: fix nonNativeWatcher leak of StatWatchers #46031
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,7 @@ | ||||||
'use strict'; | ||||||
|
||||||
const common = require('../common'); | ||||||
const { setTimeout } = require('timers/promises'); | ||||||
|
||||||
|
||||||
const assert = require('assert'); | ||||||
|
@@ -52,3 +53,59 @@ if (common.isOSX) { | |||||
process.on('exit', function() { | ||||||
assert(watcherClosed, 'watcher Object was not closed'); | ||||||
}); | ||||||
|
||||||
(async () => { | ||||||
// Watch a folder and update an already existing file in it. | ||||||
|
||||||
const rootDirectory = fs.mkdtempSync(testDir + path.sep); | ||||||
const testDirectory = path.join(rootDirectory, 'test-0'); | ||||||
fs.mkdirSync(testDirectory); | ||||||
|
||||||
const testFile = path.join(testDirectory, 'file-1.txt'); | ||||||
fs.writeFileSync(testFile, 'hello'); | ||||||
|
||||||
const watcher = fs.watch(testDirectory, { recursive: true }); | ||||||
let watcherClosed = false; | ||||||
watcher.on('change', common.mustCallAtLeast(function(event, filename) { | ||||||
// Libuv inconsistenly emits a rename event for the file we are watching | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
assert.ok(event === 'change' || event === 'rename'); | ||||||
|
||||||
if (filename === path.basename(testFile)) { | ||||||
watcher.close(); | ||||||
watcherClosed = true; | ||||||
} | ||||||
})); | ||||||
|
||||||
await setTimeout(common.platformTimeout(100)); | ||||||
fs.writeFileSync(testFile, 'hello'); | ||||||
|
||||||
process.once('exit', function() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also probably move this up a bit so that the handler is added before we start doing anything else like waiting for timers or writing to files. |
||||||
assert(watcherClosed, 'watcher Object was not closed'); | ||||||
}); | ||||||
})().then(common.mustCall()); | ||||||
|
||||||
(async () => { | ||||||
// Assert recursive watch does not leak handles | ||||||
const rootDirectory = fs.mkdtempSync(testDir + path.sep); | ||||||
const testDirectory = path.join(rootDirectory, 'test-7'); | ||||||
const filePath = path.join(testDirectory, 'only-file.txt'); | ||||||
fs.mkdirSync(testDirectory); | ||||||
|
||||||
let watcherClosed = false; | ||||||
const watcher = fs.watch(testDirectory, { recursive: true }); | ||||||
watcher.on('change', common.mustCallAtLeast(async (event, filename) => { | ||||||
await setTimeout(common.platformTimeout(100)); | ||||||
if (filename === path.basename(filePath)) { | ||||||
watcher.close(); | ||||||
watcherClosed = true; | ||||||
} | ||||||
await setTimeout(common.platformTimeout(100)); | ||||||
assert(!process._getActiveHandles().some((handle) => handle.constructor.name === 'StatWatcher')); | ||||||
})); | ||||||
|
||||||
process.on('exit', function() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
assert(watcherClosed, 'watcher Object was not closed'); | ||||||
}); | ||||||
await setTimeout(common.platformTimeout(100)); | ||||||
fs.writeFileSync(filePath, 'content'); | ||||||
})().then(common.mustCall()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.