[PATCH] pg_upgrade: handle failed nonblocking connects on Windows

Поиск
Список
Период
Сортировка
Искать

[PATCH] pg_upgrade: handle failed nonblocking connects on Windows

От:
Harrison Booth <harrisontbooth@gmail.com>
Дата:
Вложения:
pg_upgrade can stall when localhost first reaches a refused IPv6
endpoint on Windows. Its aggregate select loop omits the exception
set, so PQconnectPoll does not process the failure and try IPv4. Libpq
already handles this in its single-socket helper.

The patch handles exceptional readiness in pg_upgrade's existing state
machine. Its Windows TAP test checks both successful-first IPv4 and
refused-first localhost fallback, skipping when resolver/refusal
preconditions are unavailable.

All seven assertions pass on MSVC ARM64, MSVC x64 and MinGW x64. The
x64 runs use emulation on the ARM64 host. Earlier copied-upgrade tests
from released 18.0 x64 and official 19rc1 ARM64 also preserve the
seeded data.

The combined patch includes the tested production fix and test
controls. Exact revisions and coverage are recorded with the evidence.

Best,

Harrison

Re: [PATCH] pg_upgrade: handle failed nonblocking connects on Windows

От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
On 13/10/2026 23:58, Harrison Booth wrote:
> pg_upgrade can stall when localhost first reaches a refused IPv6
> endpoint on Windows. Its aggregate select loop omits the exception
> set, so PQconnectPoll does not process the failure and try IPv4. Libpq
> already handles this in its single-socket helper.
> 
> The patch handles exceptional readiness in pg_upgrade's existing state
> machine. Its Windows TAP test checks both successful-first IPv4 and
> refused-first localhost fallback, skipping when resolver/refusal
> preconditions are unavailable.
> 
> All seven assertions pass on MSVC ARM64, MSVC x64 and MinGW x64. The
> x64 runs use emulation on the ARM64 host. Earlier copied-upgrade tests
> from released 18.0 x64 and official 19rc1 ARM64 also preserve the
> seeded data.

Is there any harm in checking for the exception set on other systems too?

> @@ -344,7 +347,18 @@ select_loop(int maxFd, fd_set *input, fd_set *output)
>  		*input = save_input;
>  		*output = save_output;
>  
> +#ifdef WIN32
> +		/* Winsock reports failed nonblocking connects as exceptions. */
> +		exceptions = save_output;
> +		i = select(maxFd + 1, input, output, &exceptions, NULL);
> +		if (i > 0)
> +		{
> +			for (unsigned int j = 0; j < exceptions.fd_count; j++)
> +				FD_SET(exceptions.fd_array[j], output);
> +		}
> +#else
>  		i = select(maxFd + 1, input, output, NULL, NULL);
> +#endif

Looks wrong to set merge the exception set with the writable set like 
that. I think it'd be better to add an 'exceptions' argument to the 
function.

As a side-note, I'm not convinced that saving the fdsets and looping 
here on EINTR is really worth it, the caller would reconstruct the 
fdsets and loop back here anyway if there's no work to do. But that's 
not new with this patch.

- Heikki



Re: [PATCH] pg_upgrade: handle failed nonblocking connects on Windows

От:
Harrison Booth <harrisontbooth@gmail.com>
Дата:
Вложения:
I agree here. v2 (attached)  passes a separate exception set through
select_loop() and checks it on all platforms. The existing EINTR loop
is unchanged.

All seven Windows assertions pass on native x64 and ARM64. Linux
passes eight existing upgrade scripts plus EINTR,
exceptional-readiness & refused-connect controls.

I also tested it alongside the --initdb v8 proposal on 89e06d while
retaining the test registrations. It applies to 1d45c5d.

Best,
Harrison

Re: [PATCH] pg_upgrade: handle failed nonblocking connects on Windows

От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
On 16/10/2026 21:28, Harrison Booth wrote:
> I agree here. v2 (attached)  passes a separate exception set through
> select_loop() and checks it on all platforms. The existing EINTR loop
> is unchanged.
> 
> All seven Windows assertions pass on native x64 and ARM64. Linux
> passes eight existing upgrade scripts plus EINTR,
> exceptional-readiness & refused-connect controls.
> 
> I also tested it alongside the --initdb v8 proposal on 89e06d while
> retaining the test registrations. It applies to 1d45c5d.

> @@ -396,13 +400,16 @@ wait_on_slots(UpgradeTaskSlot *slots, int numslots)
>  		if (slots[i].sock < 0)
>  			pg_fatal("invalid socket");
>  		FD_SET(slots[i].sock, slots[i].select_mode ? &input : &output);
> +		/* Failed nonblocking connects are exceptional on Windows. */
> +		if (!slots[i].select_mode)
> +			FD_SET(slots[i].sock, &exceptions);
>  		maxFd = Max(maxFd, slots[i].sock);
>  	}

Any reason to not set the exception set when select_mode == true ?

>  
>  	/*
>  	 * If we found socket(s) to wait on, wait.
>  	 */
> -	if (select_loop(maxFd, &input, &output) == -1)
> +	if (select_loop(maxFd, &input, &output, &exceptions) == -1)
>  		pg_fatal("%s() failed: %m", "select");
>  
>  	/*

- Heikki


FAQ