Skip to content

fsPromises.truncate doesn't close fd. #34189

Description

@sheepa

What steps will reproduce the bug?

fsPromises.truncate(path) will result in a warning a few seconds later: (node:1387179) Warning: Closing file descriptor 22 on garbage collection - Using the callback truncate await new Promise((res, rej) => { fs.truncate(file, (err, ret) => { if(err) rej(err); else res(ret) }) }) works fine without such warning.

How often does it reproduce? Is there a required condition?

Every time.

What is the expected behavior?

Not having this warning.

What do you see instead?

Additional information

Activity

  1. himself65 commented on Jul 4, 2020

    @himself65
    Member

    Could you show the code to reproduce?

  2. added
    fsIssues and PRs related to file-system APIs and the fs module.
    on Jul 4, 2020
  3. bnoordhuis commented on Jul 7, 2020

    @bnoordhuis
    Member

    I'm not able to reproduce. As an example:

    $ node --expose_gc
    > fs.promises.truncate('/tmp/x.txt').then(_ => gc())  // x.txt exists
    Promise { <pending> }
    > 
    

    No warnings.

    @sheepa I'm going to close this but I can reopen if you post steps to reproduce.

  4. sheepa commented on Jul 7, 2020

    @sheepa
    Author
    const fs = require('fs').promises
    
    async function test()
    {
      const file = '/tmp/x.txt'
      await fs.writeFile(file, 'data')
      await fs.truncate(file)
      gc()
    }
    
    test()
    

    The following code give me (node:268029) Warning: Closing file descriptor 19 on garbage collection

  5. reopened this on Jul 7, 2020
  6. jasnell commented on Jul 7, 2020

    @jasnell
    Member

    @bnoordhuis... closing this was premature.

    This is an issue that was fixed in master in eadc385 but it does not look like the fix was backported to 12.x yet. See... https://github.com/nodejs/node/blob/v12.x/lib/internal/fs/promises.js#L307-L309

    C:\Users\jasne\Projects\tmp>node --expose-gc
    Welcome to Node.js v12.18.0.
    Type ".help" for more information.
    > fs.promises.truncate('t.js').then(_ => gc())
    Promise { <pending> }
    > (node:23652) Warning: Closing file descriptor 4 on garbage collection
    
  7. added
    confirmed-bugIssues and PRs for confirmed bugs.
    and removed
    invalidIssues and PRs that are invalid.
    on Jul 7, 2020
  8. jasnell commented on Jul 7, 2020

    @jasnell
    Member

    @sheepa ... I'll be opening a PR to backport the fix to 12.x soon but it will take a while for that to land in a release. You can work around the issue for now by using the ftruncate() method instead using the exact same pattern used in the fix:

    async function myTruncate(path, len) {
      const fd = await open(path, 'r+');
      return ftruncate(fd, len).finally(fd.close.bind(fd));
    }
  9. richardlau commented on Jul 7, 2020

    @richardlau
    Member

    This is an issue that was fixed in master in eadc385 but it does not look like the fix was backported to 12.x yet. See... https://github.com/nodejs/node/blob/v12.x/lib/internal/fs/promises.js#L307-L309

    eadc385 references #28858 as the PR and that's marked semver-major so it will be ignored by our tooling (e.g. branch-diff) and processes when evaluating what could be backported.

  10. jasnell commented on Jul 7, 2020

    @jasnell
    Member

    @richardlau ... yeah, I spotted that. This one commit is not semver-major. I'm working on a backport PR now for 12.x but github is giving me the Unicorn of Sadness at the moment. Will open as soon as the site becomes responsive again

  11. jasnell commented on Jul 7, 2020

    @jasnell
    Member

    PR opened!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues and PRs for confirmed bugs.fsIssues and PRs related to file-system APIs and the fs module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions