Re: psql client does not handle WSAEWOULDBLOCK on Windows - Mailing list pgsql-hackers

From Ning
Subject Re: psql client does not handle WSAEWOULDBLOCK on Windows
Date
Msg-id CAFGqpvgJ_Y-POTgogbmzxTbUXrci3gLqcasw1rAZ4y2Wh+9+nA@mail.gmail.com
Whole thread Raw
In response to psql client does not handle WSAEWOULDBLOCK on Windows  (Ning <ning94803@gmail.com>)
List pgsql-hackers
Hi Umar,

In the function of gss_read() if print the value of errno and SOCK_ERRNO
separately, I found the values are different:
 *ret = pqsecure_raw_read(conn, recv_buffer, length);
if (*ret < 0)
{
printf("errno: %d\n", errno);
printf("result_errno: %d\n", SOCK_ERRNO);
...

errno: 0
result_errno: 10035

Also refer to the
https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-wsagetlasterror,
It shows that the Windows Sockets function does not set errno, but uses
WSAGetLastError to report errors. And refer to the
https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-recv,
If the function fails, it should call the WSAGetLastError to get the expansion
error message. This further shows that the socket operation error will not be
reported through the errno.

So changing the error code check to use the SOCK_ERRNO instead of errno can be
properly handled on both Windows and other platforms.

To reproduce the issue, I used the following version of Postgres and MIT Kerberos:
PostgreSQL version: 16.3
MIT Kerberos Version 4.1
Operating System: Windows 11
Visual Studio 2022


On Tue, Jul 30, 2024 at 4:47 PM Ning <ning94803@gmail.com> wrote:
Description:
The connection fails with a non-blocking socket error when using psql on
Windows to connect to a PostgreSQL server with GSSAPI enabled. The error is
because the socket error code is obtained by WSAGetLastError() instead of
errno. This causes the value of errno to be incorrect when handling a
non-blocking socket error.

Steps to Reproduce:
1. Compile PostgreSQL client (psql) on Windows.
a. Make sure MIT Kerberos is installed. I use the latest version MIT Kerberos
Version 4.1.
b. Make sure GSSAPI is enabled
2. Attempt to connect to a PostgreSQL server using psql.
a. Set up the Kerberos server and configure the PostgreSQL server by referring
to https://github.com/50wu/kerberos-docker/blob/main/POSTGRES.README.md
b. change the entry to hostgssenc on PostgreSQL server pg_hba.conf and restart
hostgssenc    all    all    0.0.0.0/0    gss    include_realm=0 krb_realm=GPDB.KRB
c. Use the following command to connect to the database server
psql -h <hostname> -U "postgres/krb5-service-example-com.example.com" -d postgres
3. The connection fails with a non-blocking socket error. The error is something like:
psql: error: connection to server at "xxx", port 5432 failed:

Environment:
PostgreSQL version: 16.3
Operating System: Windows 11

Fix Steps:
In the gss_read function of src/interfaces/libpq/fe-secure-gssapi.c, change the
check of the error code to use the SOCK_ERRNO to make sure that EAGAIN,
EWOULDBLOCK and EINTR can be properly handled on Windows and other platforms.

The patch file is attached to this email, please review and consider merging it to
the main code library.

Thanks,
Ning Wu

pgsql-hackers by date:

Previous
From: Michael Paquier
Date:
Subject: Re: Injection points: preloading and runtime arguments
Next
From: Peter Eisentraut
Date:
Subject: Rename C23 keyword