public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Pedro Alves <palves@redhat.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: tom@tromey.com, gdb-patches@sourceware.org
Subject: Re: [PATCH] Readline: Cleanup some warnings
Date: Wed, 20 Mar 2019 17:50:00 -0000	[thread overview]
Message-ID: <5751c371-3313-fc24-e4d7-83214eb99d0b@redhat.com> (raw)
In-Reply-To: <83o965sax9.fsf@gnu.org>

On 03/20/2019 05:38 PM, Eli Zaretskii wrote:
>> Cc: tom@tromey.com, gdb-patches@sourceware.org
>> From: Pedro Alves <palves@redhat.com>
>> Date: Wed, 20 Mar 2019 15:46:47 +0000
>>
>>> I agree: the right solution would be for the Readline's SIGINT handler
>>> to stop the main thread (e.g., by using SuspendThread).
>>
>> I don't sure how having the SIGINT handler stop the main thread is
>> a 100% correct solution.  By the time you stop it, the main thread can well
>> be already running readline code, halfway through updating some data
>> structure, even if the mainline code disabled SIGINT temporarily,
>> with _rl_block_sigint, because by the time the mainline code calls 
>> _rl_block_sigint, the SIGINT thread may have already have been spawned.
> 
> How is this different from what happens on Posix platforms, where the
> SIGINT handler can be invoked at any moment, while Readline might be
> doing anything at all?

On Posix platforms the signal handler runs in the same thread as the
mainline code.  There's _never_ any parallel execution.  When the
mainline code calls _rl_block_sigint, you're absolutely sure that the
mainline code that follows will not be running in parallel with
a SIGINT handler.

> 
>> Also, you're not ever supposed to use SuspendThread for synchronization, I
>> believe.
> 
> We are also not supposed to mix CRT and Win32 API functions, but we do
> that all the time.

That's not at all the same thing, I'm afraid.  We're not talking about
"in theory you shouldn't, but in practice it's OK.".  We're talking about
the very real fact that you cannot use it to synchronize correctly,
as shown in the blog post I pasted below.  If you're using the function
to pause a thread, and then do things, but the thread doesn't actually
pause before you do things, then you have a problem.

> 
>> MSDN at <https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-suspendthread> says:
>>
>>  "This function is primarily designed for use by debuggers. It is not intended
>>  to be used for thread synchronization."
>>
>> And here <https://devblogs.microsoft.com/oldnewthing/?p=44743> it says:
>>
>>   "The Suspend­Thread function tells the scheduler to suspend the thread
>>   but does not wait for an acknowledgment from the scheduler that the suspension
>>   has actually occurred."
> 
> GNU Make does it for many years, and I have yet to hear a single
> complaint about that part.

Not hearing a complaint does not mean that the problem isn't real.
It probably simply means that the race window is narrow, so the number
of people that observe any issue, _and_ are willing to report an issue
is small, and also, is someone notices something odd, it may be simply
impossible to tell what happened and realize what the problem was,
after the fact.

> 
>> I think a mutex/lock for synchronization would be a better solution within
>> readline.  Make all mainline readline entry points grab a mutex on entry
>> and release it on exit.  Likewise, make the readline signal handler
>> hold a mutex around any code that is unsafe to run in parallel
>> with mainline code.
> 
> That's fine with me, but is much more complex, and will probably slow
> down the code.  I won't object if you want to do it that way, though.
> 
>> I'm not sure whether readline still installs its own signal handler
>> internally in other situations, but it'd be worth it to check that.
>> Maybe we never run any readline signal handler on Windows at all
>> nowadays with recent readlines, which would simplify things for us,
>> rendering the gdb hack in question obsolete.
> 
> Maybe we should bring Chet into this discussion.

Thanks,
Pedro Alves

  reply	other threads:[~2019-03-20 17:50 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-30  8:57 Alan Hayward
2019-01-31  7:59 ` Joel Brobecker
2019-01-31 10:02   ` Alan Hayward
2019-01-31 17:24     ` Alan Hayward
2019-02-01  8:05       ` Joel Brobecker
2019-02-01 12:47         ` Tom Tromey
2019-02-01 18:54           ` Philippe Waroquiers
2019-02-06 19:56             ` Pedro Alves
2019-03-17 17:30               ` Tom Tromey
2019-03-17 18:35                 ` Eli Zaretskii
2019-03-19 16:04                   ` Tom Tromey
2019-03-19 18:37                     ` Eli Zaretskii
2019-03-19 19:02                     ` Pedro Alves
2019-03-19 19:04                       ` Pedro Alves
2019-03-19 20:14                       ` Eli Zaretskii
2019-03-20  8:55                         ` Eli Zaretskii
2019-03-20 15:46                         ` Pedro Alves
2019-03-20 15:50                           ` Pedro Alves
2019-03-20 17:39                           ` Eli Zaretskii
2019-03-20 17:50                             ` Pedro Alves [this message]
2019-03-20 18:01                               ` Eli Zaretskii
2019-03-20 18:28                                 ` Pedro Alves
2019-03-21 17:31                           ` Pedro Alves
2019-03-21 18:30                             ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5751c371-3313-fc24-e4d7-83214eb99d0b@redhat.com \
    --to=palves@redhat.com \
    --cc=eliz@gnu.org \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).