Skip to content

Windows WSAStartup() workaround in RPostgres-init.c never runs #636

Done

Description

@krlmlr

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:

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_lib where lib is the name of the DLL file with the extension removed. [...] when unloading the object, R looks for a routine named R_unload_lib". The DLL is RPostgres, so the names R looks up are R_init_RPostgres and R_unload_RPostgres.
  • R_init_RPostgres() exists, but cpp11 generates it in src/cpp11.cpp, and it only registers routines:

    RPostgres/src/cpp11.cpp

    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_' prints T R_init_RPostgres and nothing else.

Two further defects would surface once the function is actually called:

  • WSAStartup(MAKEWORD(1, 0), NULL) passes NULL for lpWSAData. Microsoft documents that WSAStartup() returns WSAEFAULT for an invalid lpWSAData pointer, so the call needs a WSADATA variable (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() calling library.dynam.unload(). That is harmless here, since skipping the final WSACleanup() 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 by cpp11::cpp_register()), rather than a hand-written R_init_RPostgres() that would clash with src/cpp11.cpp.
  • Pass a real WSADATA* to WSAStartup() 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

  1. added a commit that references this issue on Nov 29, 2026
    5d12c41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions