Skip to content

fix: Remove dead Windows WSAStartup() workaround - #647

Merged
krlmlr merged 2 commits into
mainfrom
claude/fix-wsastartup
Nov 29, 2026
Merged

krlmlr merged 2 commits into
mainfrom
claude/fix-wsastartup

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Closes #636.

This PR deletes src/RPostgres-init.c. The file defined R_init_mypackage() and R_unload_mylib(), which R never looks up (it looks for R_init_RPostgres() and R_unload_RPostgres()), so the WSAStartup() call it contains has never run since it was added in 2015. Rather than wiring it up via [[cpp11::init]], this PR removes it, because libpq made the workaround unnecessary long ago.

Evidence

The workaround follows an old paragraph of the libpq docs: libpq used to call WSAStartup() when a connection started and WSACleanup() when it was freed, so closing the last connection unloaded Winsock and the next connect paid to load it again. That describes libpq up to 9.4: REL9_4_STABLE fe-connect.c calls WSAStartup(MAKEWORD(1, 1), &wsaData) in makeEmptyPGconn() and WSACleanup() in freePGconn(), and the 9.4 libpq.sgml still has the paragraph.

Since the November 2020 minor releases (13.1, 12.5, 11.10, 10.15, 9.6.20, 9.5.24) and in every release from 14 on, libpq calls WSAStartup(MAKEWORD(2, 2), &wsaData) once per process, guarded by a static bool wsastartup_done, and never calls WSACleanup(). Earlier releases, such as 9.5.0, 12.0 and 13.0, still start and clean up Winsock per connection. The source comment says: "the Windows documentation says that we should eventually do a matching WSACleanup() call, but experience suggests that that is at least as likely to cause problems as fix them. So we don't." The current tips of the branches from REL9_5_STABLE through REL_18_STABLE and pqMakeEmptyPGconn() on master all have it, and so do the tags REL_13_2 and REL_15_12 that RPostgres bundles.

Every libpq that RPostgres bundles on Windows has the fix: the Rtools libpq, or the bundles that tools/winlibs.R downloads (libpq 15.12, or 13.2 for R < 4.2). So the Winsock reference count never returns to zero once the first connection is made, and the workaround has nothing left to prevent. Even if it were fixed, it would only add a second reference that libpq doesn't need. The other two defects from the issue, the NULL lpWSAData (which would give WSAEFAULT) and the missing unload hook, go away along with the file.

src/CMakeLists.txt still lists RPostgres-init.c. That file is generated by cynkrathis::use_cmakelists() for IDE use only, R's build doesn't read it, and it is already stale (it lists logging.cpp, which doesn't exist). I left it alone so this PR doesn't overlap with #637, which regenerates that file; whichever of the two merges second regenerates it again.

With the file gone, src/ has no C sources left, so R CMD check notes "PKG_CFLAGS set in 'src/Makevars' / 'src/Makevars.in' without any corresponding files", which fails the CI smoke test. The second commit therefore drops PKG_CFLAGS=$(C_VISIBILITY) from src/Makevars.in. src/Makevars.win sets no PKG_CFLAGS, and configure passes its flags through PKG_CPPFLAGS (@cflags@), so nothing else writes it.

This interacts with #635, which builds src/vendor/nanoarrow/nanoarrow.c through make's implicit rule and so relies on PKG_CFLAGS=$(C_VISIBILITY) for -fvisibility=hidden. The two branches merge without a textual conflict, but the merged tree compiles nanoarrow.c without -fvisibility=hidden, and RPostgres.so then exports 71 nanoarrow symbols, compared with 0 on #635 alone. R CMD check passes on the merged tree, since it doesn't count the C file under vendor/. Whichever of the two merges second needs a follow-up to restore the hidden visibility for that object, for example by passing $(C_VISIBILITY) in an explicit recipe for vendor/nanoarrow/nanoarrow.o.

Tests

  • Built on Linux (Ubuntu, R 4.5.3, PostgreSQL 16) from the committed tree. The shared object still exports only R_init_RPostgres, as before.
  • Full testthat suite against a local PostgreSQL 16 database: 9182 passed, 0 failed, 0 errors, 11 skipped (all skips are pre-existing: Arrow TODOs, skips requested by DBItest, and two time zone tests that need a second database).
  • R CMD build followed by _R_CHECK_FORCE_SUGGESTS_=false R CMD check --no-manual --no-tests: with the first commit alone, "checking use of PKG_*FLAGS in Makefiles ... NOTE" appears. With the second commit, that check is OK and the status is OK.
  • I couldn't build or run anything on Windows here. The Windows jobs in this PR's CI are the first Windows check of the change. Since the deleted code never ran, behavior on Windows should be unchanged. Someone building against their own libpq from before November 2020 through a custom pkg-config would see the old per-connection behavior, but that is also unchanged by this PR. A full-matrix run is at https://github.com/r-dbi/RPostgres/actions/runs/46260320672.

Coordinated in #631. Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g

https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g


Generated by Claude Code

`src/RPostgres-init.c` defined `R_init_mypackage()` and `R_unload_mylib()`,
names R never looks up, so the code has never run.
Its rationale is gone as well:
since PostgreSQL 9.5, libpq calls `WSAStartup()` once per process in `pqMakeEmptyPGconn()`
and never calls `WSACleanup()`,
so closing the last connection no longer tears down Winsock.
All libpq builds RPostgres links on Windows are far newer.

Closes #636.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: Delete the dead WSAStartup() workaround, or make it run?

The deleted src/RPostgres-init.c has never been called, because R doesn't look up its function names. Its purpose was to keep Winsock loaded after the last connection closes. Since PostgreSQL 9.5, libpq does that itself: it calls WSAStartup() once per process in pqMakeEmptyPGconn() and never calls WSACleanup() (details in the PR body). The other option is to make the call actually run, through a [[cpp11::init]] function in a .cpp file, with a real WSADATA, version 2.2, a regenerated src/cpp11.cpp, and the unload half dropped. That costs about 15 lines and one more piece of Windows-only code, all to add a second Winsock reference that libpq doesn't need. Measuring repeated dbConnect()/dbDisconnect() on Windows, as the issue suggests, would only show a difference if libpq still released Winsock, and it doesn't.

You decide. If nobody does, the PR stays as it is: a deletion that changes no behavior.

  • Delete the file, as this PR does (recommended)
  • Keep the workaround, and make it run via [[cpp11::init]] with a valid WSADATA
  • Also regenerate the stale src/CMakeLists.txt (which still lists RPostgres-init.c and the missing logging.cpp) in this PR, rather than separately

Generated by Claude Code

@krlmlr
krlmlr marked this pull request as ready for review November 29, 2026 12:05
@krlmlr krlmlr mentioned this pull request Nov 29, 2026
3 of 9 tasks
With `src/RPostgres-init.c` gone, `src/` has no C sources left,
and `R CMD check` notes that `PKG_CFLAGS` is set without any corresponding files.
`src/Makevars.win` sets no `PKG_CFLAGS`,
and `configure` passes its own flags through `PKG_CPPFLAGS`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g
@krlmlr
krlmlr merged commit 5d12c41 into main Nov 29, 2026
29 checks passed
@krlmlr
krlmlr deleted the claude/fix-wsastartup branch November 29, 2026 16:00
krlmlr pushed a commit that referenced this pull request Nov 29, 2026
The file was removed in #647.
The source list again matches `src/*.c`, `src/*.cpp`, and `src/*.h`,
and CMake configures.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g
krlmlr added a commit that referenced this pull request Nov 29, 2026
…akeLists.txt` (#656)

* build: Use vendored Boost with Rtools and drop `logging.cpp` from `CMakeLists.txt`

The Rtools branch of `src/Makevars.win` now passes `-Ivendor`, like the rwinlib branch.
It comes before the pkg-config flags, so the vendored Boost wins over the copy Rtools ships in its include directory.

`src/CMakeLists.txt` no longer lists `logging.cpp`, which was removed in #545.
CMake now configures again.

Closes #637.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g

* build: Drop `RPostgres-init.c` from `CMakeLists.txt`

The file was removed in #647.
The source list again matches `src/*.c`, `src/*.cpp`, and `src/*.h`,
and CMake configures.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g

---------

Co-authored-by: Claude <[email protected]>
krlmlr pushed a commit that referenced this pull request Nov 29, 2026
Resolves the conflict in `.Rbuildignore` by keeping the entries from both sides.
The explicit recipe for `nanoarrow.o` keeps its symbols hidden
now that `main` no longer sets `PKG_CFLAGS` (#647).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g
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

Successfully merging this pull request may close these issues.

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

2 participants