The problem is that you're running TrueCrypt in the background, keeping the fd open. You should close the fd to prevent background processes from hanging on to the lock.
In lieu of your actual code, here's a test case:
foo() {
(
flock -n 9 && echo "ok" || { echo failed; exit 1; }
sleep 10 &
) 9> lock
}
foo; foo
# Output:
# ok
# failed
sleep
is forked with fd 9 open, causing the lock to be kept. Let's close fd 9 when backgrounding the process:
foo() {
(
flock -n 9 && echo "ok" || { echo failed; exit 1; }
sleep 10 9>&- &
# ^-------- Right here
) 9> lock
}
foo; foo
# Output:
# ok
# ok