Repository navigation
Description
I discovered an odd issue, which I thought I'd report and can help troubleshoot if needed. The tl;dr is that I cannot connect to one Redshift server with the Mac binary from CRAN, but can if I compile from source.
The current Mac binary of RPostgres (1.4.5) seems to have an issue with some, but not all, Redshift servers. The command I'm running is
DBI::dbConnect(RPostgres::Redshift(), ...) # same result with RPostgres::Postgres()
and the error I get is:
connection to server at "<host>" (<ip>), port 5439 failed: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The error message is the same also when I don't provide a valid username/password, so it seems to happen before authentication.
The weird thing is that I get this error with only one Redshift server (our production one) and not with another (our staging) – even though they're both running the same version. Also, I can connect to both with psql from my Mac, and can connect to both with RPostgres from within a docker container running on my Mac. This made me suspect it is somehow related to the Mac version of RPostgres. And in fact, if instead of installing the binary from CRAN I compile it from source with (I have libpq installed with Homebrew):
install.packages("RPostgres", type = "source")
The problem goes away and I can connect to both servers.
I don't know if the solution is to simply recompile the Mac binary version that is available on CRAN, and that will fix it. If not, I can help troubleshoot on my end, in case that helps. The fact that this seems to affect some, but not all, Redshift servers can understandably make this hard the reproduce. Is there something I can do to see debugging messages from RPostgres/DBI?
My sessionInfo():
R version 4.2.3 (2023-03-15)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur ... 10.16
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] bit_4.0.5 compiler_4.2.3 cli_3.6.1 hms_1.1.3
[5] DBI_1.1.3 Rcpp_1.0.11 bit64_4.0.5 vctrs_0.6.3
[9] blob_1.2.4 lifecycle_1.0.3 pkgconfig_2.0.3 rlang_1.1.1
[13] RPostgres_1.4.5
Activity
krlmlr commentedon Sep 1, 2023
Thanks. Logging must be enabled when compiling, this is not an option here. This is really weird.
We could try harder to build the package so that fails to connect to Redshift the same way the CRAN build fails. What do you see with the following?
Created on 2023-09-01 with reprex v2.0.2
I'm a bit lost here. Logging the network communication and looking for differences? But this looks like serious overkill.
@s-u: Any idea how to troubleshoot libpq issues that happen (only) with the CRAN build?
s-u commentedon Sep 2, 2023
You can check with the same binary from https://mac.r-project.org/bin/. If this is a bug in
libpqthen you can suggest an update to https://github.com/R-macos/recipes/blob/master/recipes/libpqilarischeinin commentedon Sep 4, 2023
I get the same versions as you @krlmlr :
Created on 2023-09-04 with reprex v2.0.2
It doesn't seem to have
RPostgresavailable?Created on 2023-09-04 with reprex v2.0.2
Happy to, if that's what I should do. But to be honest I have no idea where the problem is. From the recipe you linked:
This makes me think that I do have both
libpqandpopstgresqlinstalled with Homebrew, and I wonder if it could somehow be possible that in some scenario (our production Redshift) it used one, and in another scenario (our staging Redshift) the other. FWIW, here's output forbrew info:ilarischeinin commentedon Sep 5, 2023
I did some more testing with
libpqandpostgresql, and found out that:RPostgresfrom source, it usespostgresql-> workslibpq, it uses that -> workspostgresql, It uses that -> worksRPostgresfrom source naturally failsHere are the compilation outputs, in case they might be useful:
krlmlr commentedon Apr 1, 2024
Is this still a problem with the most recent CRAN binary?
krlmlr commentedon Apr 1, 2024
A duplicate of #404 perhaps?
ilarischeinin commentedon Apr 2, 2024
It is.
I'm currently on macOS 14.4.1 and R 4.3.3, and it remains to be the case that the binary version (now 1.4.6) of RPostgres installed from CRAN does not work with our Redshift. When I compile RPostgres from source, it does work. (I have postgresql@14 version 14.11 installed via Homebrew, which is used when compiling.)
I'm on an Intel Mac, whereas the linked issue seemed to be specific to Apple Silicon, so I think this is something different.
While the problem is easy enough to fix for me by compiling form source, I recently had a colleague run into the same issue, and they were unable to compile from source on an Apple Silicon Mac. I didn't look into that in more detail yet, as I will be upgrading from Intel later this month. I will then see if I can get to a similar fix on Apple Silicon too, but of course it would be ideal if the CRAN binary worked out of the box.
One thing that has changed since originally reporting this is that it now affects both our Redshift instances (staging and production). So something (like an update, cert change, …) must have happened on the AWS side to the staging instance, as that used to work.
Something perhaps worth repeating is that the error happens before authentication, as the error message is the same regardless of whether I'm using valid username/password or not. (And is also different from the one I get with invalid credentials when using the compiled-from-source version of the package.)
krlmlr commentedon Apr 13, 2024
I now understood Simon's comment.
Can you please try compiling with libpq from https://mac.r-project.org/bin/darwin20/x86_64/ (or whatever subdirectory is appropriate for your platform)? If we need a newer/different libpq, we could send a pull request to https://github.com/R-macos/recipes/blob/master/recipes/libpq .
ilarischeinin commentedon Apr 16, 2024
OK, I:
libpqandpostgresqlinstallations I had viabrew/optRPostgresfrom source withinstall.packages("RPostgres", type="source")And as a result I am running into the issue described above. So I take it the hunch is correct there that
libpqversion used needs to be bumped from the current14.5.One thing I am confused by is that the compilation output mentions "autobrew bundle version 15.2":
But the entire output doesn't make it clear to me if it's actually using the 14.5 I installed manually, or the 15.2 it might download as part of the compilation. There are lines that point to directories where I put the manual installation, e.g.
So my questions:
brewand source? (And if 16.2, should something be done to also get the autobrew from 15.2 to 16.2?)krlmlr commentedon Apr 18, 2024
I believe the autobrew source is here: https://github.com/autobrew/scripts/blob/master/libpq . In some cases (High Sierra?), we still seem to be using 14.5 here. The autobrew mechanism can be disabled by setting the
DISABLE_AUTOBREWenvironment variable.The library used should be reported with
DBI::dbGetInfo(RPostgres::Postgres()). What are you seeing? I understand it doesn't work with 14.5, does it work for you with 15.2? I understand it works for you with 16.2, can you confirm?@jeroen: Can/should we bump to 16.2 here? Would that work for the High Sierra case?
jeroen commentedon Apr 18, 2024
High-sierra is no longer in use on CRAN or GHA or anywhere else so we don't need to worry about that.
I have updated the script to libpq 16.0 (for which big-sur binaries were already available): autobrew/scripts@ea29fea.
However, the problem is that the CRAN macbuilders now have another version of libpq 14.5 preinstalled on the server (via the recipes system) and therefore autobrew is not used. I think you can fix this by increasing to
--atleast-version=15in the configure script here:RPostgres/configure
Lines 21 to 22 in 2b2081c
Bump preferred libpq version on MacOS to 15
jeroen commentedon Apr 19, 2024
@ilarischeinin could you test the binary package from r-universe? If that one works, it is very likely that the next CRAN release will also work for you:
krlmlr commentedon Apr 19, 2024
Let's keep it open for now until the fixes are confirmed.
7 remaining items
jeroen commentedon Apr 22, 2024
Well not if you use the binary packages. If you install from source, and no suitable libpq is found locally, it will get libpq from autobrew indeed.
ilarischeinin commentedon Apr 22, 2024
Right now
I'm trying to go up that list getting more paths working. While fixing 1. would of course be the best, even getting 3. to work would be nice. That's why I'm wondering if bumping the version in autobrew from 16.0 to 16.2 would help with that.
ilarischeinin commentedon Apr 22, 2024
Collecting from the various messages above
I'm not 100% sure that's causation there, or if there might be other moving parts too that are the real cause. Still, this makes me think if this might be something that was fixed separately in the 14.x/15.x/16.x major version paths in some x. And that's why bumping to e.g. 16.0 does not help.
ilarischeinin commentedon Apr 25, 2024
I asked a colleague who's on Apple Silicon to test this too (I'm still on Intel). The outcome was the same:
jeroen commentedon Apr 25, 2024
OK I'll try to build a static version of 16.2, so that we can rule that out.
Another moving piece may be the gss implementation. We can link against the one included with MacOS, and another one that is in homebrew. We use the one from MacOS because
krb5in macos does not have static libs, but there is a comment in homebrew that suggests it may not always work well.jeroen commentedon Apr 25, 2024
OK I have updated the binaries on https://r-dbi.r-universe.dev/RPostgres to 16.2. Could you test if this changes anything?
ilarischeinin commentedon Apr 25, 2024
Thank you! But it does not! 😭
Still the same error
And can confirm it is indeed 16.2:
So at least we know this is not about the version.
That
krb5thing does sound really interesting, especially considering that at first, the problem was only with one of our two Redshift servers (staging worked while production did not), even though they were running the same Redshift version. And then some time later this started to affect both. I wonder if that could be because at first they had somehow different types of certs (production had renewed while staging had not), and then later as staging renewed as well, it also started to be affected with this issue.With your better understanding on how those pre-compiled versions are generated vs. how it might happen when I compile locally, can you see a relevant difference between the two? Anything I could test, or anything I could do to increase logging to surface something more useful as for the error?
ilarischeinin commentedon Apr 25, 2024
FWIW, here's the full output from manual compilation, which results in a working binary.
jeroen commentedon Apr 25, 2024
Have you looked in your redshift server logs why it is rejecting the connection? That would probably give a clue where to look.
Also did you try if the version of MacOS mattes?
I do suspect the kerberos version could be related. On MacOS I see:
krb5-config --version # Kerberos 5 release 1.7-prereleaseWhich is a pretty old. Homebrew has krb5 1.21. I can try to build a static version of krb5 that we can use in the R package, but that might take a while.
s-u commentedon Apr 26, 2024
Can someone distill the bottom line here for me here, please? I'm about to bump the libraries on the CRAN build machines since we're about to do a full rebuild for R 4.4.0, so I'd like to know
jeroen commentedon Apr 26, 2024
We don't know... It seems like an auth failure for specific servers, I suspect krb5 included with MacOS is too old (it's about 15 years old). So we would need to build against a more recent kerberos, however building that as a static lib is not easy.
ilarischeinin commentedon Apr 26, 2024
I don't see anything in the
connectionlogs for this :/Nowadays I'm on Sonoma (as is the colleague who also tried this), but it looks like I had Big Sur when I originally filed the issue.
As I've installed
libpqwith brew, I also have brew'skrb5(1.21.2) installed as it's a dependency. So is that something that is different between when binaries are built from CRAN vs. compiling locally? I mean that CRAN binaries are not built with libpq coming from Homebrew, and as a consequence thekrb5is different and older?ilarischeinin commentedon Apr 26, 2024
One big challenge for troubleshooting is that this affects Redshit, i.e. a managed, commercial service. If it was e.g. Postgres, anyone could run the server via docker and test things. But I don't know if there is a Redshift instance running somewhere that would be publicly accessible. I can try to see if it would be possible for my workplace to temporarily turn on such a cluster.
markhwhiteii commentedon Jul 10, 2024
Read through all of this and am wondering if there is a solution yet? I ran into the same error:
See related StackOverflow question.