Assorted Win32 error handling fixes (CreateThread, pgwin32_select, pg_usleep)

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

Assorted Win32 error handling fixes (CreateThread, pgwin32_select, pg_usleep)

От:
Sehrope Sarkuni <sehrope@jackdb.com>
Дата:
Вложения:
Hi-

While trying out some new analysis tooling, I noticed a few small bugs
in error handling. Attached are four patches:

0001: CreateThread() returns NULL on failure[1], not
INVALID_HANDLE_VALUE, so the failure check in the win32 timer code
could never fire. A failed thread creation would silently disable all
timer-based timeouts rather than reporting FATAL.

0002: Same CreateThread() return value mixup in pg_test_fsync.

0003: The events array in pgwin32_select() is sized for 2*FD_SETSIZE
socket events, but the signal event is appended after the sockets. If
the read and write fd sets are completely disjoint and full, the
signal event is written one element past the end of the array. Fixed
by sizing the array as 2*FD_SETSIZE + 1.

0004: pg_usleep() on win32 only checks for WAIT_OBJECT_0 from
WaitForSingleObject()[2], so WAIT_FAILED is silently treated the same as
a timeout, i.e. an immediate return. A caller using pg_usleep() in a
retry loop would spin at full speed if the event handle were ever
bad. Changed to handle WAIT_OBJECT_0 and WAIT_TIMEOUT explicitly and
elog(FATAL) on anything else.

0004 deserves some eyeballs since it changes behavior on the
failure path rather than just fixing a dead check. The first three
are mechanical.

Passes all checks with the new GitHub Actions CI:


Regards,
-- Sehrope Sarkuni
Founder & CEO | JackDB, Inc. | https://www.jackdb.com/

Re: Assorted Win32 error handling fixes (CreateThread, pgwin32_select, pg_usleep)

От:
Harrison Booth <harrisontbooth@gmail.com>
Дата:
Sehrope,

I reviewed all four v2 patches against master 8e82d6610e7 and found no
correctness blocker introduced by the changes. Builds passed on native
Windows ARM64 and emulated x64, with the other Windows changes in my
validation checkout applied as well.

I also tested source-extracted probes on both architectures. Returning
NULL from the affected CreateThread calls reaches the revised fatal
branches; the original checks miss it. An invalid event handle
produces WAIT_FAILED and reaches the revised pg_usleep fatal branch.
Timeout, signal dispatch/EINTR, and the startup NULL-handle fallback
retain their behavior. These probes use test fatal handlers rather
than a complete instrumented backend.

For the array change, a synthetic full-capacity boundary probe
overwrites the guard with the original allocation and preserves it
with the extra slot. This verifies the bounds calculation, not a
successful wait with that many handles. The existing
MAXIMUM_WAIT_OBJECTS limit is a separate issue and doesn't block this
correction.

Happy to provide the probe sources if useful.

Best,

Harrison

Original discussion:
https://www.postgresql.org/message-id/CAH7T-apQipawP3XoUk=9yPTazDZsYHsfKrpPUygFk3UDGbCb3w@mail.gmail.com


Re: Assorted Win32 error handling fixes (CreateThread, pgwin32_select, pg_usleep)

От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
On 29/08/2026 22:51, Sehrope Sarkuni wrote:
> Attached is v2, rebased over master. Only 0003 changed to remove a
> declaration it had as context. The fixes are identical to v1.
> 
> Of the four, only 0001 has any practical server impact. If CreateThread
> fails, every timer-based timeout in that process is silently disabled
> for the rest of its lifetime. 0002 can hang pg_test_fsync. 0003 and
> 0004 are defensive fixes with no reachable in-core trigger.

Committed the CreateThread fixes, patches 0001 and 0002. Thanks. Didn't 
look at the others.

- Heikki



FAQ