1

I have a program using libpq on windows, and I use _open_osfhandle(PQsocket(cnxn), 0) so that I can call select() on it.

My problem is, libpq is closing the underlying socket in PQfinish() so I get assert failures when calling _close() on the value returned by _open_osfhandle()

Is there a way to close a CRT file descriptor, without also closing the underlying handle?

EDIT

The reason I need this, is because after about 512 connections, _open_osfhandle() fails saying too many open files. Also, I tried _free_osfhnd (I found it in close.cpp when visual studio showed me the source of the assert) and it still failed.

  • 3
    Use `DuplicateHandle` to duplicate the handle returned by `PQsocket`, and pass the duplicate to `_open_osfhandle`. – Raymond Chen May 28 '16 at 18:55
  • @RaymondChen, Thank you. I have the code running, but it will take some time before we know for sure if it worked or not. I will let you know in a few minutes. –  May 28 '16 at 19:38
  • @RaymondChen, It works, thank you so much. Can you post your comment as an answer so I can accept it? –  May 28 '16 at 19:45
  • Go ahead and post the answer yourself, with whatever level of detail you feel appropriate, and then accept it. You have my permission. (I don't need imaginary internet points.) – Raymond Chen May 28 '16 at 23:06

1 Answers1

1

So, Raymond Chen's comment told me to try DuplicateHandle, and this worked.

Here is where I dupe it:

HANDLE h_dup_handle = 0;
SERROR_CHECK(DuplicateHandle(GetCurrentProcess(), PQsocket(A->cnxn),
             GetCurrentProcess(), &h_dup_handle, 0, TRUE,
             DUPLICATE_SAME_ACCESS), "DuplicateHandle failed!");
A->int_windows_pq_handle = _open_osfhandle(h_dup_handle, 0);
SERROR_CHECK(client->int_windows_pq_handle != -1, "_open_osfhandle failed!")

(SERROR_CHECK is a macro that goes to the error label if the condition is not met)

Here is where I close it:

PQfinish(client->cnxn);
_close(client->int_windows_pq_handle);