[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Possible bug: Race condition when calling external commands during trap
From: |
Tillmann.Crueger |
Subject: |
Possible bug: Race condition when calling external commands during trap handling |
Date: |
Wed, 2 May 2012 13:16:23 +0200 |
Hi,
I have a problem with a trap handler in a script, which is doing some logging
and needs external commands (date and hostname). In some cases there seems to
be a race condition causing a syntax error every once in a while. I am assuming
it is a race condition, because the syntax errors only happen very very
infrequently.
I have produced the following script as a small example:
-----------------------
#!/bin/bash
log() {
local text="$(date +'%Y-%m-%d %H:%M:%S') $(hostname -s) $1"
echo $text >> /dev/null
}
thread() {
while true; do
log "Thread is running"
kill -ALRM $$
sleep 1
done
}
trap "log 'received ALRM'" ALRM
thread &
trap "kill $?; exit 0" INT TERM
while true; do
log "Main is running"
sleep 1
done
-----------------------
Very infrequently this script will fail with a syntax error in line 5 (echo
$text >> /dev/null). The actual error message is:
> /path/to/script.sh: command substitution: line 5: syntax error near
> unexpected token `)'
> /path/to/script.sh: command substitution: line 5: `hostname -s) $1'
Since there is not "hostname -s) $1" in line 5, I am assuming there also is an
off-by-one error and line 4 is actually meant (local text="$(date +'%Y-%m-%d
%H:%M:%S') $(hostname -s) $1").
I have encountered this problem both on bash 4.2.24(1)-release
(x86_64-pc-linux-gnu) on ubuntu 12.04 as well as on bash 4.1.2(1)-release
(x86_64-redhat-linux-gnu) on RHEL 6.2.
There may be something wrong with the way traps are used in this case, but the
documentation is very sparse on this topic. I also opened a question on
StackOverflow.com
(http://stackoverflow.com/questions/10194837/concurrent-logging-in-bash-scripts)
but did not receive any usefull answers yet.
Since this is a race condition, it might take a while for the bug to hit. In
some cases the script was running up to 30 minutes before the bug triggered.
Please let me know if you have any furhter questions or hints on how to resolve
this issue.
Thank you,
Till Crueger
- Possible bug: Race condition when calling external commands during trap handling,
Tillmann.Crueger <=
- Re: Possible bug: Race condition when calling external commands during trap handling, Bob Proulx, 2012/05/03
- Re: Possible bug: Race condition when calling external commands during trap handling, Andreas Schwab, 2012/05/03
- Re: Possible bug: Race condition when calling external commands during trap handling, Linda Walsh, 2012/05/21
- Re: Possible bug: Race condition when calling external commands during trap handling, Andreas Schwab, 2012/05/22
- Re: Possible bug: Race condition when calling external commands during trap handling, Linda Walsh, 2012/05/22
- Re: Possible bug: Race condition when calling external commands during trap handling, Greg Wooledge, 2012/05/22
- Re: Possible bug: Race condition when calling external commands during trap handling, Roman Rakus, 2012/05/22
- Re: Possible bug: Race condition when calling external commands during trap handling, Chet Ramey, 2012/05/22
- Re: Possible bug: Race condition when calling external commands during trap handling, Roman Rakus, 2012/05/22
- Re: Possible bug: Race condition when calling external commands during trap handling, Chet Ramey, 2012/05/22