pthread_abort from signal handlers

pthread_abort and pthread_cancel do not return when called from a signal
handler or an “at exit” handler when the main thread is waiting on a
semaphore. Both threads appear to be blocked on the semaphore. The
information for the second thread may be invalid.

pthread_abort and pthread_cancel return successfully if called from a third
thread. The aborted thread appears to be blocked on the semaphore. Again,
the thread information may be invalid.

I’m using QNX 6.3.0 SP1. I’ve attached a trivial sample program.

Thanks, Dennis




begin 666 TestAbort.c
M#0HC:6YC;‘5D92 <W1D;&EB+F@^#0HC:6YC;‘5D92 <W1D:6\N:#X-“B-I
M;F-L=61E(#QP=&AR96%D+F@^#0HC:6YC;'5D92 =6YI<W1D+F@^#0HC:6YC
M;‘5D92 \97)R;F\N:#X-"B-I;F-L=61E(#QS96UA<&AO<F4N:#X-“B-I;F-L
M=61E(#QS>7,O=‘EP97,N:#X-"B-I;F-L=61E(#QS>7,O<W1A="YH/@T*(VEN
M8VQU9&4@/&9C;G1L+F@^#0H-“B\JBHJBHJBHJBHJBHJBHJBHJBHJ
MBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJ
M
BHJBHJBHO#0HOBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJ
M
BHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJ
M+PT*+RHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJ
M
BHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJB-”@T*(V1E
M9FEN92!&04Q312 H,“D-“B-D969I;F4@5%)512 @#$I#0H-"G-T871I8R!P
M=&AR96%D7W0@5&AR96%D260@/2 M,3L-“G-T871I8R!P=&AR96%D7W0@06-T
M:6]N5&AR96%D260@/2 M,3L-”@T
#0IS=&%T:6,@=F]I9” @071%>&ET2&%N
M9&QE<BAV;VED3L-“G-T871I8R!V;VED(”!3:6=N86Q(86YD;&5R&EN=”!S
M:6=N=6TI.PT*<W1A=&EC(‘9O:60J(%1E<W14:’)E860H=F]I9"H@=6YU<V5D
M3L-"G-T871I8R!V;VEDB!!8W1I;VY4:’)E860H=F]I9"H@=6YU<V5D3L-
M"@T
#0HOBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJ
M
BHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJ+PT*+RHJ
MBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJ
M
BHJBHJBHJBHJBHJBHJBHJBHJBHJBHJB-“B\JBHJBHJBHJ
M
BHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJ
MBHJBHJBHJBHJBHJBHJBHJBHO#0H-“FEN=”!M86EN*&EN=”!A<F=C
M+”!C:&%R(“IA<F=V6UTI#0I[#0H@(”!I;G0@97)R;W)?9F]U;F0@/2!&04Q3
M13L-“B @(&EN=”!R971?8V]D93L-"B @(’-E;5]T(“IS96U?<'1R.PT*#0H@
M(”!P<FEN=&8H(D9I;&4@)7,L(&QI;F4@)60L(&9U;F-T:6]N(“5S7&XB+”!?
M7T9)3$5?7RP@7U],24Y%7U\L(%]?9G5N8U]?3L-"@T(” @:68@%-)1U]%
M4E(@/3T@<VEG;F%L("A324=)3E0L(%-I9VYA;$AA;F1L97(I
0T*(" @>PT*
M(" @(" @97)R;W)?9F]U;F0@/2!44E5%.PT*(" @?0T*#0H@("!I9B H1D%,
M4T4@/3T@97)R;W)?9F]U;F0I#0H@("![#0H@(" @("!I9B H," A/2!A=&5X
M:70H071%>&ET2&%N9&QE<BDI#0H@(" @("![#0H@(" @(" @("!E<G)O<E]F
M;W5N9" ](%12544[#0H@(" @("!]#0H@("!]#0H-"B @(&EF("A&04Q312 ]
M/2!E<G)O<E]F;W5N9"D-“B @('L-“B @(” @(&EF(”@M,2 ]/2!P=&AR96%D
M7V-R96%T92@F5&AR96%D260L($Y53$PL(%1E<W14:’)E860L($Y53$PI0T
M(" @(" @>PT*(" @(" @(" @97)R;W)?9F]U;F0@/2!44E5%.PT*(" @(" @
M?0T*(" @?0T*#0H@("!I9B H1D%,4T4@/3T@97)R;W)?9F]U;F0I#0H@("![
M#0H@(" @("!I9B H+3$@/3T@<‘1H<F5A9%]C<F5A=&4H)D%C=&EO;E1H<F5A
M9$ED+"!.54Q,+"!!8W1I;VY4:’)E860L($Y53$PI0T(" @(" @>PT*(" @
M(" @(" @97)R;W)?9F]U;F0@/2!44E5%.PT*(" @(" @?0T*(" @?0T*#0H@
M("!I9B H1D%,4T4@/3T@97)R;W)?9F]U;F0I#0H@("![#0H@(" @("!S>7-T
M96T@")R;2 M9B O9&5V+W-E;2]497-T06)O<G0B3L-“B @(” @(’-E;5]P
M=’(@/2!S96U?;W!E;B@B+U1E<W1!8F]R="(L#0H@(" @(" @(" @(" @(" @
M(" @(" @(" @3U]#4D5!5"!($]?15A#3"P@4U])4E=852!(%-?25)76$<@
M?"!37TE25UA/+" P3L-"@T(" @(" @:68@“AS96U?=” J2TQ(#T](’-E
M;5]P=’(I#0H@(" @("![#0H@(" @(" @("!E<G)O<E]F;W5N9" ](%12544[
M#0H@(" @("!]#0H@("!]#0H-“B @(&EF(“A&04Q312 ]/2!E<G)O<E]F;W5N
M9"D-“B @('L-“B @(” @(&9O<B H.SLI#0H@(” @(”![#0H@(” @(" @("!R
M971?8V]D92 ](’-E;5]W86ET*’-E;5]P=’(I.PT*(" @(" @(" @<’)I;G1F
M*")&:6QE(“5S+”!L:6YE(“5D+”!F=6YC=&EO;B E<RP@<F5T7V-O9&4@)61<
M;B(L#0H@(" @(" @(" @(" @(" @7U]&24Q%7U\L(%]?3$E.15]?+"!?7V9U
M;F-?7RP@<F5T7V-O9&4I.PT*(" @(" @?0T*(" @?0T*#0H@("!R971U<FX@
M15A)5%]354-#15-3.PT*?0T*#0HOBHJBHJBHJBHJBHJBHJBHJBHJ
MBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJ
M
BHJBHJBHJ+PT*+RHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBH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&QI;F4@)60L(&9U;F-T:6]N
M(“5S7&XB+”!?7T9)3$5?7RP@7U],24Y%7U\L(%]?9G5N8U]?3L-“B @('T-
M”@T
(" @<’)I;G1F*")&:6QE(“5S+”!L:6YE(“5D+”!F=6YC=&EO;B E<UQN
M(BP@7U]&24Q%7U\L(%]?3$E.15]?+"!?7V9U;F-?7RD[#0H@(" C96YD:68-
M"@T*(" @97AI="@W3L-“GT-”@T+RHJBHJBHJBHJBHJBHJBHJBHJ
M
BHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJ
MBHJBHJBHJB-“B\JBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJ
MBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJ
M
BHO#0HOBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJ
M
BHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJBHJ+PT*#0IS
M=&%T:6,@=F]I9”!!=$5X:71(86YD;&5R*‘9O:60I#0I[#0H@("!P<FEN=&8H
M(D9I;&4@)7,L(&QI;F4@)60L(&9U;F-T:6]N(“5S7&XB+”!?7T9)3$5?7RP@
M7U],24Y%7U\L(%]?9G5N8U]?3L-"@T(" @(VEF(# -"@T*(" @:68@$5/
M2R A/2!P=&AR96%D7V%B;W)T
%1H<F5A9$ED2D-“B @(”\O:68@$5/2R A
M/2!P=&AR96%D7V-A;F-E;"A4:’)E861)9"DI#0H@("![#0H@(" @("!P<FEN
M=&8H(D9I;&4@)7,L(&QI;F4@)60L(&9U;F-T:6]N(“5S7&XB+”!?7T9)3$5?
M7RP@7U],24Y%7U\L(%]?9G5N8U]?3L-“B @('T-”@T(" @<’)I;G1F*")&
M:6QE(“5S+”!L:6YE(“5D+”!F=6YC=&EO;B E<UQN(BP@7U]&24Q%7U\L(%]?
D3$E.15]?+"!?7V9U;F-?7RD[#0H@(" C96YD:68-“GT-”@T*
`
end

Dennis Miller wrote:

pthread_abort and pthread_cancel do not return when called from a signal
handler or an “at exit” handler when the main thread is waiting on a
semaphore. Both threads appear to be blocked on the semaphore. The
information for the second thread may be invalid.

pthread_abort and pthread_cancel return successfully if called from a third
thread. The aborted thread appears to be blocked on the semaphore. Again,
the thread information may be invalid.

I’m using QNX 6.3.0 SP1. I’ve attached a trivial sample program.

Thanks, Dennis

You’re calling “exit” from the signal handler. That
terminates the thread. Normally, one exits a signal
handler with “return”.

A signal handler does not have its own thread. Signals
work like interrupts on a system without a CPU dispatcher.
When a signal event is processed, some thread is interrupted,
and the signal executes in that thread, with that thread’s
stack. A signal handler is thus a forced subroutine call.
The interrupted thread may be in an inconsistent state.

As a result, there’s a very limited set of things one can
do in a signal handler. POSIX has a short list of allowed
system calls. It’s always safe, in the signal handler,
to change a static variable and return. Beyond that,
not much works.

In general, signals are a holdover from the UNIX of
the 1970s. Don’t use them much for interprocess communication;
there are far better mechanisms in QNX.

John Nagle

John Nagle wrote:

As a result, there’s a very limited set of things one can
do in a signal handler. POSIX has a short list of allowed
system calls. It’s always safe, in the signal handler,
to change a static variable and return. Beyond that,
not much works.

See

http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html#tag_02_04

for the list of POSIX calls allowed in a signal handler. So it was legal
to call “exit”, but the POSIX thread calls in the signal handler were not legal.

Somewhere there should be a list of allowed QNX-specific calls that are safe
in a signal handler, but I don’t know where that is.

John Nagle

John Nagle <nagle@downside.com> wrote:

John Nagle wrote:



See

"> http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html#tag_02_04> "

for the list of POSIX calls allowed in a signal handler. So it was legal
to call “exit”, but the POSIX thread calls in the signal handler were not legal.

Somewhere there should be a list of allowed QNX-specific calls that are safe
in a signal handler, but I don’t know where that is.

At the top of the library reference is a link to a “summary of safety
information”, in there is a table of each of:

Cancellation Points
Signal Safe functions
Thread Safe functions
Interrupt Handler Safe functions.

Also, for each function entry, at the bottom, is a table that tells
whether or not that particular function is safe in each of the above
contexts.

-David

David Gibbs
QNX Training Services
dagibbs@qnx.com

It looks like the problem is that the signal handler or the “at exit”
handler happens to be running in the thread that is being terminated.

You’re correct that exit() is not signal safe. The problem still occurs
without the exit() call. We send SIGPWR to warn processes about an
impending shutdown. The SIGPWR signal handler posts a semaphore. We send
SIGTERM to unconditionally terminate processes even if they are hung up
somewhere.

Dennis


“John Nagle” <nagle@downside.com> wrote in message
news:eds78p$c0h$1@inn.qnx.com

Dennis Miller wrote:
pthread_abort and pthread_cancel do not return when called from a signal
handler or an “at exit” handler when the main thread is waiting on a
semaphore. Both threads appear to be blocked on the semaphore. The
information for the second thread may be invalid.

pthread_abort and pthread_cancel return successfully if called from a
third thread. The aborted thread appears to be blocked on the semaphore.
Again, the thread information may be invalid.

I’m using QNX 6.3.0 SP1. I’ve attached a trivial sample program.

Thanks, Dennis

You’re calling “exit” from the signal handler. That
terminates the thread. Normally, one exits a signal
handler with “return”.

A signal handler does not have its own thread. Signals
work like interrupts on a system without a CPU dispatcher.
When a signal event is processed, some thread is interrupted,
and the signal executes in that thread, with that thread’s
stack. A signal handler is thus a forced subroutine call.
The interrupted thread may be in an inconsistent state.

As a result, there’s a very limited set of things one can
do in a signal handler. POSIX has a short list of allowed
system calls. It’s always safe, in the signal handler,
to change a static variable and return. Beyond that,
not much works.

In general, signals are a holdover from the UNIX of
the 1970s. Don’t use them much for interprocess communication;
there are far better mechanisms in QNX.

John Nagle

John Nagle <nagle@downside.com> wrote:

Somewhere there should be a list of allowed QNX-specific calls that are safe
in a signal handler, but I don’t know where that is.

See the Summary of Safety Information appendix of the Neutrino Library
Reference.


Steve Reid stever@qnx.com
Technical Editor
QNX Software Systems