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

feat: add options to control descriptor management #94

Merged
merged 2 commits into from
Nov 1, 2016

Conversation

pabigot
Copy link
Contributor

@pabigot pabigot commented Oct 10, 2016

This is a proposed implementation supporting issue #88 and providing a solution to the problem identified in issue #91. This works on Linux. I'm unable to test on Windows, and am unsure whether it behaves the same as POSIX systems when a descriptor is kept open after the file is removed.

Let me know if you find this worthwhile, or would like changes made before merging.

Thanks in advance for considering this.

This commit adds two options to control how tmp manages the file
descriptor that results from creating the temporary file.

Fixes raszi#88
Fixes raszi#91

Signed-off-by: Peter A. Bigot <[email protected]>
@@ -220,6 +220,14 @@ function _createTmpFile(options, callback) {
fs.open(name, CREATE_FLAGS, opts.mode || FILE_MODE, function _fileCreated(err, fd) {
if (err) return cb(err);

if (opts.discardDescriptor) {
return fs.close(fd, function _invokeCallback(err) {
cb(null, name, undefined, _prepareTmpFileRemoveCallback(name, -1, opts));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the error from fs.close() gets lost and must be passed to cb().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd considered that and figured the chance of a close error at this point (where nobody else can see the file) is tiny, and besides the file was successfully created so the call should succeed.

An alternative perspective which is somewhat compelling is to return the error, but then we also must unlink the file. If that also errors, we have a problem because we can only return one error. In that case the unlink error should be returned instead of the close error, as it's more important for the programmer to know that an orphan file might be present than that a descriptor got leaked.

I've added a separate commit with this behavior so we can see what it looks like, but it can't be tested since I can't force a failure in the close operation without completely reworking tmp's unit test infrastructure.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you know about sinonjs? you could temporarily replace fs.close with a mock that would then call the callback with a fake error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do in fact know sinon, and I use it extensively in projects I maintain where I target 100% unit test coverage. I'm not going to add it to node-tmp, especially since its unit tests are already fragile (they leave a lot of garbage in /tmp after they're run, and are non-deterministic as to whether they can be safely run repeatedly).

@silkentrance
Copy link
Collaborator

nice. with this in place, the api could be extended for creating tmpWriteStream()s as someone suggested in #46. Thinking of it, what sense does it make to create a tmp readstream from a new and by that empty file 😀.

Addresses comment in pull request raszi#94

Signed-off-by: Peter A. Bigot <[email protected]>
@raszi
Copy link
Owner

raszi commented Nov 1, 2016

Sorry for the late answer, I love this change as well. Unfortunately the source code needs some refactoring but I don't have that much time for this project.

@raszi raszi merged commit 38f2145 into raszi:master Nov 1, 2016
@raszi raszi changed the title add options to control descriptor management feat: add options to control descriptor management Aug 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants