Repository navigation
Description
src/RPostgres-init.c implements the performance workaround from the libpq docs: call WSAStartup() once at load time so that closing the last connection on Windows does not tear down Winsock (WSACleanup() dropping the reference count to zero). The code is in functions named R_init_mypackage() and R_unload_mylib(), the placeholder names from Writing R Extensions, so R never calls them:
RPostgres/src/RPostgres-init.c
Lines 18 to 28 in 41cd4be
| void R_init_mypackage(DllInfo* info) { | |
| #ifdef _WIN32 | |
| WSAStartup(MAKEWORD(1, 0), NULL); | |
| #endif | |
| } | |
| void R_unload_mylib(DllInfo* info) { | |
| #ifdef _WIN32 | |
| WSACleanup(); | |
| #endif | |
| } |
The code has been dead since it was added in 4401084 (2015, as src/rpq-init.c); later commits only renamed the file and changed WIN32 to _WIN32.
Evidence
- Writing R Extensions, section 5.4 ("Registering native routines"): "On loading a DLL, R will look for a routine within that DLL named
R_init_libwherelibis the name of the DLL file with the extension removed. [...] when unloading the object, R looks for a routine namedR_unload_lib". The DLL isRPostgres, so the names R looks up areR_init_RPostgresandR_unload_RPostgres. R_init_RPostgres()exists, but cpp11 generates it insrc/cpp11.cpp, and it only registers routines:Lines 238 to 242 in 41cd4be
extern "C" attribute_visible void R_init_RPostgres(DllInfo* dll){ R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); R_useDynamicSymbols(dll, FALSE); R_forceSymbols(dll, TRUE); } - The CRAN binary of the released version 1.4.10 (Linux) exports only
R_init_RPostgres:nm -D --defined-only RPostgres.so | grep -E 'R_init_|R_unload_'printsT R_init_RPostgresand nothing else.
Two further defects would surface once the function is actually called:
WSAStartup(MAKEWORD(1, 0), NULL)passesNULLforlpWSAData. Microsoft documents thatWSAStartup()returnsWSAEFAULTfor an invalidlpWSADatapointer, so the call needs aWSADATAvariable (and probably version 2.2, which libpq itself requests).- Even a correctly named
R_unload_RPostgres()runs only when the DLL is unloaded, and the package has no.onUnload()callinglibrary.dynam.unload(). That is harmless here, since skipping the finalWSACleanup()at process exit costs nothing, but it means the unload half can simply be dropped.
No reprex: the effect is only visible on Windows, as a slower connect after the previous connection was closed. I found no existing issue about this (searched for WSAStartup, WSACleanup, R_init_mypackage, and Windows connection performance).
To do
- Hook the
WSAStartup()call into the generated init, e.g. via a[[cpp11::init]]function (supported bycpp11::cpp_register()), rather than a hand-writtenR_init_RPostgres()that would clash withsrc/cpp11.cpp. - Pass a real
WSADATA*toWSAStartup()and decide on the requested version. - Drop or rename
R_unload_mylib(). - Optionally, measure repeated
dbConnect()/dbDisconnect()on Windows before and after, to confirm the workaround is still worth having.
Severity: low (proposed, Kirill may change). Windows-only performance; connections work without it.
Activity
fix: Remove dead Windows `WSAStartup()` workaround (r-dbi#647)