Skip to content

Leverage using/Symbol.dispose to ensure resources are cleaned up before Node.js exits #48687

Description

@brillout

What is the problem this feature will solve?

Today, there isn't a way to guarantee resource cleanup. For example:

export function doSomeWork() {
    const path = ".some_temp_file";
    const file = fs.openSync(path, "w+");

    try {
        // use file...

        if (someCondition()) {
            // do some more work...
            return;
        }
    }
    finally {
        // Close the file and delete it.
        fs.closeSync(file);
        fs.unlinkSync(path);
    }
}

If the process exits between fs.openSync() and the finally code block, then the file isn't removed.

What is the feature you are proposing to solve the problem?

While a 100% guarantee isn't possible, leveraging the new using keyword, there is an opportunity to dramatically increase the probability of successful resource cleanup.

Is that on Node.js's radar?

What alternatives have you considered?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions