-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
fs.close twice different behavior on Windows 10 #3718
Comments
Tested with Node.js 5.0.0 |
What value does fd have on Windows? Libuv will silently ignore it if you try to close a file descriptor <= 2. |
Updated the test to log fd after each close, the value of fd is always 3.
|
/cc @nodejs/platform-windows - can someone confirm? The second fs.close() should fail with EBADF. |
I confirm this on Windows 7. |
Thanks. Next question: why does it happen? |
It happens because Edit: |
Libuv has no routines for mapping a CRT error to a libuv error code. Therefore it reads This works most of the time, but in the case of an invalid file descriptor there is no such thing as a 'win32 error code', since the file descriptor table is maintained entirely in user space by the CRT. That's why A simple solution would be to add a special case for On a side note, using invalid file descriptors is normally a reason for the CRT to immediately crash the application, like a segmentation fault would. Node explicitly turns off this behavior, which I consider a really questionable practice, and it makes me wonder whether there are adverse security implications when e.g. certain libraries don't expect this behavior to be overridden. |
Is it problematic to add a routine for mapping Win32 error codes to libuv error codes? |
That already exists: https://github.com/libuv/libuv/blob/v1.x/src/win/error.c#L66 What you mean is, "is it problematic to add a routine got mapping CRT error codes to libuv error codes?" |
A poor man's diagram illustrating how error codes are mapped on windows:
(1) |
Is |
It looks like it. ... except |
Per the MSDN docs, On a side note, I dislike artificially setting a Win32 error code, but that's the way it's done in other places. |
You're right; I just looked it up in the crt source code (not on github unfortunately) --
I agree. |
Fixed in libuv. Will be fixed here automatically when libuv is upgraded. |
Thanks a lot! |
Fixes: nodejs#3718 PR-URL: nodejs#4276 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
of the correct `EBADF` error code because of a bug in libuv: * nodejs/node#3718 * libuv/libuv#613
The following test case opens a file, and closes it twice expecting to get an Error.
On Linux/Mac the second close callback err3 has the following Error:
{ [Error: EBADF: bad file descriptor, close] errno: -9, code: 'EBADF', syscall: 'close' }
On Windows 10 the second callback err3 has
null
The text was updated successfully, but these errors were encountered: