Fix leak in fd (#3487)

* Fix file descriptor leak

* Skip unnecessary clone

* Don't break --hot

---------

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
This commit is contained in:
Jarred Sumner
2023-07-01 21:58:06 -07:00
committed by GitHub
parent 9fecb3dfb9
commit c7cc618376
2 changed files with 18 additions and 1 deletions

View File

@@ -2065,7 +2065,7 @@ pub const VirtualMachine = struct {
)) |mapping| {
var log = logger.Log.init(default_allocator);
var errorable: ErrorableResolvedSource = undefined;
var original_source = fetchWithoutOnLoadPlugins(this, this.global, bun.String.init(top.source_url), bun.String.empty, &log, &errorable, .print_source) catch return;
var original_source = fetchWithoutOnLoadPlugins(this, this.global, top.source_url, bun.String.empty, &log, &errorable, .print_source) catch return;
const code = original_source.source_code.toUTF8(bun.default_allocator);
defer code.deinit();

View File

@@ -982,6 +982,14 @@ pub const ModuleLoader = struct {
jsc_vm.bundler.options.macro_remap;
var fallback_source: logger.Source = undefined;
// Usually, we want to close the input file automatically.
//
// If we're re-using the file descriptor from the fs watcher
// Do not close it because that will break the kqueue-based watcher
//
var should_close_input_file_fd = fd == null;
var input_file_fd: StoredFileDescriptorType = 0;
var parse_options = Bundler.ParseOptions{
.allocator = allocator,
@@ -1002,6 +1010,13 @@ pub const ModuleLoader = struct {
jsc_vm.main_hash == hash and
strings.eqlLong(jsc_vm.main, path.text, false),
};
defer {
if (should_close_input_file_fd and input_file_fd != 0) {
_ = bun.JSC.Node.Syscall.close(input_file_fd);
input_file_fd = 0;
}
}
if (is_node_override) {
if (NodeFallbackModules.contentsFromPath(specifier)) |code| {
const fallback_path = Fs.Path.initWithNamespace(specifier, "node");
@@ -1019,6 +1034,7 @@ pub const ModuleLoader = struct {
if (jsc_vm.isWatcherEnabled()) {
if (input_file_fd != 0) {
if (jsc_vm.bun_watcher != null and !is_node_override and std.fs.path.isAbsolute(path.text) and !strings.contains(path.text, "node_modules")) {
should_close_input_file_fd = false;
jsc_vm.bun_watcher.?.addFile(
input_file_fd,
path.text,
@@ -1059,6 +1075,7 @@ pub const ModuleLoader = struct {
if (jsc_vm.isWatcherEnabled()) {
if (input_file_fd != 0) {
if (jsc_vm.bun_watcher != null and !is_node_override and std.fs.path.isAbsolute(path.text) and !strings.contains(path.text, "node_modules")) {
should_close_input_file_fd = false;
jsc_vm.bun_watcher.?.addFile(
input_file_fd,
path.text,