[Top][All Lists]

[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


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 

I have produced the following script as a small example:



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

trap "log 'received ALRM'" ALRM 

thread &
trap "kill $?; exit 0" INT TERM

while true; do
  log "Main is running"
  sleep 1


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 
 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

reply via email to

[Prev in Thread] Current Thread [Next in Thread]