Skip to content

Commit b403426

Browse files
committed
Fix race condition - envsubst $file | tee $file
When running `envsubst $file` and pipe to `tee $file` there is a race causing the targe file to be wiped. The following script can be used to reproduce the race condition: ``` #!/bin/sh echo -e "foo\nbar\nbaz" > /tmp/test_file count=0 while [ -s /tmp/test_file ]; do envsubst < /tmp/test_file | tee /tmp/test_file > /dev/null ((count+=1)) done if [ ! -s /tmp/test_file ]; then echo "File was wiped after $count run's of: envsubst < \$file | tee \$file" fi ``` ``` $ sh test.sh File was wiped after 16 run's of: envsubst < $file | tee $file $ sh test.sh File was wiped after 42 run's of: envsubst < $file | tee $file $ sh test.sh File was wiped after 37 run's of: envsubst < $file | tee $file $ sh test.sh File was wiped after 73 run's of: envsubst < $file | tee $file $ sh test.sh File was wiped after 4 run's of: envsubst < $file | tee $file ``` Jira: OSPRH-17012 Jira: OSPCIX-870
1 parent 4d0ee77 commit b403426

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

templates/ironicinspector/bin/inspector-pxe-init.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,19 @@ sed -e "/BLOCK_PODINDEX_${PODINDEX}_BEGIN/,/BLOCK_PODINDEX_${PODINDEX}_END/p" \
3333
sed -e "/BLOCK_PODINDEX_${PODINDEX}_BEGIN/d" \
3434
-e "/BLOCK_PODINDEX_${PODINDEX}_END/d" \
3535
-i ${DNSMASQ_CFG}
36-
envsubst < ${DNSMASQ_CFG} | tee ${DNSMASQ_CFG}
36+
37+
# OSPCIX-870: Copy the config to tempdir to avoid race condition where pipe to
38+
# tee may wipe the file.
39+
export TMP_DNSMASQ_CFG=/var/tmp/dnsmasq.conf
40+
cp ${DNSMASQ_CFG} ${TMP_DNSMASQ_CFG}
41+
envsubst < ${TMP_DNSMASQ_CFG} | tee ${DNSMASQ_CFG}
3742

3843
export INSPECTOR_IPXE=/var/lib/ironic/inspector.ipxe
39-
envsubst < ${INSPECTOR_IPXE} | tee ${INSPECTOR_IPXE}
44+
# OSPCIX-870: Copy the config to tempdir to avoid race condition where pipe to
45+
# tee may wipe the file.
46+
export TMP_INSPECTOR_IPXE=/var/tmp/inspector.ipxe
47+
cp ${INSPECTOR_IPXE} ${TMP_INSPECTOR_IPXE}
48+
envsubst < ${TMP_INSPECTOR_IPXE} | tee ${INSPECTOR_IPXE}
4049

4150
# run common pxe-init script
4251
/usr/local/bin/container-scripts/pxe-init.sh

0 commit comments

Comments
 (0)