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

Closing a file descriptor on Windows might cause an assertion #151

Open
timwoj opened this issue Dec 9, 2022 · 1 comment
Open

Closing a file descriptor on Windows might cause an assertion #151

timwoj opened this issue Dec 9, 2022 · 1 comment

Comments

@timwoj
Copy link
Contributor

timwoj commented Dec 9, 2022

I'm working on some stuff in Windows and I'm running into a assertion where we call close on the file descriptor returned from kqueue(). The problem is that if you call close on a file descriptor twice in Windows in a debug build, you get the following:

2022-12-09 13_21_14-Microsoft Visual C++ Runtime Library

I think the issue is that the Windows code in libkqueue always returns a zero from windows_kqueue_init without any backing to an actual file or handle, which seems a bit fraught with danger. There's no guarantee that some other code somewhere isn't already using that file descriptor, and if you have already closed it somewhere else you end up with the assertion above.

The _open_osfhandle function can convert a HANDLE into a file descriptor but I'm not sure of the ramifications of that. The docs say that it transfers ownership of the handle to the descriptor, but that might alright as long as the handle remains valid. Otherwise windows_kevent_wait wouldn't be able to use the handle to check for completion status. Is windows_kqueue_free called from anywhere? It probably wouldn't be possible to call CloseHandle anymore.

@timwoj
Copy link
Contributor Author

timwoj commented Dec 9, 2022

The _open_osfhandle function can convert a HANDLE into a file descriptor but I'm not sure of the ramifications of that. The docs say that it transfers ownership of the handle to the descriptor, but that might alright as long as the handle remains valid. Otherwise windows_kevent_wait wouldn't be able to use the handle to check for completion status. Is windows_kqueue_free called from anywhere? It probably wouldn't be possible to call CloseHandle anymore.

I went ahead and tried to implement this without any luck sadly. I'm not entirely sure open_osfhandle is compatible with the IO Completion Port handle that's in use here, or if it's something else I'm missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant