From 0164959d3441ccfbaaaff20eea2d3ec9fe852373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Sat, 11 Feb 2023 13:19:13 +0100 Subject: fix(ops): Always close cancel handles for read_async/write_async (#17736) Fixes https://github.com/denoland/deno/issues/17734 --- cli/tests/unit/read_file_test.ts | 11 +++++++++++ cli/tests/unit/read_text_file_test.ts | 17 ++++++++++++++--- cli/tests/unit/write_file_test.ts | 16 ++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) (limited to 'cli') diff --git a/cli/tests/unit/read_file_test.ts b/cli/tests/unit/read_file_test.ts index 0d25ea6d8..24cf6dccf 100644 --- a/cli/tests/unit/read_file_test.ts +++ b/cli/tests/unit/read_file_test.ts @@ -140,6 +140,17 @@ Deno.test( }, ); +// Test that AbortController's cancel handle is cleaned-up correctly, and do not leak resources. +Deno.test( + { permissions: { read: true } }, + async function readFileWithAbortSignalNotCalled() { + const ac = new AbortController(); + await Deno.readFile("cli/tests/testdata/assets/fixture.json", { + signal: ac.signal, + }); + }, +); + Deno.test( { permissions: { read: true }, ignore: Deno.build.os !== "linux" }, async function readFileProcFs() { diff --git a/cli/tests/unit/read_text_file_test.ts b/cli/tests/unit/read_text_file_test.ts index e78276dde..c40cb83e3 100644 --- a/cli/tests/unit/read_text_file_test.ts +++ b/cli/tests/unit/read_text_file_test.ts @@ -95,7 +95,7 @@ Deno.test( queueMicrotask(() => ac.abort()); const error = await assertRejects( async () => { - await Deno.readFile("cli/tests/testdata/assets/fixture.json", { + await Deno.readTextFile("cli/tests/testdata/assets/fixture.json", { signal: ac.signal, }); }, @@ -113,7 +113,7 @@ Deno.test( queueMicrotask(() => ac.abort(abortReason)); const error = await assertRejects( async () => { - await Deno.readFile("cli/tests/testdata/assets/fixture.json", { + await Deno.readTextFile("cli/tests/testdata/assets/fixture.json", { signal: ac.signal, }); }, @@ -128,7 +128,7 @@ Deno.test( const ac = new AbortController(); queueMicrotask(() => ac.abort("Some string")); try { - await Deno.readFile("cli/tests/testdata/assets/fixture.json", { + await Deno.readTextFile("cli/tests/testdata/assets/fixture.json", { signal: ac.signal, }); unreachable(); @@ -138,6 +138,17 @@ Deno.test( }, ); +// Test that AbortController's cancel handle is cleaned-up correctly, and do not leak resources. +Deno.test( + { permissions: { read: true } }, + async function readTextFileWithAbortSignalNotCalled() { + const ac = new AbortController(); + await Deno.readTextFile("cli/tests/testdata/assets/fixture.json", { + signal: ac.signal, + }); + }, +); + Deno.test( { permissions: { read: true }, ignore: Deno.build.os !== "linux" }, async function readTextFileProcFs() { diff --git a/cli/tests/unit/write_file_test.ts b/cli/tests/unit/write_file_test.ts index 78d0f5bad..5f1ffd7a6 100644 --- a/cli/tests/unit/write_file_test.ts +++ b/cli/tests/unit/write_file_test.ts @@ -379,6 +379,22 @@ Deno.test( }, ); +// Test that AbortController's cancel handle is cleaned-up correctly, and do not leak resources. +Deno.test( + { permissions: { read: true, write: true } }, + async function writeFileWithAbortSignalNotCalled() { + const ac = new AbortController(); + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + const filename = Deno.makeTempDirSync() + "/test.txt"; + await Deno.writeFile(filename, data, { signal: ac.signal }); + const dataRead = Deno.readFileSync(filename); + const dec = new TextDecoder("utf-8"); + const actual = dec.decode(dataRead); + assertEquals(actual, "Hello"); + }, +); + function assertNotExists(filename: string | URL) { if (pathExists(filename)) { throw new Error(`The file ${filename} exists.`); -- cgit v1.2.3