threads / patch / 66704

patchRe: [PATCH] sigchain: recover from concurrent double-pops of the same signal

Subject: Re: [PATCH] sigchain: recover from concurrent double-pops of the same signal

## tl;dr

4 messages between Oct 29, 2026 and Oct 31, 2026. Diffs are folded; open one to read it.

replies: 3people: 3as markdown or json

ian4hu· Oct 29, 2026, 14:15 UTC · lore
Hi Phillip,

Thanks for the follow-up. All three points make sense; let me confirm what I plan to do so we are on the same page.

On SIGPIPE: you are right that my wording was wrong. Blocking is not ignoring, and the write() fails with EPIPE either way. What changes is that the signal is not delivered while it is blocked; and since a SIGPIPE raised by a write is thread-directed, it stays pending on the writing thread instead of being picked up by the main thread. If that thread never unblocks it, it is never delivered, so a worker that blocks SIGPIPE simply stops dying of it. I left SIGPIPE out precisely to keep the "upstream of a pipe dies of SIGPIPE" behaviour that common-init.c restores. The set I plan to block is SIGINT, SIGHUP, SIGTERM and SIGQUIT. If you think SIGPIPE belongs in it too, I will add it.

On the blocking pattern: agreed. I will block once around the spawn loop and restore before the join loop, rather than around each pthread_create(), via a small guard pair

    struct signal_mask_guard guard;
    block_sigchain_signals(&guard);
    for (i = 0; i < nr_threads; i++)
        pthread_create(&t[i], NULL, fn, &data[i]);
    restore_sigchain_signals(&guard);

plus a git_pthread_create() wrapper on top for the single-thread call sites. The loops I have in mind are preload_index(), index-pack, pack-objects, grep, name-hash and the read-cache entry loader; the fsmonitor--daemon and simple-ipc threads I would leave alone for now, as they have their own lifecycle and signal handling.

On sigaction: agreed as well. The handled signal is deferred by default, so the remaining hole is a handler being interrupted by one of the other common signals that share the same handler. I will set sa_mask to sigfillset() and save/restore the whole struct sigaction, so pop() does not clobber the caller's sa_flags/sa_mask.

The one gap I cannot close is a thread created by library code rather than by us. libcurl's threaded resolver is the case I am aware of, depending on how libcurl is built; I do not know of an in-tree instance, and I will mention it in the cover letter rather than claim it is covered.

So I will drop this patch and build the series along those lines. As you suggested, I will wait a few days for Peff and Junio to comment before spending much time on it; if nobody objects I will send the replacement as v2.

Thanks, ian4hu

Phillip Wood· Oct 29, 2026, 16:03 UTC · re: ian4hu · lore
On 29/10/2026 14:15, ian4hu wrote:
Show 5 quoted lines
>
> I left SIGPIPE out precisely to keep the "upstream of a pipe
> dies of SIGPIPE" behaviour that common-init.c restores.  The set I
> plan to block is SIGINT, SIGHUP, SIGTERM and SIGQUIT.  If you
> think SIGPIPE belongs in it too, I will add it.
I think we want to block all signals when we create a thread, so that all signals are handled by the main (initial) thread.
Everything else sounds good
Thanks
Phillip
Show 31 quoted lines
> On the blocking pattern: agreed.  I will block once around the
> spawn loop and restore before the join loop, rather than around
> each pthread_create(), via a small guard pair
> >      struct signal_mask_guard guard;
> >      block_sigchain_signals(&guard);
>      for (i = 0; i < nr_threads; i++)
>          pthread_create(&t[i], NULL, fn, &data[i]);
>      restore_sigchain_signals(&guard);
> > plus a git_pthread_create() wrapper on top for the single-thread
> call sites.  The loops I have in mind are preload_index(),
> index-pack, pack-objects, grep, name-hash and the read-cache entry
> loader; the fsmonitor--daemon and simple-ipc threads I would leave
> alone for now, as they have their own lifecycle and signal
> handling.
> > On sigaction: agreed as well.  The handled signal is deferred by
> default, so the remaining hole is a handler being interrupted by
> one of the other common signals that share the same handler.  I
> will set sa_mask to sigfillset() and save/restore the whole
> struct sigaction, so pop() does not clobber the caller's
> sa_flags/sa_mask.
> > The one gap I cannot close is a thread created by library code
> rather than by us.  libcurl's threaded resolver is the case I am
> aware of, depending on how libcurl is built; I do not know of an
> in-tree instance, and I will mention it in the cover letter rather
> than claim it is covered.
> > So I will drop this patch and build the series along those lines.
> As you suggested, I will wait a few days for Peff and Junio to
> comment before spending much time on it; if nobody objects I will
> send the replacement as v2.
> > Thanks,
> ian4hu
Oswald Buddenhagen· Oct 30, 2026, 08:45 UTC · re: Phillip Wood · lore
On Thu, Oct 29, 2026 at 04:03:18PM +0000, Phillip Wood wrote:
Show 8 quoted lines
>On 29/10/2026 14:15, ian4hu wrote:
>> I left SIGPIPE out precisely to keep the "upstream of a pipe
>> dies of SIGPIPE" behaviour that common-init.c restores.  The set I
>> plan to block is SIGINT, SIGHUP, SIGTERM and SIGQUIT.  If you
>> think SIGPIPE belongs in it too, I will add it.
>
>I think we want to block all signals when we create a thread, so that >all signals are handled by the main (initial) thread.
>
that wording again does not address the fact that sigpipe is different due to being thread-directed [1]. even if the outcome is identical, it needs to be reasoned about separately.
[1] https://stackoverflow.com/questions/21718311/when-multi-thread-program-receive-a-sigpipe-signal-because-send-which-thread-wo
Phillip Wood· Oct 31, 2026, 14:50 UTC · re: Oswald Buddenhagen · lore
On 30/10/2026 08:45, Oswald Buddenhagen wrote:
Show 10 quoted lines
> On Thu, Oct 29, 2026 at 04:03:18PM +0000, Phillip Wood wrote:
>> On 29/10/2026 14:15, ian4hu wrote:
>>> I left SIGPIPE out precisely to keep the "upstream of a pipe
>>> dies of SIGPIPE" behaviour that common-init.c restores.  The set I
>>> plan to block is SIGINT, SIGHUP, SIGTERM and SIGQUIT.  If you
>>> think SIGPIPE belongs in it too, I will add it.
>>
>> I think we want to block all signals when we create a thread, so that >> all signals are handled by the main (initial) thread.
>>
> that wording again does not address the fact that sigpipe is different > due to being thread-directed [1]. even if the outcome is identical, it > needs to be reasoned about separately.
Oh, thanks for pointing that out, I hadn't realized some signals were directed at a specific thread rather than the process. According to my copy of The Linux Programming interface SIGBUS, SIGFPE, SIGILL and SIGSEGV are all thread-directed as well so, to ensure the process is killed if we receive one of those signals, we shouldn't be blocking any of those either. sigchain_push_common() installs a handler for SIGPIPE so, in principle, we could end up unwinding the chain in two threads, I'm not sure if that is likely in practice. If that is an issue, maybe we could set a flag in the signal handler to indicate it has already run (though I'm not sure it is possible to do that race-free without something like atomic_flag_test_and_set()).
Thanks
Phillip
> [1] https://stackoverflow.com/questions/21718311/when-multi-thread- > program-receive-a-sigpipe-signal-because-send-which-thread-wo

← back to recent threads