Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wfeNN-005Yra-00 for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Jul 2026 13:56:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wfeNK-007eQ0-1y for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Jul 2026 13:56:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wfeNK-007ePs-0e for pgsql-hackers@lists.postgresql.org; Fri, 03 Jul 2026 13:56:14 +0000 Received: from mail-yx1-xb132.google.com ([2607:f8b0:4864:20::b132]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wfeNH-00000001Stt-2E3U for pgsql-hackers@postgresql.org; Fri, 03 Jul 2026 13:56:13 +0000 Received: by mail-yx1-xb132.google.com with SMTP id 956f58d0204a3-6626b5ace23so2315199d50.0 for ; Fri, 03 Jul 2026 06:56:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783086969; x=1783691769; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=yGvI9kEgmORVAP0dFHApA8g5tNETpROPDok/iAQuPbc=; b=BSa1bXt8uAt/HOdYXBOUfoo+jljgkAO/E9gcHaW6ML7Hb0vrsmXcD2Pl7mpVyt9WSv 2IX/1TqFEs0e8dJ7DIXlYUjZyzdPtLcuuZF3dfDLxNHtqjBZGqv5Ao3C8z+J6v+XF/U4 wETO93sTTan2JyIsBZLcRmPFQN3nebFIKJ3xhUon9jvocQmCCKKR9KsflxPVFKb10Wqb 0EHi0IOYOlsYqQBEXsahtNl5VkPwZfPcLcKIenFg4N/8vZiPmLPsI9P9gwDbUcgcXug1 kf52sRVm42tJIVvQPqDU7EuNjKI1asq9YchwtXLgXM3eZsZrcztnP2M/g6X0p3yVqyeU obGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783086969; x=1783691769; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yGvI9kEgmORVAP0dFHApA8g5tNETpROPDok/iAQuPbc=; b=HC+VGVC1yGN5CL7TdUqbRDG9W8vG72nhiLA13tfRLnozeurd54Dfz+LRM1CldtynfV cVq8Is+jRBrzlYjVMmQj0hT3zWoWkjbIPujT0gaAKpRuOYEXp1aRGEoyzKKpsOxF+RTj noisBeS++pAHGL6Dz/94EDUzLzGOSjw/wh8Kjj1/NYAasJ1BsP88BjzAaFa6uArgZBKt pAR9LJ4i198Ib8M602omOtaddEXxCJAZzM9/M2cIkJdNuZTUNM7BNwGo4yOg/OkFMi3W Yb++ZHBB3jLKW4otUQ0n9fKHjGBufWuuCgRGgRaSb4vHUV53/RVskVCubUb1Lg4JgF9a y4sQ== X-Forwarded-Encrypted: i=1; AHgh+Ro47qkCM9oihPaB9SRCQUWg49LVGsWNhqZEyny1AuHi+VTqWmuD+7APHouj7yHR+RCW1Xw8WZtpNR2y/fBG@postgresql.org X-Gm-Message-State: AOJu0YzLkUyUKAZopu1Jne5eJpR7a8OQU8xFbPXMvKznK0NeUYjMS9PA 38OHa2OU9iA99jh5sBc4aHNwg9rqobmWZfyKSI1qC934hJGYJ7+fpMXD X-Gm-Gg: AfdE7ck9XsB1qywo2nWt0/G9nSad808kbEBgltUHpaqHS9lbMRHWYmzxffiuHXisFVa y05b7ecWnNIwtDtbCmoFVJklB5pgVIPTdcIpCoQRYLOtlN2fazDXrfy8poFjUUCBejQ06xljlFV +wPY1/pHahSac/Cpzz2j6mdXy7FHrbZ4aQrLj7qjtrwf642he/lZF2CDQv/SEm68muP52MyTEZF V8Em744VJjna5CnTh6yBSEant2eywbwPb7xJtC56JhCi/QoSEt+uQ8JkLFxOBJ0RyIxlIB8q0NV X1Pj1oj/9vvwhDUO7fQ1WYfQyzQ18ELQYoY1pJc03CPgt8R9f0T1WsjpE7XU22N4vRXGh1mknkl Nr0GG8JLNodC/Deb0NimbN9yT5jJ7hYeCLcflvEEIQDS5DPnSg2Q66FB1TdlsI4A+O7DoHm7DlZ r1H7zgV+rIeddNaoAvMU8tZnw20rirEBzbiMkMwX7WPkquq1Z8zTW04YW0wKVpbtjHKRqNn4F8n TgE95Vc55Bdxs77KP7ehM31zg== X-Received: by 2002:a05:690e:a4f:b0:664:eac2:b3d1 with SMTP id 956f58d0204a3-666368f742dmr3491978d50.11.1783086969179; Fri, 03 Jul 2026 06:56:09 -0700 (PDT) Received: from ?IPV6:2600:1700:8952:80:c066:d6c3:fa90:ffd1? ([2600:1700:8952:80:c066:d6c3:fa90:ffd1]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-666407cd44bsm825762d50.12.2026.07.03.06.56.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 03 Jul 2026 06:56:08 -0700 (PDT) Message-ID: Date: Fri, 3 Jul 2026 08:55:40 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Can we get rid of TerminateThread() in pg_dump? To: Thomas Munro , pgsql-hackers Cc: Tom Lane , Kyotaro Horiguchi References: Content-Language: en-US From: Bryan Green In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 7/2/2026 10:32 PM, Thomas Munro wrote: > Hi, > > Following up on this ancient discussion and resulting commit e652273e... > > https://www.postgresql.org/message-id/flat/11515.1464961470%40sss.pgh.pa.us#3e78a2af445dd7e566cf499023e8cb97 > > write(fd, ...) locks fd's entry in a user space descriptor table on > Windows, so if the terminated thread was also writing to stderr, I am > pretty sure it would hang. Someone with Windows might be able to > repro that by hacking the workers to write to stderr a lot? > WriteFile(_get_osfhandle(STDERR_FILENO), ...) might avoid that > specific issue, but for all I know that's just the tip of the iceberg > considering the socket stuff. > > Apparently this hasn't been a problem in practice. Error reporting > coinciding with ^C must be unlikely? I'd still like to find a > non-evil way to suppress log output, though. What if we atomically > pointed STDERR_FILENO to /dev/null, with dup2()? I wrote a patch to > try that idea out, but I don't have Windows, so I'm sharing this as a > curiosity in case anyone wants to try it and/or comment on all this. > Or has a better idea. Preferably that would work on Windows and Unix > (if it also used threads). > > (How I arrived at this obscure hypothetical problem: I've been > teaching pg_dump (and everything else) to use threads on Unix too, ie > harmonising Windows and Unix code paths. We certainly don't want > thread termination/cancellation in pg_threads.h (modern APIs don't > even have that, designers of older APIs regret it, including the > Windows people who are quite emphatic about there being no safe way to > use it[1]), so I wanted to find another way to silence error output. > close() would kinda work but cause chaos if another open() squats the > fd number, which leads to the idea of dup2(dummy, STDERR_FILENO). > Then I wondered if that would also work on Windows' fake fd system, ie > whether dup2(dummy, STDERR_FILENO) would play nicely with concurrent > write(STDERR_FILENO) or fwrite(stderr) as the file is switched. Yes, > as far as I can tell, but as soon as I read about the user space fd > locks that make that work, the above concrete problem with write() vs > TerminateThread() became clear.) > > [1] https://devblogs.microsoft.com/oldnewthing/20150814-00/?p=91811 I build pg_dump on Windows, so I tried this out (MSVC 19.51, VS 2026). It works. It compiles clean, and on a ^C during a parallel dump it suppresses the worker cancel messages the same way the TerminateThread call did-- I checked against unmodified master with the TerminateThread call still in place, and the output is identical-- "terminated by user" and nothing else. Moving write_stderr off fileno(stderr) to STDERR_FILENO is worth having on its own: that shim's comment notes _fileno returns -1 when the stream is closed, which is a state the shutdown path can be in. Opening NUL in the handler worked here; I didn't need to open it in advance. Since you mention moving parallel.c to threads on non-Windows, I've been replacing the socketpair/select() worker protocol on Windows with an in-process queue (a mutex and two condition variables)[1], heading the same direction to unify across platforms. There a worker signals its own death through the condition variable rather than the leader seeing pipe EOF, so ^C hits an extra pg_fatal("a worker process died unexpectedly") in the leader-- and your redirect swallows that one too. I put your patch on top of that series and hit ^C on a 2.3 GB dump repeatedly at -j 2, 4, 8, and 100: clean exit every time, "terminated by user", nothing else. pg_stat_activity right afterward showed no live COPYs, up through -j 100-- so the PQcancel()s land and the backends actually stop, which TerminateThread never did. So your change composes with that work. [1] https://www.postgresql.org/message-id/flat/8c712d76-ecf7-4749-a6d8-dddc01f298ec%40gmail.com -- Bryan Green EDB: https://www.enterprisedb.com