From 5f336ffbb72db0edf30b39a870c55717626ba13e Mon Sep 17 00:00:00 2001 From: Charles Shin Date: Mon, 20 Nov 2023 11:26:45 -0800 Subject: [PATCH] fix: close file handle warning (#715) * fix: close fd warning * adding changeset * moving fs.close to finally block * mock .close for test * fix tests --------- Co-authored-by: Praveen Gupta --- .changeset/fresh-lobsters-cheer.md | 5 +++++ .../src/config/local_configuration_controller.test.ts | 7 ++++++- .../src/config/local_configuration_controller.ts | 9 ++++++--- 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 .changeset/fresh-lobsters-cheer.md diff --git a/.changeset/fresh-lobsters-cheer.md b/.changeset/fresh-lobsters-cheer.md new file mode 100644 index 0000000000..3355d6676c --- /dev/null +++ b/.changeset/fresh-lobsters-cheer.md @@ -0,0 +1,5 @@ +--- +'@aws-amplify/platform-core': patch +--- + +close file handle after reading config diff --git a/packages/platform-core/src/config/local_configuration_controller.test.ts b/packages/platform-core/src/config/local_configuration_controller.test.ts index 3397149971..19644a41ff 100644 --- a/packages/platform-core/src/config/local_configuration_controller.test.ts +++ b/packages/platform-core/src/config/local_configuration_controller.test.ts @@ -7,16 +7,20 @@ void describe('config controller', () => { const mockedFsReadFile = mock.method(fs, 'readFile'); const mockedFsWriteFile = mock.method(fs, 'writeFile'); const mockedFsOpen = mock.method(fs, 'open'); + const mockedFdClose = mock.fn(); beforeEach(() => { mockedFsReadFile.mock.resetCalls(); mockedFsWriteFile.mock.resetCalls(); mockedFsOpen.mock.resetCalls(); + mockedFdClose.mock.resetCalls(); }); void it('if config has not been cached, read from fs', async () => { mockedFsOpen.mock.mockImplementationOnce(() => { - return Promise.resolve(); + return Promise.resolve({ + close: mockedFdClose, + }); }); mockedFsReadFile.mock.mockImplementationOnce(function () { return Promise.resolve('{"hello": 123}'); @@ -26,6 +30,7 @@ void describe('config controller', () => { assert.strictEqual(resolvedValue, undefined); assert.strictEqual(mockedFsOpen.mock.callCount(), 1); assert.strictEqual(mockedFsReadFile.mock.callCount(), 1); + assert.strictEqual(mockedFdClose.mock.callCount(), 1); }); void it('should not throw & return undefined with path points to undefined nested object ', async () => { diff --git a/packages/platform-core/src/config/local_configuration_controller.ts b/packages/platform-core/src/config/local_configuration_controller.ts index 4991819cc7..df2782660f 100644 --- a/packages/platform-core/src/config/local_configuration_controller.ts +++ b/packages/platform-core/src/config/local_configuration_controller.ts @@ -30,19 +30,22 @@ export class LocalConfigurationController implements ConfigurationController { if (this._store) { return this._store; } + // check if file exists & readable. + let fd; + try { - // check if file exists & readable. - const fd = await fs.open( + fd = await fs.open( this.configFilePath, fs.constants.F_OK, fs.constants.O_RDWR ); - const fileContent = await fs.readFile(fd, 'utf-8'); this._store = JSON.parse(fileContent); } catch { this._store = {}; await this.write(); + } finally { + await fd?.close(); } return this._store; }