Skip to content

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

@timwoj

Description

@timwoj

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions