Skip to content
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

error: "Leaks detected" using createWriteStream from 'node:fs' in tests #22626

Closed
77it opened this issue Feb 28, 2024 · 5 comments
Closed

error: "Leaks detected" using createWriteStream from 'node:fs' in tests #22626

77it opened this issue Feb 28, 2024 · 5 comments
Labels
invalid what appeared to be an issue with Deno wasn't

Comments

@77it
Copy link

77it commented Feb 28, 2024

Version: Deno 1.41.0 on Windows 11

Running the following test many times, sometimes (1 time every 10/20 runs) returns the following error:

error: Leaks detected:
  - An async call to op_fs_open_async was started in this test, but never completed. The operation was started here:
    at op_fs_open_async (ext:core/00_infra.js:265:13)
    at Object.open (ext:deno_fs/30_fs.js:633:21)
    at Object.open (ext:deno_node/_fs/_fs_open.ts:80:10)
    at WriteStream._construct (ext:deno_node/internal/fs/streams.mjs:63:17)
    at constructNT (ext:deno_node/_stream.mjs:1672:16)
    at Array.processTicksAndRejections (ext:deno_node/_next_tick.ts:30:15)
    at eventLoopTick (ext:core/01_core.js:150:29)
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';

import { test } from 'node:test';
import assert from 'node:assert';

/** @type {any} */ const t = (typeof Deno !== 'undefined') ? Deno.test : test;

const tempDir = os.tmpdir();
const filePath = path.join(tempDir, 'test.txt' + Date.now());
const data = Array.from({ length: 10 }, (_, i) => `Line àèéìòù ${i + 1}`);

t('createWriteStream (called 1 times): write txt to a file stream and read it back', async () => {
  const stream = fs.createWriteStream(filePath, { flags: 'w', encoding: 'utf8' });  // 'a' means append; 'w' means overwrite

  stream.on('finish', () => {
    const readData = fs.readFileSync(filePath, 'utf8');
    console.log(readData);

    const expectedData = data.join('\n') + '\n';
    assert.strictEqual(readData, expectedData, 'The read data was not correct');

    fs.unlinkSync(filePath);
  });

  stream.on('error', (err) => {
    console.error('An error occurred:', err);
  });

  // Write each line to file
  for (const line of data) {
    if (!stream.write(line + '\n')) {
      // Handle backpressure, waiting for the drain event before writing more data
      await new Promise((resolve) => stream.once('drain', resolve));
    }
  }

  stream.end();
});
@mmastrac
Copy link
Member

Dupe of #22473

Should be fixed by #22480

@mmastrac mmastrac reopened this Feb 28, 2024
@mmastrac
Copy link
Member

@dsherret Actually, looks like this fix should have been in 1.41.0. Any ideas?

@mmastrac
Copy link
Member

@77it

Looking closer, I see that you are using Deno.test when running on Deno. This means that the op and resource sanitizers are enabled, which means you might get occasional sanitizer failures.

You should be able to fix this by either 1) using node:test on Deno (which will disable the sanitizers for you) or 2) waiting for the stream close event with something like await new Promise(r => stream.on('close', r)).

@dsherret dsherret added the invalid what appeared to be an issue with Deno wasn't label Feb 28, 2024
@dsherret
Copy link
Member

@mmastrac that looks like the correct fix to me. Alternatively it looks like .end takes a callback for when it's done: https://nodejs.org/api/stream.html#writableendchunk-encoding-callback

I think we can close this one.

@77it
Copy link
Author

77it commented Feb 28, 2024

resolved adding await new Promise(r => stream.on('close', r)); at the end. thanks to all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid what appeared to be an issue with Deno wasn't
Projects
None yet
Development

No branches or pull requests

3 participants