From 5961626faaf374b2d61a91e70857cc646622669a Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 9 Jan 2020 11:24:23 +0100 Subject: [PATCH] fs: add missing HandleScope to FileHandle.close Fixes: https://github.com/nodejs/node/issues/31202 PR-URL: https://github.com/nodejs/node/pull/31276 Reviewed-By: David Carlier Reviewed-By: Gireesh Punathil Reviewed-By: Colin Ihrig Reviewed-By: Richard Lau Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Rich Trott --- src/node_file.cc | 1 + test/parallel/test-filehandle-close.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 test/parallel/test-filehandle-close.js diff --git a/src/node_file.cc b/src/node_file.cc index 29d1eb23d9f20e..a87126887a1d86 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -301,6 +301,7 @@ MaybeLocal FileHandle::ClosePromise() { close->file_handle()->AfterClose(); Isolate* isolate = close->env()->isolate(); if (req->result < 0) { + HandleScope handle_scope(isolate); close->Reject(UVException(isolate, req->result, "close")); } else { close->Resolve(); diff --git a/test/parallel/test-filehandle-close.js b/test/parallel/test-filehandle-close.js new file mode 100644 index 00000000000000..457b06a486f90f --- /dev/null +++ b/test/parallel/test-filehandle-close.js @@ -0,0 +1,17 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); + +// Test that using FileHandle.close to close an already-closed fd fails +// with EBADF. + +(async function() { + const fh = await fs.promises.open(__filename); + fs.closeSync(fh.fd); + + assert.rejects(() => fh.close(), { + code: 'EBADF', + syscall: 'close' + }); +})().then(common.mustCall());